handling all of the stuff with header logout and handling better jwttoken

This commit is contained in:
talksik
2022-04-01 13:48:30 -04:00
parent c769f40a15
commit 7d8d05ad39
13 changed files with 58 additions and 65 deletions
@@ -6,7 +6,6 @@ export function useAuthCheck() {
return useQuery("AUTH_CHECK", ApiCalls.authCheck, {
retry: false,
refetchOnWindowFocus: false,
enabled: NirvanaApi._jwtToken ? true : false,
});
}
+17 -23
View File
@@ -19,31 +19,25 @@ export default class NirvanaApi {
const fullUrl = localHost + url;
try {
let res;
if (privateRoute && !this._jwtToken)
throw Error("No jwt token available!");
let res;
if (privateRoute && !this._jwtToken) throw Error("No jwt token available!");
if (privateRoute && this._jwtToken) {
res = await fetch(fullUrl, {
method: method,
headers: { Authorization: this._jwtToken },
});
} else {
res = await fetch(fullUrl);
}
if (!res.ok) {
if (res.status === 401) throw new Error("You are not authorized here");
throw new Error("Something went wrong");
}
return await res.json();
} catch (error) {
console.log(error);
throw error;
if (privateRoute && this._jwtToken) {
res = await fetch(fullUrl, {
method: method,
headers: { Authorization: this._jwtToken },
});
} else {
res = await fetch(fullUrl);
}
if (!res.ok) {
if (res.status === 401) throw new Error("You are not authorized here");
throw new Error("Something went wrong");
}
return await res.json();
}
}
+5 -9
View File
@@ -1,19 +1,15 @@
import { atom } from "recoil";
export const $authTokens = atom<{
accessToken: string;
refreshToken: string;
idToken: string;
} | null>({
key: "AUTH_TOKENS", // unique ID (with respect to other atoms/selectors)
default: null, // default value (aka initial value)
});
export const $searchQuery = atom<string>({
key: "SEARCH_QUERY",
default: "",
});
export const $jwtToken = atom<string>({
key: "JWT_TOKEN",
default: null,
});
// conversation id
export const $selectedConversation = atom<string>({
key: "SELECTED_CONVERSATION",