Skip to content

Commit

Permalink
Merge pull request #1 from codad5/dev
Browse files Browse the repository at this point in the history
Fix login and refactor code
  • Loading branch information
codad5 committed Dec 13, 2023
2 parents d4f4582 + 689c041 commit 7249224
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 72 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "google-task",
"private": true,
"version": "0.0.5",
"version": "0.0.6",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
1 change: 0 additions & 1 deletion password.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
101 changes: 42 additions & 59 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -15,7 +15,7 @@ loadContextmenu();

function App() {
const [loading, setLoading] = useState<boolean>(false);
const [loggedIn, setLoggedIn] = useRecoilState<boolean>(loggedInState);
const loggedIn = useRecoilValue(loggedInSelector);
const setProfile = useSetRecoilState<UserProfile | null>(userProfileState);
const setAccessToken = useSetRecoilState<string | null>(accessTokenState);
const [attemptedLogin, setAttemptedLogin] = useRecoilState<boolean>(attemptLoginState);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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])




Expand Down
17 changes: 9 additions & 8 deletions src/config/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 != "";
},
});

Expand All @@ -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;
},
Expand Down Expand Up @@ -141,8 +141,9 @@ const messageSelector = selector({
});



export {
loggedInState,
// loggedInState,
userProfileState,
loggedInSelector,
userProfileSelector,
Expand Down

0 comments on commit 7249224

Please sign in to comment.