The same four things in four places: sign in, keep the session, verify a token on your server, and deal with the code request.
// Verifying a token your own front end sent you.
// This is the only call an API key is really for.
const API = "https://api.albos.org";
const API_KEY = process.env.CLOUD_AUTH_API_KEY!; // ca_...
export type VerifiedUser = {
id: string;
email: string;
displayName: string;
};
export async function verifyAccessToken(
accessToken: string,
): Promise<VerifiedUser | null> {
const res = await fetch(`${API}/auth.v1.AuthService/VerifyToken`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": API_KEY,
},
body: JSON.stringify({ accessToken }),
});
// 429 means the key's own allowance is spent. Back off; do not loop.
// 403 means the key may not do this at all. Retrying will never help.
if (!res.ok) throw new Error(`verify failed: ${res.status}`);
const data = await res.json();
// active is the answer. Do not read the token yourself.
// Signing out and disabling an account take effect at once; a token still
// inside its 15 minutes does not know that.
if (!data.active) return null;
return {
id: data.user.id,
email: data.user.email,
displayName: data.user.displayName,
};
}