From 9227411d7435f5b178b015aa35747ce31dec4f48 Mon Sep 17 00:00:00 2001 From: "web@ppanel" Date: Wed, 22 Jan 2025 22:16:04 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(oauth):=20Update=20OAuth=20log?= =?UTF-8?q?in=20handling=20to=20use=20callback=20parameter=20and=20improve?= =?UTF-8?q?=20URL=20parameter=20retrieval?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/services/common/typings.d.ts | 2 +- .../user/app/oauth/[platform]/certification.tsx | 11 ++++++----- apps/user/services/common/typings.d.ts | 2 +- apps/user/utils/common.ts | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/admin/services/common/typings.d.ts b/apps/admin/services/common/typings.d.ts index f66eaac..3fe8eb3 100644 --- a/apps/admin/services/common/typings.d.ts +++ b/apps/admin/services/common/typings.d.ts @@ -234,7 +234,7 @@ declare namespace API { type OAuthLoginGetTokenRequest = { /** google, facebook, apple, telegram, github etc. */ method: string; - code: string; + callback: Record; }; type OAuthLoginResponse = { diff --git a/apps/user/app/oauth/[platform]/certification.tsx b/apps/user/app/oauth/[platform]/certification.tsx index b01986c..5bc0748 100644 --- a/apps/user/app/oauth/[platform]/certification.tsx +++ b/apps/user/app/oauth/[platform]/certification.tsx @@ -1,8 +1,8 @@ 'use client'; import { oAuthLoginGetToken } from '@/services/common/oauth'; -import { getRedirectUrl, setAuthorization } from '@/utils/common'; -import { useRouter, useSearchParams } from 'next/navigation'; +import { getAllUrlParams, getRedirectUrl, setAuthorization } from '@/utils/common'; +import { usePathname, useRouter } from 'next/navigation'; import { useEffect } from 'react'; interface CertificationProps { @@ -11,13 +11,14 @@ interface CertificationProps { } export default function Certification({ platform, children }: CertificationProps) { - const searchParams = useSearchParams(); const router = useRouter(); + const pathname = usePathname(); useEffect(() => { + const searchParams = getAllUrlParams(); oAuthLoginGetToken({ method: platform, - code: searchParams.get('code') || '', + callback: searchParams, }) .then((res) => { const token = res?.data?.data?.token; @@ -31,7 +32,7 @@ export default function Certification({ platform, children }: CertificationProps .catch((error) => { router.replace('/auth'); }); - }, [platform, searchParams.values()]); + }, [pathname]); return children; } diff --git a/apps/user/services/common/typings.d.ts b/apps/user/services/common/typings.d.ts index f66eaac..3fe8eb3 100644 --- a/apps/user/services/common/typings.d.ts +++ b/apps/user/services/common/typings.d.ts @@ -234,7 +234,7 @@ declare namespace API { type OAuthLoginGetTokenRequest = { /** google, facebook, apple, telegram, github etc. */ method: string; - code: string; + callback: Record; }; type OAuthLoginResponse = { diff --git a/apps/user/utils/common.ts b/apps/user/utils/common.ts index ec7fc6d..afb7d1a 100644 --- a/apps/user/utils/common.ts +++ b/apps/user/utils/common.ts @@ -75,3 +75,20 @@ export function getPlatform(): 'windows' | 'mac' | 'linux' | 'android' | 'ios' | return 'windows'; } + +export function getAllUrlParams() { + const searchParams = new URLSearchParams(window.location.search); + const hashParams = new URLSearchParams(window.location.hash.substring(1)); + + const params: { [key: string]: string } = {}; + + for (const [key, value] of searchParams.entries()) { + params[key] = value; + } + + for (const [key, value] of hashParams.entries()) { + params[key] = value; + } + + return params; +}