-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
42 lines (38 loc) · 1.16 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function checkLoginStatus() {
chrome.runtime.sendMessage({ type: "LOGIN" }, (response) => {
if(response.success) {
console.log("User logged in.");
switchScreen("main");
} else {
console.log("User not logged in.");
switchScreen("login");
}
});
}
function loginUser() {
chrome.runtime.sendMessage({ type: "LOGIN" }, (response) => {
if(response.success) {
localStorage.setItem("access_token", response.token);
switchScreen("main");
} else {
console.log("Login failed.");
}
});
}
function logoutUser() {
chrome.identity.getAuthToken({ interactive: false }, (token) => {
if(chrome.runtime.lastError || !token) {
finishLogout();
return;
}
chrome.identity.removeCachedAuthToken({ token }, () => {
fetch(`https://accounts.google.com/o/oauth2/revoke?token=${token}`)
.then(() => finishLogout())
.catch(() => finishLogout());
});
});
}
function finishLogout() {
localStorage.removeItem("access_token");
switchScreen("login");
}