🛂(frontend) redirect to correct url after login
If a user wanted to access a doc but was not logged in, they would be redirected to the login page. After logging in, they would be redirected to the home page. This change makes it so that they are redirected to the doc they originally wanted to access. Usefull from the mail sent to the user to access the doc they were invited to.
This commit is contained in:
@@ -4,6 +4,7 @@ import { PropsWithChildren, useEffect } from 'react';
|
||||
import { Box } from '@/components';
|
||||
|
||||
import { useAuthStore } from './useAuthStore';
|
||||
|
||||
export const Auth = ({ children }: PropsWithChildren) => {
|
||||
const { authenticated, initAuth } = useAuthStore();
|
||||
|
||||
|
||||
1
src/frontend/apps/impress/src/core/auth/conf.ts
Normal file
1
src/frontend/apps/impress/src/core/auth/conf.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const PATH_AUTH_LOCAL_STORAGE = 'docs-path-auth';
|
||||
@@ -3,6 +3,7 @@ import { create } from 'zustand';
|
||||
import { baseApiUrl } from '@/core/conf';
|
||||
|
||||
import { User, getMe } from './api';
|
||||
import { PATH_AUTH_LOCAL_STORAGE } from './conf';
|
||||
|
||||
interface AuthStore {
|
||||
authenticated: boolean;
|
||||
@@ -23,9 +24,26 @@ export const useAuthStore = create<AuthStore>((set) => ({
|
||||
initAuth: () => {
|
||||
getMe()
|
||||
.then((data: User) => {
|
||||
// If a path is stored in the local storage, we redirect to it
|
||||
const path_auth = localStorage.getItem(PATH_AUTH_LOCAL_STORAGE);
|
||||
if (path_auth) {
|
||||
localStorage.removeItem(PATH_AUTH_LOCAL_STORAGE);
|
||||
window.location.replace(path_auth);
|
||||
return;
|
||||
}
|
||||
|
||||
set({ authenticated: true, userData: data });
|
||||
})
|
||||
.catch(() => {
|
||||
// If we try to access a specific page and we are not authenticated
|
||||
// we store the path in the local storage to redirect to it after login
|
||||
if (window.location.pathname !== '/') {
|
||||
localStorage.setItem(
|
||||
PATH_AUTH_LOCAL_STORAGE,
|
||||
window.location.pathname,
|
||||
);
|
||||
}
|
||||
|
||||
window.location.replace(new URL('authenticate/', baseApiUrl()).href);
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user