✨ feat: create auth provider context
This commit is contained in:
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