Skip to content

Commit c9c1a34

Browse files
hyriousBlackHole1
andauthored
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 <[email protected]> * chore: compact code Co-authored-by: Black-Hole <[email protected]>
1 parent c2088b0 commit c9c1a34

File tree

23 files changed

+215
-26
lines changed

23 files changed

+215
-26
lines changed

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
pnpm check-spelling
54
pnpm lint

config/America/.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ AGORA_APP_ID=931b86d6781e49a2a255db4ce6e8e804
33
GITHUB_CLIENT_ID=0ac608815326aead5db7
44
WECHAT_APP_ID=wx1133c2153a45e9b8
55
AGORA_OAUTH_CLIENT_ID=flat-dev
6+
GOOGLE_OAUTH_CLIENT_ID=273996094508-p97og69ojac5ja0khn1rvmi3tb7vgfgm.apps.googleusercontent.com
67

78
CLOUD_STORAGE_OSS_ALIBABA_ACCESS_KEY=LTAI5t9Gb6tzQzzLmB6cTVf7
89
CLOUD_STORAGE_OSS_ALIBABA_BUCKET=flat-storage

config/America/.env.production

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ AGORA_APP_ID=931b86d6781e49a2a255db4ce6e8e804
33
GITHUB_CLIENT_ID=da83d7e14217594fba46
44
WECHAT_APP_ID=wx96d522d69d384cce
55
AGORA_OAUTH_CLIENT_ID=flat
6+
GOOGLE_OAUTH_CLIENT_ID=273996094508-2rpraucen77a1o5dul5ftrua5k3og157.apps.googleusercontent.com
67

78
CLOUD_STORAGE_OSS_ALIBABA_ACCESS_KEY=LTAI5t9Gb6tzQzzLmB6cTVf7
89
CLOUD_STORAGE_OSS_ALIBABA_BUCKET=flat-storage

config/China/.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ AGORA_APP_ID=931b86d6781e49a2a255db4ce6e8e804
33
GITHUB_CLIENT_ID=9821657775fbc74773f1
44
WECHAT_APP_ID=wx1133c2153a45e9b8
55
AGORA_OAUTH_CLIENT_ID=flat-dev
6+
GOOGLE_OAUTH_CLIENT_ID=273996094508-p97og69ojac5ja0khn1rvmi3tb7vgfgm.apps.googleusercontent.com
67

78
CLOUD_STORAGE_OSS_ALIBABA_ACCESS_KEY=LTAI5t9Gb6tzQzzLmB6cTVf7
89
CLOUD_STORAGE_OSS_ALIBABA_BUCKET=flat-storage

config/China/.env.production

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ AGORA_APP_ID=931b86d6781e49a2a255db4ce6e8e804
33
GITHUB_CLIENT_ID=71a29285a437998bdfe0
44
WECHAT_APP_ID=wx96d522d69d384cce
55
AGORA_OAUTH_CLIENT_ID=flat
6+
GOOGLE_OAUTH_CLIENT_ID=273996094508-2rpraucen77a1o5dul5ftrua5k3og157.apps.googleusercontent.com
67

78
CLOUD_STORAGE_OSS_ALIBABA_ACCESS_KEY=LTAI5t9Gb6tzQzzLmB6cTVf7
89
CLOUD_STORAGE_OSS_ALIBABA_BUCKET=flat-storage

desktop/renderer-app/src/api-middleware/flatServer/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const FLAT_SERVER_VERSIONS = {
88

99
export const FLAT_SERVER_LOGIN = {
1010
GITHUB_CALLBACK: `${FLAT_SERVER_VERSIONS.V1}/login/github/callback`,
11+
GOOGLE_CALLBACK: `${FLAT_SERVER_VERSIONS.V1}/login/google/callback`,
1112
WECHAT_CALLBACK: `${FLAT_SERVER_VERSIONS.V1}/login/weChat/web/callback`,
1213
} as const;
1314

desktop/renderer-app/src/constants/process.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export const GITHUB = Object.freeze({
2424
CLIENT_ID: process.env.GITHUB_CLIENT_ID,
2525
});
2626

27+
export const GOOGLE = Object.freeze({
28+
CLIENT_ID: process.env.GOOGLE_OAUTH_CLIENT_ID,
29+
});
30+
2731
export const FLAT_SERVER_DOMAIN = process.env.FLAT_SERVER_DOMAIN;
2832
export const FLAT_WEB_DOMAIN = process.env.FLAT_WEB_DOMAIN;
2933

desktop/renderer-app/src/pages/HomePage/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ export const HomePage = observer<HomePageProps>(function HomePage() {
7676

7777
try {
7878
await loginCheck();
79-
globalStore.lastLoginCheck = Date.now();
79+
globalStore.updateLastLoginCheck(Date.now());
8080
return true;
8181
} catch (e) {
82-
globalStore.lastLoginCheck = null;
82+
globalStore.updateLastLoginCheck(null);
8383
console.error(e);
8484
errorTips(e as Error);
8585
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { setAuthUUID, loginProcess } from "../../api-middleware/flatServer";
2+
import { v4 as uuidv4 } from "uuid";
3+
import { LoginExecutor } from "./utils";
4+
import { shell } from "electron";
5+
import { errorTips } from "../../components/Tips/ErrorTips";
6+
import { FLAT_SERVER_LOGIN } from "../../api-middleware/flatServer/constants";
7+
import { GOOGLE } from "../../constants/process";
8+
9+
// @TODO: migrate to new google login api before 2023. @hyrious
10+
// https://developers.google.com/identity/gsi/web
11+
export const googleLogin: LoginExecutor = onSuccess => {
12+
let timer = NaN;
13+
const authUUID = uuidv4();
14+
const scopes = ["openid", "https://www.googleapis.com/auth/userinfo.profile"];
15+
16+
function getGoogleURL(authUUID: string): string {
17+
const redirectURL = encodeURIComponent(FLAT_SERVER_LOGIN.GOOGLE_CALLBACK);
18+
const scope = encodeURIComponent(scopes.join(" "));
19+
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}`;
20+
}
21+
22+
void (async () => {
23+
try {
24+
await setAuthUUID(authUUID);
25+
} catch (err) {
26+
errorTips(err);
27+
return;
28+
}
29+
30+
void shell.openExternal(getGoogleURL(authUUID));
31+
32+
const googleLoginProcessRequest = async (): Promise<void> => {
33+
try {
34+
const data = await loginProcess(authUUID);
35+
36+
if (!data.name) {
37+
timer = window.setTimeout(googleLoginProcessRequest, 2000);
38+
return;
39+
}
40+
41+
onSuccess(data);
42+
} catch (err) {
43+
errorTips(err);
44+
}
45+
};
46+
47+
void googleLoginProcessRequest();
48+
})();
49+
50+
return () => {
51+
window.clearTimeout(timer);
52+
};
53+
};

desktop/renderer-app/src/pages/LoginPage/index.tsx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ import "./index.less";
22

33
import React, { useCallback, useContext, useEffect, useRef, useState } from "react";
44
import { constants } from "flat-types";
5+
import { useTranslation } from "react-i18next";
56
import { observer } from "mobx-react-lite";
6-
import { ipcAsyncByMainWindow, ipcSyncByApp } from "../../utils/ipc";
7+
import { message } from "antd";
78
import { LoginPanel, LoginButton, LoginButtonProviderType } from "flat-components";
8-
import { LoginDisposer } from "./utils";
99
import { githubLogin } from "./githubLogin";
10+
import WeChatLogin from "./WeChatLogin";
11+
import { googleLogin } from "./googleLogin";
12+
import { ipcAsyncByMainWindow, ipcSyncByApp } from "../../utils/ipc";
13+
import { LoginDisposer } from "./utils";
1014
import { RouteNameType, usePushHistory } from "../../utils/routes";
1115
import { GlobalStoreContext } from "../../components/StoreProvider";
1216
import { AppUpgradeModal, AppUpgradeModalProps } from "../../components/AppUpgradeModal";
1317
import { runtime } from "../../utils/runtime";
1418
import { useSafePromise } from "../../utils/hooks/lifecycle";
15-
import { useTranslation } from "react-i18next";
1619
import {
1720
PRIVACY_URL_EN,
1821
PRIVACY_URL_CN,
1922
SERVICE_URL_EN,
2023
SERVICE_URL_CN,
2124
} from "../../constants/process";
22-
import { message } from "antd";
23-
import WeChatLogin from "./WeChatLogin";
2425

2526
export const LoginPage = observer(function LoginPage() {
2627
const { i18n } = useTranslation();
@@ -81,6 +82,13 @@ export const LoginPage = observer(function LoginPage() {
8182
});
8283
return;
8384
}
85+
case "google": {
86+
loginDisposer.current = googleLogin(async authData => {
87+
globalStore.updateUserInfo(authData);
88+
pushHistory(RouteNameType.HomePage);
89+
});
90+
return;
91+
}
8492
case "wechat": {
8593
setWeChatLogin(true);
8694
return;
@@ -105,11 +113,19 @@ export const LoginPage = observer(function LoginPage() {
105113
function renderButtonList(): React.ReactNode {
106114
return (
107115
<>
108-
<LoginButton
109-
provider="wechat"
110-
text={i18n.t("login-wechat")}
111-
onLogin={handleLogin}
112-
/>
116+
{process.env.FLAT_REGION === "America" ? (
117+
<LoginButton
118+
provider="google"
119+
text={i18n.t("login-google")}
120+
onLogin={handleLogin}
121+
/>
122+
) : (
123+
<LoginButton
124+
provider="wechat"
125+
text={i18n.t("login-wechat")}
126+
onLogin={handleLogin}
127+
/>
128+
)}
113129
<LoginButton
114130
provider="github"
115131
text={i18n.t("login-github")}

0 commit comments

Comments
 (0)