mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 17:49:09 +00:00
port full auth flow to cf workers
This commit is contained in:
parent
9eeb946389
commit
aad0987dce
50 changed files with 551 additions and 1167 deletions
40
src/providers/auth/AuthContext.tsx
Normal file
40
src/providers/auth/AuthContext.tsx
Normal file
|
@ -0,0 +1,40 @@
|
|||
import * as React from 'react';
|
||||
|
||||
type AuthContextType = {
|
||||
sessionKey: string | null;
|
||||
setSessionKey: (value: string | null) => void;
|
||||
};
|
||||
|
||||
type Props = {
|
||||
sessionKey: string | null;
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
const AuthContext = React.createContext<AuthContextType>({
|
||||
sessionKey: null,
|
||||
setSessionKey: () => {},
|
||||
});
|
||||
|
||||
export const AuthProvider = (props: Props) => {
|
||||
const [sessionKey, setSessionKey] = React.useState(props.sessionKey);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ sessionKey, setSessionKey }}>
|
||||
{props.children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useAuth = () => {
|
||||
const authCtx = React.useContext(AuthContext);
|
||||
if (!authCtx) {
|
||||
throw new Error('useAuth used without AuthProvider');
|
||||
}
|
||||
|
||||
return authCtx;
|
||||
};
|
||||
|
||||
export const isAuthenticated = () => {
|
||||
const authCtx = useAuth();
|
||||
return authCtx.sessionKey !== null;
|
||||
};
|
1
src/providers/auth/index.ts
Normal file
1
src/providers/auth/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './AuthContext';
|
Loading…
Add table
Add a link
Reference in a new issue