✨ feat(oauth): Implement OAuth token retrieval and refactor login callback handling
This commit is contained in:
parent
8346c85109
commit
40a6f7ca81
@ -79,3 +79,18 @@ export async function oAuthLogin(body: API.OAthLoginRequest, options?: { [key: s
|
|||||||
...(options || {}),
|
...(options || {}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** OAuth login get token POST /v1/auth/oauth/login/token */
|
||||||
|
export async function oAuthLoginGetToken(
|
||||||
|
body: API.OAuthLoginGetTokenRequest,
|
||||||
|
options?: { [key: string]: any },
|
||||||
|
) {
|
||||||
|
return request<API.Response & { data?: API.LoginResponse }>('/v1/auth/oauth/login/token', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
data: body,
|
||||||
|
...(options || {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
6
apps/admin/services/common/typings.d.ts
vendored
6
apps/admin/services/common/typings.d.ts
vendored
@ -231,6 +231,12 @@ declare namespace API {
|
|||||||
redirect: string;
|
redirect: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type OAuthLoginGetTokenRequest = {
|
||||||
|
/** google, facebook, apple, telegram, github etc. */
|
||||||
|
method: string;
|
||||||
|
code: string;
|
||||||
|
};
|
||||||
|
|
||||||
type OAuthLoginResponse = {
|
type OAuthLoginResponse = {
|
||||||
redirect: string;
|
redirect: string;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import {
|
import { oAuthLoginGetToken } from '@/services/common/oauth';
|
||||||
appleLoginCallback,
|
|
||||||
facebookLoginCallback,
|
|
||||||
googleLoginCallback,
|
|
||||||
telegramLoginCallback,
|
|
||||||
} from '@/services/common/oauth';
|
|
||||||
import { getRedirectUrl, setAuthorization } from '@/utils/common';
|
import { getRedirectUrl, setAuthorization } from '@/utils/common';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
@ -19,26 +14,16 @@ export default function Certification({ platform, children }: CertificationProps
|
|||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
const router = useRouter();
|
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(() => {
|
useEffect(() => {
|
||||||
LoginCallback()
|
oAuthLoginGetToken({
|
||||||
|
method: platform,
|
||||||
|
code: searchParams.get('code') || '',
|
||||||
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
const token = res?.data?.data?.token;
|
const token = res?.data?.data?.token;
|
||||||
|
if (!token) {
|
||||||
|
throw new Error('Invalid token');
|
||||||
|
}
|
||||||
setAuthorization(token);
|
setAuthorization(token);
|
||||||
router.replace(getRedirectUrl());
|
router.replace(getRedirectUrl());
|
||||||
router.refresh();
|
router.refresh();
|
||||||
|
|||||||
@ -32,6 +32,7 @@ export async function appleLoginCallback(
|
|||||||
return request<API.Response & { data?: any }>('/v1/auth/oauth/callback/apple', {
|
return request<API.Response & { data?: any }>('/v1/auth/oauth/callback/apple', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
data: formData,
|
data: formData,
|
||||||
|
requestType: 'form',
|
||||||
...(options || {}),
|
...(options || {}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -78,3 +79,18 @@ export async function oAuthLogin(body: API.OAthLoginRequest, options?: { [key: s
|
|||||||
...(options || {}),
|
...(options || {}),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** OAuth login get token POST /v1/auth/oauth/login/token */
|
||||||
|
export async function oAuthLoginGetToken(
|
||||||
|
body: API.OAuthLoginGetTokenRequest,
|
||||||
|
options?: { [key: string]: any },
|
||||||
|
) {
|
||||||
|
return request<API.Response & { data?: API.LoginResponse }>('/v1/auth/oauth/login/token', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
data: body,
|
||||||
|
...(options || {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
6
apps/user/services/common/typings.d.ts
vendored
6
apps/user/services/common/typings.d.ts
vendored
@ -231,6 +231,12 @@ declare namespace API {
|
|||||||
redirect: string;
|
redirect: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type OAuthLoginGetTokenRequest = {
|
||||||
|
/** google, facebook, apple, telegram, github etc. */
|
||||||
|
method: string;
|
||||||
|
code: string;
|
||||||
|
};
|
||||||
|
|
||||||
type OAuthLoginResponse = {
|
type OAuthLoginResponse = {
|
||||||
redirect: string;
|
redirect: string;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user