From 40a6f7ca81fa0596d86b6043d59630c6fd959590 Mon Sep 17 00:00:00 2001 From: "web@ppanel" Date: Tue, 21 Jan 2025 23:32:12 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(oauth):=20Implement=20OAuth=20?= =?UTF-8?q?token=20retrieval=20and=20refactor=20login=20callback=20handlin?= =?UTF-8?q?g?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/services/common/oauth.ts | 15 +++++++++ apps/admin/services/common/typings.d.ts | 6 ++++ .../app/oauth/[platform]/certification.tsx | 31 +++++-------------- apps/user/services/common/oauth.ts | 16 ++++++++++ apps/user/services/common/typings.d.ts | 6 ++++ 5 files changed, 51 insertions(+), 23 deletions(-) diff --git a/apps/admin/services/common/oauth.ts b/apps/admin/services/common/oauth.ts index 44a816e..a298049 100644 --- a/apps/admin/services/common/oauth.ts +++ b/apps/admin/services/common/oauth.ts @@ -79,3 +79,18 @@ export async function oAuthLogin(body: API.OAthLoginRequest, options?: { [key: s ...(options || {}), }); } + +/** OAuth login get token POST /v1/auth/oauth/login/token */ +export async function oAuthLoginGetToken( + body: API.OAuthLoginGetTokenRequest, + options?: { [key: string]: any }, +) { + return request('/v1/auth/oauth/login/token', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + data: body, + ...(options || {}), + }); +} diff --git a/apps/admin/services/common/typings.d.ts b/apps/admin/services/common/typings.d.ts index 751d088..f66eaac 100644 --- a/apps/admin/services/common/typings.d.ts +++ b/apps/admin/services/common/typings.d.ts @@ -231,6 +231,12 @@ declare namespace API { redirect: string; }; + type OAuthLoginGetTokenRequest = { + /** google, facebook, apple, telegram, github etc. */ + method: string; + code: string; + }; + type OAuthLoginResponse = { redirect: string; }; diff --git a/apps/user/app/oauth/[platform]/certification.tsx b/apps/user/app/oauth/[platform]/certification.tsx index 4fdc4c0..b01986c 100644 --- a/apps/user/app/oauth/[platform]/certification.tsx +++ b/apps/user/app/oauth/[platform]/certification.tsx @@ -1,11 +1,6 @@ 'use client'; -import { - appleLoginCallback, - facebookLoginCallback, - googleLoginCallback, - telegramLoginCallback, -} from '@/services/common/oauth'; +import { oAuthLoginGetToken } from '@/services/common/oauth'; import { getRedirectUrl, setAuthorization } from '@/utils/common'; import { useRouter, useSearchParams } from 'next/navigation'; import { useEffect } from 'react'; @@ -19,26 +14,16 @@ export default function Certification({ platform, children }: CertificationProps const searchParams = useSearchParams(); const router = useRouter(); - async function LoginCallback() { - const body = Object.fromEntries(searchParams.entries()) as any; - switch (platform) { - case 'apple': - return appleLoginCallback(body); - case 'facebook': - return facebookLoginCallback(body); - case 'google': - return googleLoginCallback(body); - case 'telegram': - // 回调参数是 # 开头的,需要去掉 - return telegramLoginCallback(body); - default: - break; - } - } useEffect(() => { - LoginCallback() + oAuthLoginGetToken({ + method: platform, + code: searchParams.get('code') || '', + }) .then((res) => { const token = res?.data?.data?.token; + if (!token) { + throw new Error('Invalid token'); + } setAuthorization(token); router.replace(getRedirectUrl()); router.refresh(); diff --git a/apps/user/services/common/oauth.ts b/apps/user/services/common/oauth.ts index 5e42f5d..a298049 100644 --- a/apps/user/services/common/oauth.ts +++ b/apps/user/services/common/oauth.ts @@ -32,6 +32,7 @@ export async function appleLoginCallback( return request('/v1/auth/oauth/callback/apple', { method: 'POST', data: formData, + requestType: 'form', ...(options || {}), }); } @@ -78,3 +79,18 @@ export async function oAuthLogin(body: API.OAthLoginRequest, options?: { [key: s ...(options || {}), }); } + +/** OAuth login get token POST /v1/auth/oauth/login/token */ +export async function oAuthLoginGetToken( + body: API.OAuthLoginGetTokenRequest, + options?: { [key: string]: any }, +) { + return request('/v1/auth/oauth/login/token', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + data: body, + ...(options || {}), + }); +} diff --git a/apps/user/services/common/typings.d.ts b/apps/user/services/common/typings.d.ts index 751d088..f66eaac 100644 --- a/apps/user/services/common/typings.d.ts +++ b/apps/user/services/common/typings.d.ts @@ -231,6 +231,12 @@ declare namespace API { redirect: string; }; + type OAuthLoginGetTokenRequest = { + /** google, facebook, apple, telegram, github etc. */ + method: string; + code: string; + }; + type OAuthLoginResponse = { redirect: string; };