mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 17:49:09 +00:00
Refactor SessionContext (#181)
* fix(web): refactor session context so it works more consistently * add warning for when syncSession is locked but called again * cd * add tests to new-session
This commit is contained in:
parent
b6ae2abd2f
commit
42323a46f6
4 changed files with 206 additions and 82 deletions
34
packages/web/src/pages/machinery/new-session.spec.tsx
Normal file
34
packages/web/src/pages/machinery/new-session.spec.tsx
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { render, screen } from '@testing-library/react';
|
||||
import { useSessionContext } from '../../contexts/session/SessionContext';
|
||||
import NewSession from './new-session';
|
||||
|
||||
const setupSessionMock = jest.fn();
|
||||
(useSessionContext as jest.Mock) = jest.fn(() => ({
|
||||
setupSession: setupSessionMock,
|
||||
isAuthenticated: true,
|
||||
}));
|
||||
|
||||
const testSessionID = 'sessionid1234';
|
||||
|
||||
it('sets up the session', () => {
|
||||
render(<NewSession sessionID={testSessionID} />);
|
||||
|
||||
expect(useSessionContext).toBeCalled();
|
||||
expect(setupSessionMock).toBeCalledWith('sessionid1234');
|
||||
});
|
||||
|
||||
it('redirects to the correct location when rp_postauth_redirect is set', async () => {
|
||||
localStorage.setItem('rp_postauth_redirect', '/hello_world');
|
||||
render(<NewSession sessionID={testSessionID} />);
|
||||
|
||||
const bounceLink = screen.getByText("If you aren't redirected soon, click here.");
|
||||
expect(bounceLink.getAttribute('href')).toBe('/hello_world');
|
||||
});
|
||||
|
||||
it('redirects to the correct location by default', async () => {
|
||||
localStorage.setItem('rp_postauth_redirect', '/servers');
|
||||
render(<NewSession sessionID={testSessionID} />);
|
||||
|
||||
const bounceLink = screen.getByText("If you aren't redirected soon, click here.");
|
||||
expect(bounceLink.getAttribute('href')).toBe('/servers');
|
||||
});
|
|
@ -5,23 +5,23 @@ import { useSessionContext } from '../../contexts/session/SessionContext';
|
|||
import { Title } from '../../utils/metaTitle';
|
||||
|
||||
const NewSession = (props: { sessionID: string }) => {
|
||||
const session = useSessionContext();
|
||||
const { setupSession, isAuthenticated } = useSessionContext();
|
||||
const [postauthUrl, setPostauthUrl] = React.useState('/servers');
|
||||
|
||||
React.useEffect(() => {
|
||||
const url = new URL(window.location.href);
|
||||
const id = props.sessionID || url.searchParams.get('session_id');
|
||||
if (id) {
|
||||
localStorage.setItem('rp_session_key', id);
|
||||
// session.setSession({ sessionID: id });
|
||||
|
||||
const storedPostauthUrl = localStorage.getItem('rp_postauth_redirect');
|
||||
if (storedPostauthUrl) {
|
||||
setPostauthUrl(storedPostauthUrl);
|
||||
localStorage.removeItem('rp_postauth_redirect');
|
||||
}
|
||||
const storedPostauthUrl = localStorage.getItem('rp_postauth_redirect');
|
||||
if (storedPostauthUrl) {
|
||||
setPostauthUrl(storedPostauthUrl);
|
||||
localStorage.removeItem('rp_postauth_redirect');
|
||||
}
|
||||
}, [setPostauthUrl, props.sessionID, session]);
|
||||
}, [setPostauthUrl]);
|
||||
|
||||
React.useCallback(
|
||||
(sessionID) => {
|
||||
setupSession(sessionID);
|
||||
},
|
||||
[setupSession]
|
||||
)(props.sessionID);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
@ -30,7 +30,7 @@ const NewSession = (props: { sessionID: string }) => {
|
|||
<div>
|
||||
<Link href={postauthUrl}>If you aren't redirected soon, click here.</Link>
|
||||
</div>
|
||||
{session.isAuthenticated && <Redirect to={postauthUrl} noThrow replace />}
|
||||
{isAuthenticated && <Redirect to={postauthUrl} noThrow replace />}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue