Frontend integrations
React, Vue, Astro, HTML, Svelte, Next.js and Nuxt use the same three browser helpers.
On this page
Mount the quickstart backend router first.
Install the current SDK source for these helpers.
All examples below use
startLogin('google'), getProfile() and logout() against that backend’s /auth routes.
There are no frontend secrets or localStorage login credentials. Browser-only OAuth is deferred.
Choose your frontend
import { useEffect, useState } from 'react';
import * as anyoauth from '@anyoauth/client';
export default function Login() {
const [user, setUser] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
anyoauth.getProfile().then(setUser).catch(() => setError('Could not load your session.'));
}, []);
async function signOut() {
try { await anyoauth.logout(); setUser(null); }
catch { setError('Could not sign out.'); }
}
return <section>
{error && <p role="alert">{error}</p>}
{user ? <>
<p>Signed in as {user.name ?? user.username ?? user.subject}</p>
<button onClick={signOut}>Sign out</button>
</> : <>
<button onClick={() => anyoauth.startLogin("google")}>Google</button>
<button onClick={() => anyoauth.startLogin("github")}>GitHub</button>
</>}
</section>;
}
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
<script setup>
import { onMounted, ref } from 'vue';
import * as anyoauth from '@anyoauth/client';
const user = ref(null);
const error = ref('');
onMounted(() => {
anyoauth.getProfile().then(value => { user.value = value; })
.catch(() => { error.value = 'Could not load your session.'; });
});
async function signOut() {
try { await anyoauth.logout(); user.value = null; }
catch { error.value = 'Could not sign out.'; }
}
</script>
<template>
<section>
<p v-if="error" role="alert">{{ error }}</p>
<template v-if="user">
<p>Signed in as {{ user.name ?? user.username ?? user.subject }}</p>
<button @click="signOut">Sign out</button>
</template>
<template v-else>
<button @click="anyoauth.startLogin('google')">Google</button>
<button @click="anyoauth.startLogin('github')">GitHub</button>
</template>
</section>
</template>
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
<section>
<p id="auth-status">Loading your session…</p>
<div id="login-buttons" hidden>
<button id="google">Google</button>
<button id="github">GitHub</button>
</div>
<button id="logout" hidden>Sign out</button>
</section>
<script>
import * as anyoauth from '@anyoauth/client';
document.querySelector('#google').addEventListener('click', () => anyoauth.startLogin("google"));
document.querySelector('#github').addEventListener('click', () => anyoauth.startLogin("github"));
document.querySelector('#logout').addEventListener('click', async () => {
try { await anyoauth.logout(); window.location.reload(); }
catch { document.querySelector('#auth-status').textContent = 'Could not sign out.'; }
});
try {
const user = await anyoauth.getProfile();
document.querySelector('#login-buttons').hidden = Boolean(user);
document.querySelector('#logout').hidden = !user;
document.querySelector('#auth-status').textContent = user
? 'Signed in as ' + (user.name ?? user.username ?? user.subject)
: 'Choose Google or GitHub.';
} catch {
document.querySelector('#auth-status').textContent = 'Could not load your session.';
}
</script>
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
<section>
<p id="auth-status">Loading your session…</p>
<div id="login-buttons" hidden>
<button id="google">Google</button>
<button id="github">GitHub</button>
</div>
<button id="logout" hidden>Sign out</button>
</section>
<script type="module">
import * as anyoauth from '/auth-client.js';
document.querySelector('#google').addEventListener('click', () => anyoauth.startLogin("google"));
document.querySelector('#github').addEventListener('click', () => anyoauth.startLogin("github"));
document.querySelector('#logout').addEventListener('click', async () => {
try { await anyoauth.logout(); window.location.reload(); }
catch { document.querySelector('#auth-status').textContent = 'Could not sign out.'; }
});
try {
const user = await anyoauth.getProfile();
document.querySelector('#login-buttons').hidden = Boolean(user);
document.querySelector('#logout').hidden = !user;
document.querySelector('#auth-status').textContent = user
? 'Signed in as ' + (user.name ?? user.username ?? user.subject)
: 'Choose Google or GitHub.';
} catch {
document.querySelector('#auth-status').textContent = 'Could not load your session.';
}
</script>
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
<script>
import { onMount } from 'svelte';
import * as anyoauth from '@anyoauth/client';
let user = $state(null);
let error = $state('');
onMount(() => {
anyoauth.getProfile().then(value => { user = value; })
.catch(() => { error = 'Could not load your session.'; });
});
async function signOut() {
try { await anyoauth.logout(); user = null; }
catch { error = 'Could not sign out.'; }
}
</script>
<section>
{#if error}<p role="alert">{error}</p>{/if}
{#if user}
<p>Signed in as {user.name ?? user.username ?? user.subject}</p>
<button onclick={signOut}>Sign out</button>
{:else}
<button onclick={() => anyoauth.startLogin("google")}>Google</button>
<button onclick={() => anyoauth.startLogin("github")}>GitHub</button>
{/if}
</section>
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
'use client';
import { useEffect, useState } from 'react';
import * as anyoauth from '@anyoauth/client';
export default function Login() {
const [user, setUser] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
anyoauth.getProfile().then(setUser).catch(() => setError('Could not load your session.'));
}, []);
async function signOut() {
try { await anyoauth.logout(); setUser(null); }
catch { setError('Could not sign out.'); }
}
return <section>
{error && <p role="alert">{error}</p>}
{user ? <>
<p>Signed in as {user.name ?? user.username ?? user.subject}</p>
<button onClick={signOut}>Sign out</button>
</> : <>
<button onClick={() => anyoauth.startLogin("google")}>Google</button>
<button onClick={() => anyoauth.startLogin("github")}>GitHub</button>
</>}
</section>;
}
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
<script setup>
import { onMounted, ref } from 'vue';
import * as anyoauth from '@anyoauth/client';
const user = ref(null);
const error = ref('');
onMounted(() => {
anyoauth.getProfile().then(value => { user.value = value; })
.catch(() => { error.value = 'Could not load your session.'; });
});
async function signOut() {
try { await anyoauth.logout(); user.value = null; }
catch { error.value = 'Could not sign out.'; }
}
</script>
<template>
<section>
<p v-if="error" role="alert">{{ error }}</p>
<template v-if="user">
<p>Signed in as {{ user.name ?? user.username ?? user.subject }}</p>
<button @click="signOut">Sign out</button>
</template>
<template v-else>
<button @click="anyoauth.startLogin('google')">Google</button>
<button @click="anyoauth.startLogin('github')">GitHub</button>
</template>
</section>
</template>
npm install @anyoauth/clientBrowser helpers · uses your backend sessionCurrent source example · published 0.1.0 predates these helpers
React uses an effect, Vue/Nuxt use onMounted, and Svelte uses onMount to read the
session after hydration. Next.js needs the shown 'use client' boundary. Astro runs
its <script> in the browser; do not call these helpers in server frontmatter.
Use profile.subject as the stable identity. Names, usernames and emails can be null.
These examples update the UI after logout only when the backend request succeeds.
Plain HTML
The HTML example imports /auth-client.js, a bundled copy of the browser SDK. After
installing the SDK source in your app, build it once:
npx esbuild node_modules/@anyoauth/client/index.js --bundle --format=esm --outfile=public/auth-client.jsServe public/auth-client.js at /auth-client.js alongside the HTML and your backend
routes. Plain browsers cannot resolve the npm package’s bare import name without a
bundler or import map. The quickstart
shows the browser helpers used by every framework.
Same-origin backend and development proxies
The browser helpers expect the auth routes on the frontend’s own origin. For a Vite
frontend and a separately listening backend, proxy /auth to that backend. Set the
managed router’s origin to the public browser origin, including its development
port, and register ${origin}/auth/callback exactly.
If you mount at /api/auth, pass that path to all three helpers:
anyoauth.startLogin('google', '/api/auth');
const profile = await anyoauth.getProfile('/api/auth');
// On the sign-out button: await anyoauth.logout('/api/auth');Cross-origin auth endpoints are not enabled by these examples. A static frontend can use a same-origin proxy to its backend; it is not a browser-only OAuth integration.
Next.js backend route
Put this in app/auth/[...route]/route.js:
import { createAuth } from "@anyoauth/node/auth";
const auth = createAuth({
clientId: process.env.ANYOAUTH_CLIENT_ID,
clientSecret: process.env.ANYOAUTH_CLIENT_SECRET,
origin: "http://localhost:3000",
});
// app/auth/[...route]/route.js
export const GET = auth.handle;
export const POST = auth.handle;
npm install @anyoauth/nodeManaged backend routerCurrent source example · published 0.1.0 predates these helpers
Register http://localhost:3000/auth/callback for the local example and configure an
HTTPS origin for production. Keep credentials in server environment variables.
Astro backend route
With an Astro server adapter, put this in src/pages/auth/[...route].js:
import { createAuth } from "@anyoauth/node/auth";
const auth = createAuth({
clientId: process.env.ANYOAUTH_CLIENT_ID,
clientSecret: process.env.ANYOAUTH_CLIENT_SECRET,
origin: "http://localhost:3000",
});
// src/pages/auth/[...route].js (Astro with a server adapter)
export const prerender = false;
export const GET = ({ request }) => auth.handle(request);
export const POST = ({ request }) => auth.handle(request);
npm install @anyoauth/nodeManaged backend routerCurrent source example · published 0.1.0 predates these helpers
Change the shown origin and registration to your actual Astro port (commonly 4321).
This endpoint needs server execution; a static-only Astro deployment must route /auth
to a separate backend. Server environment variables must be configured in that backend.
Existing sessions and protected resources
Browser profiles are for display, not permission checks. Your protected backend routes validate the authenticated SDK session or your own session store. Custom user hooks, sessions and routes remain available.