mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-24 19:39:11 +00:00
Improve pre- and post-auth redirect flows (#180)
* fix(web): add /machinery/new-session/... route, start auth flow from there * fix(api): change redirect to path-based format * fix(api): remove extra / from login-callback redirect * fix(web): /auth/login should skip flows if isAuthenticated is true
This commit is contained in:
parent
8fbf8f2519
commit
fd02dad62b
5 changed files with 33 additions and 11 deletions
|
@ -106,9 +106,7 @@ export const LoginCallback = resolveFailures(
|
||||||
|
|
||||||
await Sessions.put(sessionID.string, sessionData, 60 * 60 * 6);
|
await Sessions.put(sessionID.string, sessionData, 60 * 60 * 6);
|
||||||
|
|
||||||
return Bounce(
|
return Bounce(bounceBaseUrl + 'machinery/new-session/' + sessionID.string);
|
||||||
bounceBaseUrl + '/machinery/new-session?session_id=' + sessionID.string
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,10 @@ export const AppRouter = () => {
|
||||||
<RouteWrapper component={PickerPage} path="/s/:serverID" />
|
<RouteWrapper component={PickerPage} path="/s/:serverID" />
|
||||||
|
|
||||||
<RouteWrapper component={MachineryNewSession} path="/machinery/new-session" />
|
<RouteWrapper component={MachineryNewSession} path="/machinery/new-session" />
|
||||||
|
<RouteWrapper
|
||||||
|
component={MachineryNewSession}
|
||||||
|
path="/machinery/new-session/:sessionID"
|
||||||
|
/>
|
||||||
<RouteWrapper component={MachineryLogout} path="/machinery/logout" />
|
<RouteWrapper component={MachineryLogout} path="/machinery/logout" />
|
||||||
<RouteWrapper component={MachineryBotJoin} path="/machinery/bot-join" />
|
<RouteWrapper component={MachineryBotJoin} path="/machinery/bot-join" />
|
||||||
<RouteWrapper component={MachineryBotJoin} path="/machinery/bot-join/:serverID" />
|
<RouteWrapper component={MachineryBotJoin} path="/machinery/bot-join/:serverID" />
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
|
import { redirectTo } from '@reach/router';
|
||||||
import { AuthLogin } from '@roleypoly/design-system/templates/auth-login';
|
import { AuthLogin } from '@roleypoly/design-system/templates/auth-login';
|
||||||
import { GuildSlug } from '@roleypoly/types';
|
import { GuildSlug } from '@roleypoly/types';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useApiContext } from '../../contexts/api/ApiContext';
|
import { useApiContext } from '../../contexts/api/ApiContext';
|
||||||
|
import { useSessionContext } from '../../contexts/session/SessionContext';
|
||||||
import { Title } from '../../utils/metaTitle';
|
import { Title } from '../../utils/metaTitle';
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const { apiUrl, fetch } = useApiContext();
|
const { apiUrl, fetch } = useApiContext();
|
||||||
|
const { isAuthenticated } = useSessionContext();
|
||||||
// If ?r is in query, then let's render the slug page
|
// If ?r is in query, then let's render the slug page
|
||||||
// If not, redirect.
|
// If not, redirect.
|
||||||
const [guildSlug, setGuildSlug] = React.useState<GuildSlug | null>(null);
|
const [guildSlug, setGuildSlug] = React.useState<GuildSlug | null>(null);
|
||||||
|
@ -17,6 +20,9 @@ const Login = () => {
|
||||||
const redirectServerID = url.searchParams.get('r');
|
const redirectServerID = url.searchParams.get('r');
|
||||||
const redirectUrl = `${apiUrl}/login-bounce?cbh=${callbackHost.href}`;
|
const redirectUrl = `${apiUrl}/login-bounce?cbh=${callbackHost.href}`;
|
||||||
if (!redirectServerID) {
|
if (!redirectServerID) {
|
||||||
|
if (isAuthenticated) {
|
||||||
|
redirectTo('/servers');
|
||||||
|
}
|
||||||
window.location.href = redirectUrl;
|
window.location.href = redirectUrl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -33,7 +39,11 @@ const Login = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchGuildSlug(redirectServerID);
|
fetchGuildSlug(redirectServerID);
|
||||||
}, [apiUrl, fetch]);
|
|
||||||
|
if (isAuthenticated) {
|
||||||
|
redirectTo(`/s/${redirectServerID}`);
|
||||||
|
}
|
||||||
|
}, [apiUrl, fetch, isAuthenticated]);
|
||||||
|
|
||||||
if (guildSlug === null) {
|
if (guildSlug === null) {
|
||||||
return <div>Loading...</div>;
|
return <div>Loading...</div>;
|
||||||
|
|
|
@ -10,7 +10,7 @@ const Landing = () => {
|
||||||
const appShellProps = useAppShellProps();
|
const appShellProps = useAppShellProps();
|
||||||
|
|
||||||
if (isAuthenticated) {
|
if (isAuthenticated) {
|
||||||
return <Redirect to="/servers" />;
|
return <Redirect to="/servers" noThrow />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,22 +1,32 @@
|
||||||
|
import { Redirect } from '@reach/router';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import { useSessionContext } from '../../contexts/session/SessionContext';
|
||||||
import { Title } from '../../utils/metaTitle';
|
import { Title } from '../../utils/metaTitle';
|
||||||
|
|
||||||
const NewSession = () => {
|
const NewSession = (props: { sessionID: string }) => {
|
||||||
|
const session = useSessionContext();
|
||||||
|
const [postauthUrl, setPostauthUrl] = React.useState('/servers');
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const url = new URL(window.location.href);
|
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) {
|
if (id) {
|
||||||
localStorage.setItem('rp_session_key', id);
|
localStorage.setItem('rp_session_key', id);
|
||||||
|
session.setSession({ sessionID: id });
|
||||||
|
|
||||||
const redirectUrl = localStorage.getItem('rp_postauth_redirect');
|
const storedPostauthUrl = localStorage.getItem('rp_postauth_redirect');
|
||||||
window.location.href = redirectUrl || '/';
|
if (storedPostauthUrl) {
|
||||||
|
setPostauthUrl(storedPostauthUrl);
|
||||||
|
localStorage.removeItem('rp_postauth_redirect');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
}, [setPostauthUrl, props.sessionID, session]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Title title="Logging you into Roleypoly..." />
|
<Title title="Logging you into Roleypoly..." />
|
||||||
<div>Redirecting you...</div>
|
<div>Logging you into Roleypoly...</div>
|
||||||
|
{session.isAuthenticated && <Redirect to={postauthUrl} noThrow replace />}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue