diff --git a/apps/polyflix/src/main.tsx b/apps/polyflix/src/main.tsx index 12df94a6..f741bdf9 100644 --- a/apps/polyflix/src/main.tsx +++ b/apps/polyflix/src/main.tsx @@ -111,13 +111,22 @@ const PolyflixApp = () => { } } + // We want to redirect the user to the wanted page after the authentication + // So we get the current url and pass it to the AuthRouter + const wantedUri = window.location.href + return ( - {/* We want the user to be redirected to home page if already logged in */} + {/* We want the user to be redirected if already logged in or not */} } + render={() => ( + + )} /> diff --git a/apps/polyflix/src/modules/authentication/auth.router.tsx b/apps/polyflix/src/modules/authentication/auth.router.tsx index 3bdea5ea..dbc17d2f 100644 --- a/apps/polyflix/src/modules/authentication/auth.router.tsx +++ b/apps/polyflix/src/modules/authentication/auth.router.tsx @@ -8,20 +8,33 @@ import { MockAuthenticationPage } from './pages/Mock.page' interface Props { isAuthenticated: boolean + redirectUrl: string } -export const AuthRouter = ({ isAuthenticated }: Props) => { +export const AuthRouter = ({ + isAuthenticated, + redirectUrl: redirectUri, +}: Props) => { const { url } = useRouteMatch() + // If we are in a mocked environment, we don't want to redirect to keycloak if (!isAuthenticated && environment.mocked) { - return + return } return ( - + } + /> - + } + /> ) diff --git a/apps/polyflix/src/modules/authentication/pages/Mock.page.tsx b/apps/polyflix/src/modules/authentication/pages/Mock.page.tsx index b016581e..51b53367 100644 --- a/apps/polyflix/src/modules/authentication/pages/Mock.page.tsx +++ b/apps/polyflix/src/modules/authentication/pages/Mock.page.tsx @@ -7,19 +7,23 @@ import { User } from '@users/models/user.model' import { BaseUsers } from 'mock-server' import { useHistory } from 'react-router-dom' -export function MockAuthenticationPage() { +interface Props { + redirectUri: string +} + +export function MockAuthenticationPage({ redirectUri: redirectUri }: Props) { const authService = useInjection(AuthService) const history = useHistory() let { keycloak } = useKeycloak() - function login(user: User) { + function login(user: User, redirectUrl: string = '/') { keycloak.subject = user.id authService .getUser() .catch(console.error) .finally(() => { keycloak.token = 'my-mock-token' - history.push('/') + history.push(redirectUrl) }) } @@ -47,7 +51,7 @@ export function MockAuthenticationPage() {