feat(oauth): Update OAuth login handling to use callback parameter and improve URL parameter retrieval

This commit is contained in:
web@ppanel 2025-01-22 22:16:04 +07:00
parent 0aa5d5b0a9
commit 9227411d74
4 changed files with 25 additions and 7 deletions

View File

@ -234,7 +234,7 @@ declare namespace API {
type OAuthLoginGetTokenRequest = {
/** google, facebook, apple, telegram, github etc. */
method: string;
code: string;
callback: Record<string, any>;
};
type OAuthLoginResponse = {

View File

@ -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;
}

View File

@ -234,7 +234,7 @@ declare namespace API {
type OAuthLoginGetTokenRequest = {
/** google, facebook, apple, telegram, github etc. */
method: string;
code: string;
callback: Record<string, any>;
};
type OAuthLoginResponse = {

View File

@ -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;
}