✨ feat: create auth provider context
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import type { Metadata } from "next";
|
||||
import { Geist, Geist_Mono, Inter } from "next/font/google";
|
||||
import Navbar from "@/shared/widgets/navbar/components/Navbar";
|
||||
import "./globals.css";
|
||||
import AuthSessionProviderWrapper from "@/shared/providers/AuthSession";
|
||||
|
||||
const inter = Inter({ subsets: ["latin"], variable: "--font-sans" });
|
||||
|
||||
@ -30,7 +30,7 @@ export default function RootLayout({
|
||||
<body
|
||||
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
|
||||
>
|
||||
{children}
|
||||
<AuthSessionProviderWrapper>{children}</AuthSessionProviderWrapper>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
"use server";
|
||||
|
||||
import { backendFetch, BackendResponse } from "@/shared/helper/backendFetch";
|
||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export const submitProviderCallback = async (
|
||||
providerName: string,
|
||||
queries?: unknown
|
||||
queries?: unknown,
|
||||
): Promise<
|
||||
BackendResponse<{
|
||||
authToken: string;
|
||||
@ -15,7 +15,7 @@ export const submitProviderCallback = async (
|
||||
const envKey = providerName.toUpperCase() + "_CALLBACK_URL";
|
||||
|
||||
const authClientCallbackUrl = (await backendFetch(
|
||||
"auth/providers/" + providerName + "/callback"
|
||||
"auth/providers/" + providerName + "/callback",
|
||||
)) as BackendResponse<{
|
||||
callback_url: string;
|
||||
}>;
|
||||
@ -26,7 +26,7 @@ export const submitProviderCallback = async (
|
||||
const responseProvision = (await backendFetch(
|
||||
`${authClientCallbackUrl.data?.callback_url!}?callbackURI=${
|
||||
process.env.APP_URL
|
||||
}${process.env[envKey]}&${queries}`
|
||||
}${process.env[envKey]}&${queries}`,
|
||||
)) as BackendResponse<{
|
||||
authToken: string;
|
||||
}>;
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
"use client";
|
||||
import { Spinner } from "@/shared/libs/shadcn/ui/spinner";
|
||||
import { submitProviderCallback } from "@/features/authCallback/actions/submitProviderCallback";
|
||||
import { useParams, useRouter, useSearchParams } from "next/navigation";
|
||||
import { useParams, useSearchParams } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
const AuthCallbackIndex = () => {
|
||||
const { name } = useParams();
|
||||
const queries = useSearchParams().toString();
|
||||
const [textDescription, setTextDescription] = useState(
|
||||
"We are processing your authentication."
|
||||
"We are processing your authentication.",
|
||||
);
|
||||
|
||||
const finishOAuthFlow = (type: string) => {
|
||||
@ -26,6 +26,7 @@ const AuthCallbackIndex = () => {
|
||||
setTextDescription("Authentication successful! Redirecting...");
|
||||
finishOAuthFlow("oauth-success");
|
||||
} else {
|
||||
console.error("Error in authentication callback:", response);
|
||||
setTextDescription("Authentication failed. Please try again.");
|
||||
finishOAuthFlow("oauth-failed");
|
||||
}
|
||||
|
||||
14
shared/contexts/AuthContext.tsx
Normal file
14
shared/contexts/AuthContext.tsx
Normal file
@ -0,0 +1,14 @@
|
||||
import { createContext, useContext } from "react";
|
||||
import { UserSession } from "../models/auth/validateAndDecodeJWT";
|
||||
|
||||
type AuthContextType = {
|
||||
session: UserSession | null;
|
||||
};
|
||||
|
||||
export const AuthContext = createContext<AuthContextType>({
|
||||
session: null,
|
||||
});
|
||||
|
||||
export function useAuth() {
|
||||
return useContext(AuthContext);
|
||||
}
|
||||
42
shared/models/auth/validateAndDecodeJWT.ts
Normal file
42
shared/models/auth/validateAndDecodeJWT.ts
Normal file
@ -0,0 +1,42 @@
|
||||
"use server";
|
||||
|
||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export interface UserSession {
|
||||
id: string;
|
||||
isAuthenticated: boolean;
|
||||
validUntil: Date;
|
||||
user: {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
username: string;
|
||||
avatar: string;
|
||||
birthDate: null;
|
||||
bioProfile: null;
|
||||
preference: {
|
||||
id: string;
|
||||
userId: string;
|
||||
langPreference: null;
|
||||
adultFiltering: string;
|
||||
adultAlert: string;
|
||||
videoQuality: string;
|
||||
serviceDefaultId: null;
|
||||
};
|
||||
};
|
||||
iat: number;
|
||||
exp: number;
|
||||
}
|
||||
|
||||
export const validateAndDecodeJWT = async (): Promise<UserSession> => {
|
||||
const cookieHeader = (await cookies()).get("auth_token")?.value;
|
||||
const res = (await backendFetch("auth/token/validate", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
token: cookieHeader,
|
||||
}),
|
||||
})) as BackendResponse<UserSession>;
|
||||
|
||||
return res.data!;
|
||||
};
|
||||
17
shared/providers/AuthSession.client.tsx
Normal file
17
shared/providers/AuthSession.client.tsx
Normal file
@ -0,0 +1,17 @@
|
||||
"use client";
|
||||
import { AuthContext } from "../contexts/AuthContext";
|
||||
import React from "react";
|
||||
import { UserSession } from "../models/auth/validateAndDecodeJWT";
|
||||
|
||||
const AuthSessionProvider = ({
|
||||
children,
|
||||
session,
|
||||
}: Readonly<{ children: React.ReactNode; session: UserSession | null }>) => {
|
||||
return (
|
||||
<AuthContext.Provider value={{ session: session }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthSessionProvider;
|
||||
@ -1,10 +1,17 @@
|
||||
import { cookies } from "next/headers";
|
||||
import React from "react";
|
||||
import {
|
||||
UserSession,
|
||||
validateAndDecodeJWT,
|
||||
} from "../models/auth/validateAndDecodeJWT";
|
||||
import AuthSessionProvider from "./AuthSession.client";
|
||||
|
||||
const AuthSessionProvider = ({children}: readonly<{children: React.ReactNode}>) => {
|
||||
const cookieHeader = cookies().toString();
|
||||
console.log("Cookies in AuthSessionProvider:", cookieHeader);
|
||||
return <AuthContext.Provider value={{ cookie = cookieHeader }}>{children}</AuthContext.Provider>;
|
||||
const AuthSessionProviderWrapper = async ({
|
||||
children,
|
||||
}: Readonly<{ children: React.ReactNode }>) => {
|
||||
let session: UserSession | null = await validateAndDecodeJWT();
|
||||
|
||||
return (
|
||||
<AuthSessionProvider session={session}>{children}</AuthSessionProvider>
|
||||
);
|
||||
};
|
||||
|
||||
export default AuthSessionProvider;
|
||||
export default AuthSessionProviderWrapper;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
"use server";
|
||||
import { backendFetch, BackendResponse } from "@/shared/helper/backendFetch";
|
||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||
|
||||
export type GetALlThirdPartyAuthCallback = BackendResponse<
|
||||
{
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
"use server";
|
||||
import { backendFetch, BackendResponse } from "@/shared/helper/backendFetch";
|
||||
import { backendFetch, BackendResponse } from "@/shared/helpers/backendFetch";
|
||||
|
||||
interface GetOauthEndpointParams {
|
||||
endpointUrl: string;
|
||||
@ -13,7 +13,7 @@ export const getOauthEndpoint = async ({
|
||||
const envKey = providerName.toUpperCase() + "_CALLBACK_URL";
|
||||
|
||||
return (await backendFetch(
|
||||
`${endpointUrl}?callback=${process.env.APP_URL}${process.env[envKey]}`
|
||||
`${endpointUrl}?callback=${process.env.APP_URL}${process.env[envKey]}`,
|
||||
)) as BackendResponse<{
|
||||
endpointUrl: string;
|
||||
}>;
|
||||
|
||||
Reference in New Issue
Block a user