feat: add logout redirect url (#954)

* feat: add logout redirect url

* fix: format issue
This commit is contained in:
Meier Lukas
2024-08-09 19:24:07 +02:00
committed by GitHub
parent 67195c6051
commit 13e09968d9
5 changed files with 34 additions and 5 deletions

View File

@@ -1,19 +1,38 @@
"use client";
import { createContext, useContext, useEffect } from "react";
import type { PropsWithChildren } from "react";
import { useEffect } from "react";
import dayjs from "dayjs";
import type { Session } from "@homarr/auth";
import { SessionProvider, signIn } from "@homarr/auth/client";
interface AuthProviderProps {
interface AuthProviderProps extends AuthContextProps {
session: Session | null;
}
export const AuthProvider = ({ children, session }: PropsWithChildren<AuthProviderProps>) => {
export const AuthProvider = ({ children, session, logoutUrl }: PropsWithChildren<AuthProviderProps>) => {
useLoginRedirectOnSessionExpiry(session);
return <SessionProvider session={session}>{children}</SessionProvider>;
return (
<SessionProvider session={session}>
<AuthContext.Provider value={{ logoutUrl }}>{children}</AuthContext.Provider>
</SessionProvider>
);
};
interface AuthContextProps {
logoutUrl: string | undefined;
}
const AuthContext = createContext<AuthContextProps | null>(null);
export const useAuthContext = () => {
const context = useContext(AuthContext);
if (!context) throw new Error("useAuthContext must be used within an AuthProvider");
return context;
};
const useLoginRedirectOnSessionExpiry = (session: Session | null) => {