forked from ajnart/homarr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logout callback URL and session expiration environment variables (a…
…jnart#2023) Co-authored-by: @Meierschlumpf
- Loading branch information
Showing
7 changed files
with
107 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import dayjs from 'dayjs'; | ||
import { Session } from 'next-auth'; | ||
import { SessionProvider, signIn } from 'next-auth/react'; | ||
import { createContext, useContext, useEffect } from 'react'; | ||
|
||
interface CustomSessionProviderProps { | ||
session: Session; | ||
children: React.ReactNode; | ||
logoutUrl?: string; | ||
} | ||
|
||
export const CustomSessionProvider = ({ | ||
session, | ||
children, | ||
logoutUrl, | ||
}: CustomSessionProviderProps) => { | ||
//Automatically redirect to the login page after a session expires or after 24 days | ||
useEffect(() => { | ||
if (!session) return () => {}; | ||
//setTimeout doesn't allow for a number higher than 2147483647 (2³¹-1 , or roughly 24 days) | ||
const timeout = setTimeout(signIn, Math.min(dayjs(session.expires).diff(), 2147483647)); | ||
return () => clearTimeout(timeout); | ||
}, [session]); | ||
|
||
return ( | ||
<SessionProvider session={session} refetchOnWindowFocus={false}> | ||
<SessionContext.Provider value={{ logoutUrl }}>{children}</SessionContext.Provider> | ||
</SessionProvider> | ||
); | ||
}; | ||
|
||
interface SessionContextProps { | ||
logoutUrl?: string; | ||
} | ||
|
||
const SessionContext = createContext<SessionContextProps | null>(null); | ||
|
||
export function useLogoutUrl() { | ||
const context = useContext(SessionContext); | ||
if (!context) { | ||
throw new Error('You cannot use logoutUrl outside of session context.'); | ||
} | ||
return context.logoutUrl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters