Skip to content

Integrating with VanillaJS

Terminal window
npm install @authaction/web-sdk
src/oauth.js
import { AuthActionClient } from "@authaction/web-sdk";
export const client = new AuthActionClient({
domain: import.meta.env.VITE_AUTHACTION_DOMAIN,
clientId: import.meta.env.VITE_AUTHACTION_CLIENT_ID,
redirectUri: import.meta.env.VITE_AUTHACTION_REDIRECT_URI,
postLogoutRedirectUri: import.meta.env.VITE_AUTHACTION_LOGOUT_REDIRECT_URI,
cacheLocation: "localstorage",
});
export const login = () => client.loginWithRedirect();
export const signup = () =>
client.loginWithRedirect({ authorizationParams: { screen_hint: "signup" } });
export const logout = () => client.logout();
export const handleCallback = async () => {
await client.handleRedirectCallback();
window.location.href = "/";
};
index.html
<script type="module">
import { client, login } from "./src/oauth.js";
const isAuthenticated = await client.isAuthenticated();
if (isAuthenticated) {
window.location.replace("/dashboard.html");
} else {
document.getElementById("login-btn").addEventListener("click", login);
}
</script>
callback.html
<script type="module">
import { handleCallback } from "./src/oauth.js";
await handleCallback();
</script>
dashboard.html
<script type="module">
import { client, logout } from "./src/oauth.js";
const user = await client.getUser();
if (!user) {
window.location.replace("/");
}
document.getElementById("name").textContent = user.name;
document.getElementById("logout-btn").addEventListener("click", logout);
</script>

loginWithPopup() opens the AuthAction login page in a popup instead of redirecting the whole page. Social sign-in such as Google, GitHub or Microsoft works the same way inside it. The React, Vue and Angular wrappers expose the same method.

src/oauth.js
export const loginWithPopup = () => client.loginWithPopup();

The popup returns to your redirect URI. On that page, pass the result back to the page that opened it:

callback.html
<script type="module">
import { AuthActionClient } from "@authaction/web-sdk";
import { handleCallback } from "./src/oauth.js";
if (window.opener) {
AuthActionClient.handlePopupCallback();
} else {
await handleCallback();
}
</script>

To skip the login page and open one provider directly, pass its connection. Use the connection’s name, or its type, such as google, github or microsoft, when the application has one connection of that type:

export const loginWithGoogle = () =>
client.loginWithPopup({ authorizationParams: { connection: "google" } });

loginWithPopup() resolves once the user is signed in. It rejects if the browser blocks the popup or the user closes it before signing in, so call it from a click handler.

Full SDK reference: github.com/authaction/authaction-web-sdk