nice auth flow working with verification and stuff with token

This commit is contained in:
talksik
2022-04-01 13:05:38 -04:00
parent 0e69e0ed64
commit 23eeee4228
6 changed files with 59 additions and 34 deletions
+6 -4
View File
@@ -1,10 +1,12 @@
import NirvanaApi, { login } from "./nirvanaApi";
import NirvanaApi, { authCheck, login } from "./nirvanaApi";
import { useMutation, useQuery } from "react-query";
import { $authTokens } from "./recoil";
import { useRecoilValue } from "recoil";
// ====== QUERIES
export function useAuthCheck() {
return useQuery("AUTH_CHECK", authCheck, {
retry: false,
});
}
export function useLogin() {
return useMutation("LOGIN", login, {});
@@ -20,6 +20,9 @@ export default class NirvanaApi {
const fullUrl = localHost + url;
let res;
if (privateRoute && !this._jwtToken)
throw Error("No jwt token available!");
if (privateRoute && this._jwtToken) {
res = await fetch(fullUrl, {
method: method,
@@ -36,10 +39,8 @@ export default class NirvanaApi {
}
return await res.json();
} catch (error) {
} catch (error: any) {
console.log(error);
throw Error(error);
}
}
}
@@ -55,6 +56,6 @@ export async function login(reqLoginTokens: {
);
}
export async function authCheck(jwtToken: string) {
return await NirvanaApi.fetch(`/user/authCheck`, "GET", true);
export async function authCheck(): Promise<void> {
return await NirvanaApi.fetch(`/user/authcheck`, "GET", true);
}