-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(login): support google login (#1465)
* feat(flat-web): google login * feat(desktop): google login * chore: remove unused code * chore: remove spell check since it already exist in lint * Update desktop/renderer-app/src/pages/LoginPage/googleLogin.ts Co-authored-by: Black-Hole <158blackhole@gmail.com> * chore: compact code Co-authored-by: Black-Hole <158blackhole@gmail.com>
- Loading branch information
1 parent
c2088b0
commit c9c1a34
Showing
23 changed files
with
215 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#!/bin/sh | ||
. "$(dirname "$0")/_/husky.sh" | ||
|
||
pnpm check-spelling | ||
pnpm lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { setAuthUUID, loginProcess } from "../../api-middleware/flatServer"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { LoginExecutor } from "./utils"; | ||
import { shell } from "electron"; | ||
import { errorTips } from "../../components/Tips/ErrorTips"; | ||
import { FLAT_SERVER_LOGIN } from "../../api-middleware/flatServer/constants"; | ||
import { GOOGLE } from "../../constants/process"; | ||
|
||
// @TODO: migrate to new google login api before 2023. @hyrious | ||
// https://developers.google.com/identity/gsi/web | ||
export const googleLogin: LoginExecutor = onSuccess => { | ||
let timer = NaN; | ||
const authUUID = uuidv4(); | ||
const scopes = ["openid", "https://www.googleapis.com/auth/userinfo.profile"]; | ||
|
||
function getGoogleURL(authUUID: string): string { | ||
const redirectURL = encodeURIComponent(FLAT_SERVER_LOGIN.GOOGLE_CALLBACK); | ||
const scope = encodeURIComponent(scopes.join(" ")); | ||
return `https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=online&scope=${scope}&client_id=${GOOGLE.CLIENT_ID}&redirect_uri=${redirectURL}&state=${authUUID}`; | ||
} | ||
|
||
void (async () => { | ||
try { | ||
await setAuthUUID(authUUID); | ||
} catch (err) { | ||
errorTips(err); | ||
return; | ||
} | ||
|
||
void shell.openExternal(getGoogleURL(authUUID)); | ||
|
||
const googleLoginProcessRequest = async (): Promise<void> => { | ||
try { | ||
const data = await loginProcess(authUUID); | ||
|
||
if (!data.name) { | ||
timer = window.setTimeout(googleLoginProcessRequest, 2000); | ||
return; | ||
} | ||
|
||
onSuccess(data); | ||
} catch (err) { | ||
errorTips(err); | ||
} | ||
}; | ||
|
||
void googleLoginProcessRequest(); | ||
})(); | ||
|
||
return () => { | ||
window.clearTimeout(timer); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { setAuthUUID, loginProcess } from "../../api-middleware/flatServer"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { LoginExecutor } from "./utils"; | ||
import { errorTips } from "../../components/Tips/ErrorTips"; | ||
import { FLAT_SERVER_LOGIN } from "../../api-middleware/flatServer/constants"; | ||
import { GOOGLE } from "../../constants/process"; | ||
|
||
// @TODO: migrate to new google login api before 2023 | ||
// https://developers.google.com/identity/gsi/web | ||
export const googleLogin: LoginExecutor = onSuccess => { | ||
let timer = NaN; | ||
const authUUID = uuidv4(); | ||
const scopes = ["openid", "https://www.googleapis.com/auth/userinfo.profile"]; | ||
|
||
function getGoogleURL(authUUID: string): string { | ||
const redirectURL = encodeURIComponent(FLAT_SERVER_LOGIN.GOOGLE_CALLBACK); | ||
const scope = encodeURIComponent(scopes.join(" ")); | ||
return `https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=online&scope=${scope}&client_id=${GOOGLE.CLIENT_ID}&redirect_uri=${redirectURL}&state=${authUUID}`; | ||
} | ||
|
||
void (async () => { | ||
try { | ||
await setAuthUUID(authUUID); | ||
} catch (err) { | ||
errorTips(err); | ||
return; | ||
} | ||
|
||
void window.open(getGoogleURL(authUUID)); | ||
|
||
const googleLoginProcessRequest = async (): Promise<void> => { | ||
try { | ||
const data = await loginProcess(authUUID); | ||
|
||
if (!data.name) { | ||
timer = window.setTimeout(googleLoginProcessRequest, 2000); | ||
return; | ||
} | ||
|
||
onSuccess(data); | ||
} catch (err) { | ||
errorTips(err); | ||
} | ||
}; | ||
|
||
void googleLoginProcessRequest(); | ||
})(); | ||
|
||
return () => { | ||
window.clearTimeout(timer); | ||
}; | ||
}; |
Oops, something went wrong.