From 1b206af28cafd59ec477e2875c7450d1b646ade0 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Mon, 4 Aug 2025 00:58:45 +0800 Subject: [PATCH 1/3] Open Google auth in new window --- frontend/src/main.js | 9 +--- frontend/src/router/index.js | 6 +++ frontend/src/utils/google.js | 48 +++++-------------- frontend/src/views/GoogleCallbackPageView.vue | 48 +++++++++++++++++++ frontend/src/views/LoginPageView.vue | 8 ++-- frontend/src/views/SignupPageView.vue | 6 +-- 6 files changed, 73 insertions(+), 52 deletions(-) create mode 100644 frontend/src/views/GoogleCallbackPageView.vue diff --git a/frontend/src/main.js b/frontend/src/main.js index 8bb82f72a..28cfd9659 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -8,9 +8,8 @@ import './assets/toast.css' // Use Sugar theme from vue-toast-notification for better toast styling. // If you prefer Bootstrap style, replace with theme-bootstrap.css instead. import { useToast } from 'vue-toastification' -import { checkToken, clearToken, isLogin } from './utils/auth' +import { checkToken, clearToken } from './utils/auth' import { initTheme } from './utils/theme' -import { loginWithGoogle } from './utils/google' import { clearVditorStorage } from './utils/clearVditorStorage' // Configurable API domain and port @@ -50,10 +49,4 @@ checkToken().then(valid => { if (!valid) { clearToken() } - - if (!isLogin()) { - setTimeout(() => { - loginWithGoogle() - }, 3000) - } }) diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index 6215fe757..3822c93a6 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -16,6 +16,7 @@ import NotFoundPageView from '../views/NotFoundPageView.vue' import GithubCallbackPageView from '../views/GithubCallbackPageView.vue' import DiscordCallbackPageView from '../views/DiscordCallbackPageView.vue' import TwitterCallbackPageView from '../views/TwitterCallbackPageView.vue' +import GoogleCallbackPageView from '../views/GoogleCallbackPageView.vue' import ForgotPasswordPageView from '../views/ForgotPasswordPageView.vue' const routes = [ @@ -104,6 +105,11 @@ const routes = [ name: 'twitter-callback', component: TwitterCallbackPageView }, + { + path: '/google-callback', + name: 'google-callback', + component: GoogleCallbackPageView + }, { path: '/404', name: 'not-found', diff --git a/frontend/src/utils/google.js b/frontend/src/utils/google.js index 533d6c808..fa79454b4 100644 --- a/frontend/src/utils/google.js +++ b/frontend/src/utils/google.js @@ -1,21 +1,17 @@ import { API_BASE_URL, GOOGLE_CLIENT_ID, toast } from '../main' import { setToken, loadCurrentUser } from './auth' import { registerPush } from './push' +import { WEBSITE_BASE_URL } from '../constants' -export async function googleGetIdToken() { - return new Promise((resolve, reject) => { - if (!window.google || !GOOGLE_CLIENT_ID) { - toast.error('Google 登录不可用, 请检查网络设置与VPN') - reject() - return - } - window.google.accounts.id.initialize({ - client_id: GOOGLE_CLIENT_ID, - callback: ({ credential }) => resolve(credential), - use_fedcm: true - }) - window.google.accounts.id.prompt() - }) +export function googleAuthorize() { + if (!GOOGLE_CLIENT_ID) { + toast.error('Google 登录不可用, 请检查网络设置与VPN') + return + } + const redirectUri = `${WEBSITE_BASE_URL}/google-callback` + const nonce = Math.random().toString(36).substring(2) + const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=id_token&scope=openid%20email%20profile&nonce=${nonce}` + window.open(url, '_blank', 'width=500,height=600') } export async function googleAuthWithToken(idToken, redirect_success, redirect_not_approved) { @@ -36,32 +32,10 @@ export async function googleAuthWithToken(idToken, redirect_success, redirect_no toast.info('当前为注册审核模式,请填写注册理由') if (redirect_not_approved) redirect_not_approved(data.token) } else if (data.reason_code === 'IS_APPROVING') { - toast.info('您的注册理由正在审批中') + toast.info('您的注册理由正在审批中') if (redirect_success) redirect_success() } } catch (e) { toast.error('登录失败') } -} - -export async function googleSignIn(redirect_success, redirect_not_approved) { - try { - const token = await googleGetIdToken() - await googleAuthWithToken(token, redirect_success, redirect_not_approved) - } catch { - /* ignore */ - } -} - -import router from '../router' - -export function loginWithGoogle() { - googleSignIn( - () => { - router.push('/') - }, - token => { - router.push('/signup-reason?token=' + token) - } - ) } \ No newline at end of file diff --git a/frontend/src/views/GoogleCallbackPageView.vue b/frontend/src/views/GoogleCallbackPageView.vue new file mode 100644 index 000000000..520a8e04e --- /dev/null +++ b/frontend/src/views/GoogleCallbackPageView.vue @@ -0,0 +1,48 @@ + + + + + diff --git a/frontend/src/views/LoginPageView.vue b/frontend/src/views/LoginPageView.vue index cba93fb48..4a33fb508 100644 --- a/frontend/src/views/LoginPageView.vue +++ b/frontend/src/views/LoginPageView.vue @@ -31,7 +31,7 @@
-
+ @@ -54,7 +54,7 @@