Integrating with VanillaJS
Using @authaction/web-sdk
Section titled “Using @authaction/web-sdk”npm install @authaction/web-sdkimport { 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 = "/";};<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><script type="module"> import { handleCallback } from "./src/oauth.js"; await handleCallback();</script><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>Sign in with a popup
Section titled “Sign in with a popup”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.
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:
<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