Skip to content

Commit 7249224

Browse files
authored
Merge pull request #1 from codad5/dev
Fix login and refactor code
2 parents d4f4582 + 689c041 commit 7249224

File tree

6 files changed

+55
-72
lines changed

6 files changed

+55
-72
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "google-task",
33
"private": true,
4-
"version": "0.0.5",
4+
"version": "0.0.6",
55
"type": "module",
66
"scripts": {
77
"dev": "vite",

password.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src-tauri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ borsh = { version = "1.2.0", features = ["std", "borsh-derive", "derive", "bson"
2222
cocoon = "0.4.1"
2323
tauri-plugin-oauth = { git = "https://github.com/FabianLars/tauri-plugin-oauth", branch = "main" }
2424
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
25-
tauri-plugin-context-menu = { git = "https://github.com/c2r0b/tauri-plugin-context-menu", branch = "main" }
25+
tauri-plugin-context-menu = { version = "0.6.1" }
2626
dotenv = "0.15.0"
2727
lazy_static = "1.4.0"
2828

src/App.tsx

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { fetchUserProfile,getUserProfileFromStorage, getAccessToken, openAuthWi
44
import { AccessToken, UserProfile } from "./types/googleapis";
55
import { loadContextmenu , pushNotification } from "./helpers/windowhelper";
66
import TaskPage from "./components/TaskPage";
7-
import { useRecoilState, useSetRecoilState } from "recoil";
8-
import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, attemptLoginState, attemptLogoutState, loggedInState, messageState, userProfileState } from "./config/states";
7+
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
8+
import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, attemptLoginState, attemptLogoutState, loggedInSelector, messageState, userProfileState } from "./config/states";
99
import Header from "./components/ui/Header";
1010
import { task } from "./types/taskapi";
1111
import { listen_for_auth_code } from "./helpers/eventlistner";
@@ -15,7 +15,7 @@ loadContextmenu();
1515

1616
function App() {
1717
const [loading, setLoading] = useState<boolean>(false);
18-
const [loggedIn, setLoggedIn] = useRecoilState<boolean>(loggedInState);
18+
const loggedIn = useRecoilValue(loggedInSelector);
1919
const setProfile = useSetRecoilState<UserProfile | null>(userProfileState);
2020
const setAccessToken = useSetRecoilState<string | null>(accessTokenState);
2121
const [attemptedLogin, setAttemptedLogin] = useRecoilState<boolean>(attemptLoginState);
@@ -44,6 +44,20 @@ function App() {
4444
}, [toastMessage])
4545

4646

47+
useEffect(() => {
48+
if (attemptedLogin) {
49+
handleLogin();
50+
setAttemptedLogin(false);
51+
}
52+
}, [attemptedLogin])
53+
54+
useEffect(() => {
55+
if (attemptedLogout) {
56+
handleLogout();
57+
setAttemptedLogout(false);
58+
}
59+
}, [attemptedLogout])
60+
4761

4862

4963
// to generate a port and listen to it
@@ -76,60 +90,40 @@ function App() {
7690
// check the offline data for access token
7791
useEffect(() => {
7892
setLoading(true)
79-
// get access token from storage
80-
getAccessTokenFromStorage().then((accessToken) => {
81-
try {
82-
if (!accessToken) throw new Error("Signin required");
83-
pushNotification("Login Successful")
84-
if (navigator.onLine) {
85-
// fetch user profile
86-
fetchUserProfile(accessToken.access_token).then((profile) => {
87-
if(!profile) throw new Error("Something went wrong, please try again");
88-
setProfile(profile);
89-
setAccessToken(accessToken.access_token);
90-
setLoggedIn(true);
91-
setLoading(false)
92-
pushNotification(`welcome back ${profile.name}`)
93-
});
94-
}
95-
else {
96-
getUserProfileFromStorage().then((profile) => {
97-
if (!profile) throw new Error("Signin required");
98-
setProfile(profile);
99-
setAccessToken(accessToken.access_token);
100-
setLoggedIn(true);
101-
pushNotification(`welcome back ${profile.name}`)
102-
});
103-
}
104-
}catch (err) {
105-
console.log(err);
106-
setLoading(false)
107-
setToastMessage({
108-
title: "Error",
109-
body: "Error signing in",
110-
type: "error"
111-
})
112-
}
93+
handleInitialLogin ().catch((err) => {
94+
console.log(err);
95+
setLoading(false)
96+
setToastMessage({
97+
title: "Error",
98+
body: "Error signing in",
99+
type: "error"
100+
})
113101
}).finally(() => {
114102
setLoading(false)
115103
})
116104
}, [])
117105

118106

107+
async function handleInitialLogin() {
108+
// get access token from storage
109+
const accessToken = await getAccessTokenFromStorage();
110+
if (!accessToken) throw new Error("Signin required");
111+
pushNotification("Login Successful")
112+
const profile = navigator.onLine ? await fetchUserProfile(accessToken.access_token) : await getUserProfileFromStorage();
113+
if(!profile) throw new Error("Something went wrong, please try again");
114+
setProfile(profile);
115+
setAccessToken(accessToken.access_token);
116+
pushNotification(`welcome back ${profile.name}`)
117+
}
118+
119119

120120
async function handleLoadFrom(accessTokenBody: AccessToken) {
121121
try {
122-
saveAccessToken(JSON.stringify(accessTokenBody, null, 2)).then(() => {
123-
console.log("access token saved");
124-
});
122+
await saveAccessToken(JSON.stringify(accessTokenBody, null, 2))
125123
setAccessToken(accessTokenBody.access_token);
126124
const userProfile = await fetchUserProfile(accessTokenBody.access_token);
127-
saveUserProfile(userProfile).then(() => {
128-
console.log("user profile saved");
129-
});
130-
console.log(userProfile);
125+
await saveUserProfile(userProfile)
131126
setProfile(userProfile);
132-
setLoggedIn(true);
133127
}
134128
catch (err) {
135129
console.log(err);
@@ -142,32 +136,21 @@ function App() {
142136
})
143137
}
144138
}
139+
140+
145141

146142

147143
async function handleLogout() {
148144
setLoading(true)
149145
setAccessToken(null);
150146
setProfile(null);
151-
setLoggedIn(false);
152147
setActiveTaskCategory(-1)
153148
setActiveCategoryTasksState([])
154149
await deleteAccessToken();
155150
setLoading(false)
156151
}
157152

158-
useEffect(() => {
159-
if (attemptedLogin) {
160-
handleLogin();
161-
setAttemptedLogin(false);
162-
}
163-
}, [attemptedLogin])
164-
165-
useEffect(() => {
166-
if (attemptedLogout) {
167-
handleLogout();
168-
setAttemptedLogout(false);
169-
}
170-
}, [attemptedLogout])
153+
171154

172155

173156

src/config/states.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { UserProfile } from '../types/googleapis';
33
import { Task } from '../helpers/task';
44
import { task, taskCategory } from '../types/taskapi';
55

6-
const loggedInState = atom({
7-
key: 'loggedInState',
8-
default: false,
9-
});
6+
// const loggedInState = atom({
7+
// key: 'loggedInState',
8+
// default: false,
9+
// });
1010

1111
const attemptLoginState = atom({
1212
key: 'attemptLoginState',
@@ -59,8 +59,8 @@ const messageState = atom<{ title: string, body?: string , type: "info" | "warni
5959
const loggedInSelector = selector({
6060
key: 'loggedInSelector',
6161
get: ({ get }) => {
62-
const loggedIn = get(loggedInState);
63-
return loggedIn;
62+
const loggedIn = get(userProfileState);
63+
return loggedIn && loggedIn.email != null && loggedIn.email != "";
6464
},
6565
});
6666

@@ -84,7 +84,7 @@ const attemptLogoutSelector = selector({
8484
const userProfileSelector = selector({
8585
key: 'userSelector',
8686
get: ({ get }) => {
87-
const loggedIn = get(loggedInState);
87+
const loggedIn = get(loggedInSelector);
8888
const user = get(userProfileState);
8989
return loggedIn && user;
9090
},
@@ -141,8 +141,9 @@ const messageSelector = selector({
141141
});
142142

143143

144+
144145
export {
145-
loggedInState,
146+
// loggedInState,
146147
userProfileState,
147148
loggedInSelector,
148149
userProfileSelector,

0 commit comments

Comments
 (0)