port full auth flow to cf workers

This commit is contained in:
41666 2020-12-05 03:09:20 -05:00
parent 9eeb946389
commit aad0987dce
50 changed files with 551 additions and 1167 deletions

View file

@ -1,11 +1,36 @@
import { AppProps } from 'next/app';
import NextApp, { AppContext, AppProps } from 'next/app';
import * as React from 'react';
import { InjectTypekitFont } from 'roleypoly/design-system/atoms/fonts';
import nookies from 'nookies';
import { AuthProvider } from 'roleypoly/providers/auth/AuthContext';
const App = (props: AppProps) => (
type Props = AppProps & {
sessionKey: string | null;
};
const App = (props: Props) => (
<>
<InjectTypekitFont />
<props.Component {...props.pageProps} />
<AuthProvider sessionKey={props.sessionKey}>
<props.Component {...props.pageProps} />
</AuthProvider>
</>
);
export default App;
export const getInitialProps = async (context: AppContext) => {
let sessionKey: string | null = null;
if (context.ctx.req) {
const key = nookies.get(context.ctx)['rp_session_key'];
if (key) {
sessionKey = key;
}
} else {
sessionKey = sessionStorage.getItem('session_key');
}
const pageProps = await NextApp.getInitialProps(context);
return { ...pageProps, sessionKey };
};

View file

@ -1,5 +1,9 @@
import { NextPageContext } from 'next';
import * as React from 'react';
import nookies from 'nookies';
import { AppShell } from 'roleypoly/design-system/organisms/app-shell';
import { Hero } from 'roleypoly/design-system/atoms/hero';
import { AccentTitle } from 'roleypoly/design-system/atoms/typography';
type Props = {
sessionID: string;
@ -9,19 +13,33 @@ const NewSession = (props: Props) => {
const { sessionID } = props;
React.useEffect(() => {
sessionStorage.setItem('session_key', sessionID);
location.href = '/';
}, [sessionID]);
return <div>Logging you in...</div>;
return (
<AppShell>
<Hero>
<AccentTitle>Logging you in...</AccentTitle>
</Hero>
</AppShell>
);
};
NewSession.getInitialProps = (context: NextPageContext): Props => {
const sessionID = context.query.session_id;
export const getServerSideProps = (context: NextPageContext): { props: Props } => {
const sessionID = context.query.session_id as string;
if (!sessionID) {
throw new Error("I shouldn't be here today.");
}
return { sessionID: sessionID as string };
nookies.set(context, 'rp_session_key', sessionID, {
httpOnly: true,
maxAge: 60 * 60 * 6,
path: '/',
sameSite: 'strict',
});
return { props: { sessionID } };
};
export default NewSession;