From bb4c86114e2c351cee49b763024745e554b31139 Mon Sep 17 00:00:00 2001 From: Ann Catton Date: Thu, 7 Jan 2021 12:32:59 -0500 Subject: [PATCH 01/23] wip connect login --- components/Root.tsx | 7 +++-- global/hooks/useAuthContext.tsx | 53 +++++++++++++++++++++++++++++++++ global/utils/constants.ts | 1 + pages/_app.tsx | 15 ++++++---- pages/index.tsx | 28 +++++++++++++++++ 5 files changed, 97 insertions(+), 7 deletions(-) create mode 100644 global/hooks/useAuthContext.tsx create mode 100644 global/utils/constants.ts diff --git a/components/Root.tsx b/components/Root.tsx index a75b7bb0..deb10c9e 100644 --- a/components/Root.tsx +++ b/components/Root.tsx @@ -2,8 +2,9 @@ import React from 'react'; import { ThemeProvider } from 'emotion-theming'; import defaultTheme from './theme'; import Head from './Head'; +import { AuthProvider } from '../global/hooks/useAuthContext'; -const Root = ({ children }: { children: React.ReactElement }) => { +const Root = ({ children, egoJwt }: { children: React.ReactElement; egoJwt: string }) => { return ( <> - {children} + + {children} + ); }; diff --git a/global/hooks/useAuthContext.tsx b/global/hooks/useAuthContext.tsx new file mode 100644 index 00000000..3c08e13a --- /dev/null +++ b/global/hooks/useAuthContext.tsx @@ -0,0 +1,53 @@ +import React from 'react'; + +import { EGO_JWT_KEY } from '../utils/constants'; +// logout +// update +// set + +type T_AuthContext = { + token?: string | null; + logout: () => void; + updateToken?: () => Promise; +}; + +const AuthContext = React.createContext({ + token: undefined, + logout: () => {}, + updateToken: async () => {}, +}); + +export const AuthProvider = ({ + egoJwt, + children, +}: { + egoJwt: string; + children: React.ReactElement; +}) => { + const [token, setTokenState] = React.useState(egoJwt); + + const setToken = (token: string) => { + localStorage.setItem(EGO_JWT_KEY, token); + setTokenState(token); + }; + + const removeToken = () => { + localStorage.removeItem(EGO_JWT_KEY); + setTokenState(null); + }; + + const logout = () => { + removeToken(); + }; + + const authData = { + token, + logout, + }; + + return {children}; +}; + +export default function useAuthContext() { + return React.useContext(AuthContext); +} diff --git a/global/utils/constants.ts b/global/utils/constants.ts new file mode 100644 index 00000000..29225f60 --- /dev/null +++ b/global/utils/constants.ts @@ -0,0 +1 @@ +export const EGO_JWT_KEY = 'EGO_JWT'; diff --git a/pages/_app.tsx b/pages/_app.tsx index 0fa68e23..f60f80cf 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -1,17 +1,22 @@ import Root from '../components/Root'; +import { NextPageContext } from 'next'; -function MyApp({ +const App = ({ Component, pageProps, + egoJwt = '', + ctx, }: { Component: React.ComponentType; pageProps: { [k: string]: any }; -}) { + egoJwt?: string; + ctx: NextPageContext; +}) => { return ( - + ); -} +}; -export default MyApp; +export default App; diff --git a/pages/index.tsx b/pages/index.tsx index 62855153..52375267 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,9 +1,37 @@ +import React from 'react'; import { css } from '@emotion/core'; import { useTheme } from 'emotion-theming'; +import urlJoin from 'url-join'; + import defaultTheme from '../components/theme'; +import { getConfig } from '../global/config'; +import { EGO_JWT_KEY } from '../global/utils/constants'; const HomePage = () => { const theme: typeof defaultTheme = useTheme(); + const { NEXT_PUBLIC_EGO_API_ROOT, NEXT_PUBLIC_EGO_CLIENT_ID } = getConfig(); + React.useEffect(() => { + const egoLoginUrl = urlJoin( + NEXT_PUBLIC_EGO_API_ROOT, + `/api/oauth/ego-token?client_id=${NEXT_PUBLIC_EGO_CLIENT_ID}`, + ); + fetch(egoLoginUrl, { + credentials: 'include', + headers: { accept: '*/*' }, + body: null, + method: 'GET', + mode: 'cors', + }) + .then((res) => res.text()) + .then((egoToken) => { + localStorage.setItem(EGO_JWT_KEY, egoToken); + redirect(egoToken); + }) + .catch((err) => { + console.warn('err: ', err); + redirect(null); + }); + }); return (
Date: Thu, 4 Feb 2021 10:18:35 -0500 Subject: [PATCH 02/23] wip ego login --- components/Root.tsx | 2 +- global/hooks/useAuthContext.tsx | 19 +++++------ global/utils/pages/index.tsx | 7 +++- global/utils/pages/types.ts | 1 + next-env.d.ts | 4 +++ package-lock.json | 47 ++++++++++++++++++++++++--- package.json | 6 +++- pages/_app.tsx | 39 ++++++++++++++++++++-- pages/index.tsx | 53 +++++++----------------------- pages/logged-in.tsx | 57 +++++++++++++++++++++++++++++++++ pages/login/index.tsx | 9 +++--- pages/repository/index.tsx | 1 + url-join.d.ts | 1 - 13 files changed, 180 insertions(+), 66 deletions(-) create mode 100644 pages/logged-in.tsx delete mode 100644 url-join.d.ts diff --git a/components/Root.tsx b/components/Root.tsx index deb10c9e..079d840b 100644 --- a/components/Root.tsx +++ b/components/Root.tsx @@ -4,7 +4,7 @@ import defaultTheme from './theme'; import Head from './Head'; import { AuthProvider } from '../global/hooks/useAuthContext'; -const Root = ({ children, egoJwt }: { children: React.ReactElement; egoJwt: string }) => { +const Root = ({ children, egoJwt }: { children: React.ReactElement; egoJwt?: string }) => { return ( <>