Update index.tsx
This commit is contained in:
@@ -1,86 +1,52 @@
|
|||||||
import { $authFailureCount, $authTokens } from "../../controller/recoil";
|
|
||||||
import Channels, { STORE_ITEMS } from "../../electron/constants";
|
import Channels, { STORE_ITEMS } from "../../electron/constants";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useRecoilState, useSetRecoilState } from "recoil";
|
import { useRecoilState, useSetRecoilState } from "recoil";
|
||||||
|
|
||||||
|
import { $authTokens } from "../../controller/recoil";
|
||||||
import { CircularProgress } from "@mui/material";
|
import { CircularProgress } from "@mui/material";
|
||||||
import { FcGoogle } from "react-icons/fc";
|
import { FcGoogle } from "react-icons/fc";
|
||||||
import Logo from "../../components/Logo";
|
import Logo from "../../components/Logo";
|
||||||
|
import NirvanaApi from "../../controller/nirvanaApi";
|
||||||
|
import { useLogin } from "../../controller/index";
|
||||||
|
|
||||||
export default function Login({ onReady }: { onReady: () => void }) {
|
export default function Login() {
|
||||||
const setAuthTokens = useSetRecoilState($authTokens);
|
const { mutateAsync } = useLogin();
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
const [authFailureCount, setAuthFailureCount] =
|
|
||||||
useRecoilState($authFailureCount);
|
|
||||||
|
|
||||||
const continueAuth = () => {
|
const continueAuth = () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
|
||||||
// send to main process
|
// send to main process
|
||||||
// ipcRenderer.send(Channels.ACTIVATE_LOG_IN);
|
|
||||||
window.electronAPI.auth.initiateLogin();
|
window.electronAPI.auth.initiateLogin();
|
||||||
};
|
};
|
||||||
|
|
||||||
const logOut = () => {
|
|
||||||
window.electronAPI.store.set(STORE_ITEMS.AUTH_TOKENS, null);
|
|
||||||
};
|
|
||||||
|
|
||||||
const setAccessTokensAndContinue = (tokens: {
|
|
||||||
accessToken: string;
|
|
||||||
idToken: string;
|
|
||||||
refreshToken: string;
|
|
||||||
}) => {
|
|
||||||
setAuthTokens(tokens);
|
|
||||||
|
|
||||||
console.log(tokens.idToken);
|
|
||||||
|
|
||||||
onReady();
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// todo solve this better by just using refresh token in the backend
|
window.electronAPI.once(
|
||||||
|
Channels.GOOGLE_AUTH_TOKENS,
|
||||||
|
async (tokens: {
|
||||||
|
access_token: string;
|
||||||
|
id_token: string;
|
||||||
|
refresh_token: string;
|
||||||
|
}) => {
|
||||||
|
console.log("got tokens", tokens);
|
||||||
|
|
||||||
setAuthFailureCount((prevVal) => prevVal + 1);
|
setIsLoading(true);
|
||||||
|
// todo: implement refresh token procedure in api layer by sending refresh_token and such
|
||||||
|
|
||||||
// if failure count is 2, then just log the user out
|
const loginResponse = await mutateAsync({
|
||||||
if (authFailureCount >= 2) {
|
accessToken: tokens.access_token,
|
||||||
logOut();
|
idToken: tokens.id_token,
|
||||||
} else {
|
|
||||||
// todo: comment all of this else when trying to test with two users/instances
|
|
||||||
|
|
||||||
// see if we have tokens in localstorage in which case we can continue on
|
|
||||||
window.electronAPI.store
|
|
||||||
.get(STORE_ITEMS.AUTH_TOKENS)
|
|
||||||
.then((tokensFromStore: any) => {
|
|
||||||
if (tokensFromStore) {
|
|
||||||
setIsLoading(true);
|
|
||||||
|
|
||||||
const { access_token, id_token, refresh_token } = tokensFromStore;
|
|
||||||
|
|
||||||
setAccessTokensAndContinue({
|
|
||||||
accessToken: access_token,
|
|
||||||
idToken: id_token,
|
|
||||||
refreshToken: refresh_token,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
window.electronAPI.once(Channels.AUTH_TOKENS, (tokens: any) => {
|
const { jwtToken, userDetails } = loginResponse;
|
||||||
setIsLoading(true);
|
|
||||||
|
|
||||||
// todo: implement refresh token procedure in api layer by sending refresh_token and such
|
window.electronAPI.store.set(STORE_ITEMS.AUTH_SESSION_JWT, jwtToken);
|
||||||
const { access_token, id_token, refresh_token } = tokens;
|
}
|
||||||
|
);
|
||||||
setAccessTokensAndContinue({
|
|
||||||
accessToken: access_token,
|
|
||||||
idToken: id_token,
|
|
||||||
refreshToken: refresh_token,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// todo: figure out how to clean up with the preload api
|
// todo: figure out how to clean up with the preload api
|
||||||
// return () => {
|
// return () => {
|
||||||
// window.electronAPI.removeAllListeners(Channels.AUTH_TOKENS);
|
// window.electronAPI.removeAllListeners(Channels.GOOGLE_AUTH_TOKENS);
|
||||||
// };
|
// };
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user