feat(UI): add role picker, auth helpers, refactor for viability

This commit is contained in:
41666 2020-12-18 00:14:30 -05:00
parent 3fe3cfc21f
commit e4e4bb9024
32 changed files with 408 additions and 136 deletions

View 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,
};
};

View file

@ -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;
};

View file

@ -1 +0,0 @@
export * from './AuthContext';