diff --git a/package-lock.json b/package-lock.json index 1abdc72..be14e0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "google-task", - "version": "0.0.4", + "version": "0.0.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "google-task", - "version": "0.0.4", + "version": "0.0.5", "dependencies": { "@auth0/auth0-react": "^2.2.1", "@chakra-ui/icons": "^2.1.1", diff --git a/package.json b/package.json index fa3812a..7641524 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "google-task", "private": true, - "version": "0.0.5", + "version": "0.0.6", "type": "module", "scripts": { "dev": "vite", diff --git a/password.txt b/password.txt deleted file mode 100644 index 2d3f97b..0000000 --- a/password.txt +++ /dev/null @@ -1 +0,0 @@ -default_password \ No newline at end of file diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index fe7257a..cbea852 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -22,7 +22,7 @@ borsh = { version = "1.2.0", features = ["std", "borsh-derive", "derive", "bson" cocoon = "0.4.1" tauri-plugin-oauth = { git = "https://github.com/FabianLars/tauri-plugin-oauth", branch = "main" } tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } -tauri-plugin-context-menu = { git = "https://github.com/c2r0b/tauri-plugin-context-menu", branch = "main" } +tauri-plugin-context-menu = { version = "0.6.1" } dotenv = "0.15.0" lazy_static = "1.4.0" diff --git a/src/App.tsx b/src/App.tsx index db550a5..515a267 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,8 +4,8 @@ import { fetchUserProfile,getUserProfileFromStorage, getAccessToken, openAuthWi import { AccessToken, UserProfile } from "./types/googleapis"; import { loadContextmenu , pushNotification } from "./helpers/windowhelper"; import TaskPage from "./components/TaskPage"; -import { useRecoilState, useSetRecoilState } from "recoil"; -import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, attemptLoginState, attemptLogoutState, loggedInState, messageState, userProfileState } from "./config/states"; +import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil"; +import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, attemptLoginState, attemptLogoutState, loggedInSelector, messageState, userProfileState } from "./config/states"; import Header from "./components/ui/Header"; import { task } from "./types/taskapi"; import { listen_for_auth_code } from "./helpers/eventlistner"; @@ -15,7 +15,7 @@ loadContextmenu(); function App() { const [loading, setLoading] = useState(false); - const [loggedIn, setLoggedIn] = useRecoilState(loggedInState); + const loggedIn = useRecoilValue(loggedInSelector); const setProfile = useSetRecoilState(userProfileState); const setAccessToken = useSetRecoilState(accessTokenState); const [attemptedLogin, setAttemptedLogin] = useRecoilState(attemptLoginState); @@ -44,6 +44,20 @@ function App() { }, [toastMessage]) + useEffect(() => { + if (attemptedLogin) { + handleLogin(); + setAttemptedLogin(false); + } + }, [attemptedLogin]) + + useEffect(() => { + if (attemptedLogout) { + handleLogout(); + setAttemptedLogout(false); + } + }, [attemptedLogout]) + // to generate a port and listen to it @@ -76,60 +90,40 @@ function App() { // check the offline data for access token useEffect(() => { setLoading(true) - // get access token from storage - getAccessTokenFromStorage().then((accessToken) => { - try { - if (!accessToken) throw new Error("Signin required"); - pushNotification("Login Successful") - if (navigator.onLine) { - // fetch user profile - fetchUserProfile(accessToken.access_token).then((profile) => { - if(!profile) throw new Error("Something went wrong, please try again"); - setProfile(profile); - setAccessToken(accessToken.access_token); - setLoggedIn(true); - setLoading(false) - pushNotification(`welcome back ${profile.name}`) - }); - } - else { - getUserProfileFromStorage().then((profile) => { - if (!profile) throw new Error("Signin required"); - setProfile(profile); - setAccessToken(accessToken.access_token); - setLoggedIn(true); - pushNotification(`welcome back ${profile.name}`) - }); - } - }catch (err) { - console.log(err); - setLoading(false) - setToastMessage({ - title: "Error", - body: "Error signing in", - type: "error" - }) - } + handleInitialLogin ().catch((err) => { + console.log(err); + setLoading(false) + setToastMessage({ + title: "Error", + body: "Error signing in", + type: "error" + }) }).finally(() => { setLoading(false) }) }, []) + async function handleInitialLogin() { + // get access token from storage + const accessToken = await getAccessTokenFromStorage(); + if (!accessToken) throw new Error("Signin required"); + pushNotification("Login Successful") + const profile = navigator.onLine ? await fetchUserProfile(accessToken.access_token) : await getUserProfileFromStorage(); + if(!profile) throw new Error("Something went wrong, please try again"); + setProfile(profile); + setAccessToken(accessToken.access_token); + pushNotification(`welcome back ${profile.name}`) + } + async function handleLoadFrom(accessTokenBody: AccessToken) { try { - saveAccessToken(JSON.stringify(accessTokenBody, null, 2)).then(() => { - console.log("access token saved"); - }); + await saveAccessToken(JSON.stringify(accessTokenBody, null, 2)) setAccessToken(accessTokenBody.access_token); const userProfile = await fetchUserProfile(accessTokenBody.access_token); - saveUserProfile(userProfile).then(() => { - console.log("user profile saved"); - }); - console.log(userProfile); + await saveUserProfile(userProfile) setProfile(userProfile); - setLoggedIn(true); } catch (err) { console.log(err); @@ -142,32 +136,21 @@ function App() { }) } } + + async function handleLogout() { setLoading(true) setAccessToken(null); setProfile(null); - setLoggedIn(false); setActiveTaskCategory(-1) setActiveCategoryTasksState([]) await deleteAccessToken(); setLoading(false) } - useEffect(() => { - if (attemptedLogin) { - handleLogin(); - setAttemptedLogin(false); - } - }, [attemptedLogin]) - - useEffect(() => { - if (attemptedLogout) { - handleLogout(); - setAttemptedLogout(false); - } - }, [attemptedLogout]) + diff --git a/src/config/states.ts b/src/config/states.ts index 1f33221..d08ee71 100644 --- a/src/config/states.ts +++ b/src/config/states.ts @@ -3,10 +3,10 @@ import { UserProfile } from '../types/googleapis'; import { Task } from '../helpers/task'; import { task, taskCategory } from '../types/taskapi'; -const loggedInState = atom({ - key: 'loggedInState', - default: false, -}); +// const loggedInState = atom({ +// key: 'loggedInState', +// default: false, +// }); const attemptLoginState = atom({ key: 'attemptLoginState', @@ -59,8 +59,8 @@ const messageState = atom<{ title: string, body?: string , type: "info" | "warni const loggedInSelector = selector({ key: 'loggedInSelector', get: ({ get }) => { - const loggedIn = get(loggedInState); - return loggedIn; + const loggedIn = get(userProfileState); + return loggedIn && loggedIn.email != null && loggedIn.email != ""; }, }); @@ -84,7 +84,7 @@ const attemptLogoutSelector = selector({ const userProfileSelector = selector({ key: 'userSelector', get: ({ get }) => { - const loggedIn = get(loggedInState); + const loggedIn = get(loggedInSelector); const user = get(userProfileState); return loggedIn && user; }, @@ -141,8 +141,9 @@ const messageSelector = selector({ }); + export { - loggedInState, + // loggedInState, userProfileState, loggedInSelector, userProfileSelector,