mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 09:39:09 +00:00
feat(UI): add role picker, auth helpers, refactor for viability
This commit is contained in:
parent
3fe3cfc21f
commit
e4e4bb9024
32 changed files with 408 additions and 136 deletions
27
src/providers/appShellData.tsx
Normal file
27
src/providers/appShellData.tsx
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { NextPageContext } from 'next';
|
||||
import { SessionData } from 'roleypoly/common/types';
|
||||
import { swrFetch } from 'roleypoly/common/utils/isomorphicFetch';
|
||||
import { AppShellProps } from 'roleypoly/design-system/organisms/app-shell';
|
||||
|
||||
export type ProvidableAppShellProps = {
|
||||
user: AppShellProps['user'];
|
||||
guilds: AppShellProps['guilds'];
|
||||
};
|
||||
|
||||
export const useAppShellProps = (context?: NextPageContext) => {
|
||||
const { data, error } = swrFetch<Omit<SessionData, 'tokens'>>(
|
||||
'/get-session',
|
||||
context
|
||||
);
|
||||
|
||||
const props: ProvidableAppShellProps = {
|
||||
user: data?.user,
|
||||
guilds: data?.guilds,
|
||||
};
|
||||
|
||||
return {
|
||||
appShellProps: props,
|
||||
isLoading: !error && !data,
|
||||
isError: error,
|
||||
};
|
||||
};
|
|
@ -1,40 +0,0 @@
|
|||
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 +0,0 @@
|
|||
export * from './AuthContext';
|
Loading…
Add table
Add a link
Reference in a new issue