👔 add oauth callback handler
This commit is contained in:
17
app/(auth)/auth/callback/[...provider]/page.tsx
Normal file
17
app/(auth)/auth/callback/[...provider]/page.tsx
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
|
interface PageProps {
|
||||||
|
params: { provider: string[] };
|
||||||
|
searchParams: { [key: string]: string | string[] | undefined };
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = ({ params, searchParams }: PageProps) => {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<h1>Nama provider: {params.provider}</h1>
|
||||||
|
<h1>Data provider: {JSON.stringify(searchParams)}</h1>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default page;
|
||||||
@ -4,16 +4,26 @@ import { api } from "@/shared/api/connector";
|
|||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { ResponseRequestOauthUrl } from "../types/responseRequestOauthUrl";
|
import { ResponseRequestOauthUrl } from "../types/responseRequestOauthUrl";
|
||||||
|
|
||||||
const requestOauthUrl = async (requestEndpoint: string) => {
|
const requestOauthUrl = async (providerData: {
|
||||||
|
name: string;
|
||||||
|
endpoint: string;
|
||||||
|
}) => {
|
||||||
// Check if requestEndpoint is provided, if not throw an error
|
// Check if requestEndpoint is provided, if not throw an error
|
||||||
if (!requestEndpoint) throw new Error("oAuth endpoint request not found");
|
if (!providerData.endpoint)
|
||||||
|
throw new Error("oAuth endpoint request not found");
|
||||||
|
|
||||||
// Define a variable to hold the OAuth data
|
// Define a variable to hold the OAuth data
|
||||||
let oauthData: Promise<ResponseRequestOauthUrl>;
|
let oauthData: Promise<ResponseRequestOauthUrl>;
|
||||||
|
|
||||||
// Fetch OAuth data from the API
|
// Fetch OAuth data from the API
|
||||||
try {
|
try {
|
||||||
const response = await api.get(requestEndpoint);
|
const response = await api.get(providerData.endpoint, {
|
||||||
|
searchParams: {
|
||||||
|
callback: `${
|
||||||
|
process.env.APP_DOMAIN
|
||||||
|
}/auth/callback/${providerData.name.toLocaleLowerCase()}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
oauthData = response.json<ResponseRequestOauthUrl>();
|
oauthData = response.json<ResponseRequestOauthUrl>();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(JSON.stringify(error));
|
throw new Error(JSON.stringify(error));
|
||||||
|
|||||||
@ -37,13 +37,14 @@ const OAuthProviders = () => {
|
|||||||
*
|
*
|
||||||
* @param providerRequestEndpoint The request endpoint for the OAuth provider
|
* @param providerRequestEndpoint The request endpoint for the OAuth provider
|
||||||
*/
|
*/
|
||||||
const startOauthProcess = async (providerRequestEndpoint: string) => {
|
const startOauthProcess = async (providerData: {
|
||||||
|
name: string;
|
||||||
|
endpoint: string;
|
||||||
|
}) => {
|
||||||
try {
|
try {
|
||||||
setLoadingButton(true);
|
setLoadingButton(true);
|
||||||
|
|
||||||
(await requestOauthUrl(
|
(await requestOauthUrl(providerData)) as ResponseRequestOauthUrl;
|
||||||
providerRequestEndpoint
|
|
||||||
)) as ResponseRequestOauthUrl;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setLoadingButton(false);
|
setLoadingButton(false);
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@ -61,7 +62,12 @@ const OAuthProviders = () => {
|
|||||||
className="w-full hover:bg-neutral-800"
|
className="w-full hover:bg-neutral-800"
|
||||||
variant="bordered"
|
variant="bordered"
|
||||||
startContent={<Icon className="w-4 h-4" icon={provider.icon} />}
|
startContent={<Icon className="w-4 h-4" icon={provider.icon} />}
|
||||||
onPress={() => startOauthProcess(provider.req_endpoint)}
|
onPress={() =>
|
||||||
|
startOauthProcess({
|
||||||
|
name: provider.name,
|
||||||
|
endpoint: provider.req_endpoint,
|
||||||
|
})
|
||||||
|
}
|
||||||
isLoading={loadingButton}
|
isLoading={loadingButton}
|
||||||
>
|
>
|
||||||
Continue with {provider.name}
|
Continue with {provider.name}
|
||||||
|
|||||||
Reference in New Issue
Block a user