feat: redirect to requested doc after authentication

This commit is contained in:
Tom Moor 2019-08-05 22:25:19 -07:00
parent 38a67b1f9e
commit 5c99116898
3 changed files with 24 additions and 5 deletions

View File

@ -34,7 +34,7 @@ const Authenticated = observer(({ auth, children }: Props) => {
return children;
}
auth.logout();
auth.logout(true);
return null;
});

View File

@ -10,7 +10,7 @@ type Props = {
const Home = observer(({ auth }: Props) => {
if (auth.authenticated) return <Redirect to="/dashboard" />;
auth.logout();
auth.logout(true);
return null;
});

View File

@ -76,6 +76,13 @@ export default class AuthStore {
team: team.name,
};
}
// If we came from a redirect then send the user immediately there
const postLoginRedirectPath = getCookie('postLoginRedirectPath');
if (postLoginRedirectPath) {
removeCookie('postLoginRedirectPath');
window.location.href = postLoginRedirectPath;
}
});
} catch (err) {
if (err.error === 'user_suspended') {
@ -133,9 +140,21 @@ export default class AuthStore {
};
@action
logout = async () => {
this.user = null;
this.token = null;
logout = async (savePath: boolean = false) => {
// remove user and team from localStorage
localStorage.setItem(
AUTH_STORE,
JSON.stringify({
user: null,
team: null,
})
);
// if this logout was forced from an authenticated route then
// save the current path so we can go back there once signed in
if (savePath) {
setCookie('postLoginRedirectPath', window.location.pathname);
}
// remove authentication token itself
removeCookie('accessToken', { path: '/' });