Refactor authentication and token handling.

This commit is contained in:
Mike Cao 2022-11-08 22:58:52 -08:00
parent 1a8c7c42f4
commit 67732b9b5a
7 changed files with 50 additions and 70 deletions

View file

@ -1,38 +1,19 @@
import { useEffect } from 'react';
import debug from 'debug';
import { useRouter } from 'next/router';
import { setItem } from 'next-basics';
import { AUTH_TOKEN } from 'lib/constants';
import useApi from 'hooks/useApi';
import { setUser } from 'store/app';
const log = debug('umami:sso');
export default function SingleSignOnPage() {
const router = useRouter();
const { get } = useApi();
const { token, url } = router.query;
useEffect(() => {
async function verify() {
if (url && token) {
setItem(AUTH_TOKEN, token);
const { ok, data } = await get('/auth/verify');
if (ok) {
log(data);
setUser(data);
if (url) {
await router.push(url);
}
}
router.push(url);
}
if (token) {
verify();
}
}, [token]);
}, [url, token]);
return null;
}