diff --git a/packages/api/handlers/login-callback.ts b/packages/api/handlers/login-callback.ts index eff593b..8a5db34 100644 --- a/packages/api/handlers/login-callback.ts +++ b/packages/api/handlers/login-callback.ts @@ -106,9 +106,7 @@ export const LoginCallback = resolveFailures( await Sessions.put(sessionID.string, sessionData, 60 * 60 * 6); - return Bounce( - bounceBaseUrl + '/machinery/new-session?session_id=' + sessionID.string - ); + return Bounce(bounceBaseUrl + 'machinery/new-session/' + sessionID.string); } ); diff --git a/packages/web/src/app-router/AppRouter.tsx b/packages/web/src/app-router/AppRouter.tsx index 31ff9ab..95c54dd 100644 --- a/packages/web/src/app-router/AppRouter.tsx +++ b/packages/web/src/app-router/AppRouter.tsx @@ -31,6 +31,10 @@ export const AppRouter = () => { + diff --git a/packages/web/src/pages/auth/login.tsx b/packages/web/src/pages/auth/login.tsx index 0eae3a3..147bfd4 100644 --- a/packages/web/src/pages/auth/login.tsx +++ b/packages/web/src/pages/auth/login.tsx @@ -1,11 +1,14 @@ +import { redirectTo } from '@reach/router'; import { AuthLogin } from '@roleypoly/design-system/templates/auth-login'; import { GuildSlug } from '@roleypoly/types'; import React from 'react'; import { useApiContext } from '../../contexts/api/ApiContext'; +import { useSessionContext } from '../../contexts/session/SessionContext'; import { Title } from '../../utils/metaTitle'; const Login = () => { const { apiUrl, fetch } = useApiContext(); + const { isAuthenticated } = useSessionContext(); // If ?r is in query, then let's render the slug page // If not, redirect. const [guildSlug, setGuildSlug] = React.useState(null); @@ -17,6 +20,9 @@ const Login = () => { const redirectServerID = url.searchParams.get('r'); const redirectUrl = `${apiUrl}/login-bounce?cbh=${callbackHost.href}`; if (!redirectServerID) { + if (isAuthenticated) { + redirectTo('/servers'); + } window.location.href = redirectUrl; return; } @@ -33,7 +39,11 @@ const Login = () => { }; fetchGuildSlug(redirectServerID); - }, [apiUrl, fetch]); + + if (isAuthenticated) { + redirectTo(`/s/${redirectServerID}`); + } + }, [apiUrl, fetch, isAuthenticated]); if (guildSlug === null) { return
Loading...
; diff --git a/packages/web/src/pages/landing.tsx b/packages/web/src/pages/landing.tsx index f663d9f..fecbfb3 100644 --- a/packages/web/src/pages/landing.tsx +++ b/packages/web/src/pages/landing.tsx @@ -10,7 +10,7 @@ const Landing = () => { const appShellProps = useAppShellProps(); if (isAuthenticated) { - return ; + return ; } return ( diff --git a/packages/web/src/pages/machinery/new-session.tsx b/packages/web/src/pages/machinery/new-session.tsx index 959f80f..798b177 100644 --- a/packages/web/src/pages/machinery/new-session.tsx +++ b/packages/web/src/pages/machinery/new-session.tsx @@ -1,22 +1,32 @@ +import { Redirect } from '@reach/router'; import * as React from 'react'; +import { useSessionContext } from '../../contexts/session/SessionContext'; import { Title } from '../../utils/metaTitle'; -const NewSession = () => { +const NewSession = (props: { sessionID: string }) => { + const session = useSessionContext(); + const [postauthUrl, setPostauthUrl] = React.useState('/servers'); + React.useEffect(() => { const url = new URL(window.location.href); - const id = url.searchParams.get('session_id'); + const id = props.sessionID || url.searchParams.get('session_id'); if (id) { localStorage.setItem('rp_session_key', id); + session.setSession({ sessionID: id }); - const redirectUrl = localStorage.getItem('rp_postauth_redirect'); - window.location.href = redirectUrl || '/'; + const storedPostauthUrl = localStorage.getItem('rp_postauth_redirect'); + if (storedPostauthUrl) { + setPostauthUrl(storedPostauthUrl); + localStorage.removeItem('rp_postauth_redirect'); + } } - }); + }, [setPostauthUrl, props.sessionID, session]); return ( <> - <div>Redirecting you...</div> + <div>Logging you into Roleypoly...</div> + {session.isAuthenticated && <Redirect to={postauthUrl} noThrow replace />} </> ); };