Skip to content

Commit

Permalink
fix(auth): redirection works again in mock and regular mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-reynaud committed Dec 16, 2022
1 parent e3958ec commit 4690717
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
13 changes: 11 additions & 2 deletions apps/polyflix/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Router>
<Switch>
{/* 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 */}
<Route
path="/auth"
render={() => <AuthRouter isAuthenticated={isAuthenticated} />}
render={() => (
<AuthRouter
isAuthenticated={isAuthenticated}
redirectUrl={wantedUri}
/>
)}
/>
<Route path="/certificate/:id" component={CertificatePage} />

Expand Down
21 changes: 17 additions & 4 deletions apps/polyflix/src/modules/authentication/auth.router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MockAuthenticationPage />
return <MockAuthenticationPage redirectUri={redirectUri} />
}

return (
<Switch>
<Route exact path={`${url}/redirect`} component={RedirectPage} />
<Route
exact
path={`${url}/redirect`}
render={() => <RedirectPage redirectUri={redirectUri} />}
/>
<PrivateRoute condition={!isAuthenticated} redirectTo={'/'}>
<Route exact path={`${url}/login`} component={RedirectPage} />
<Route
exact
path={`${url}/login`}
render={() => <RedirectPage redirectUri={redirectUri} />}
/>
</PrivateRoute>
</Switch>
)
Expand Down
12 changes: 8 additions & 4 deletions apps/polyflix/src/modules/authentication/pages/Mock.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>(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)
})
}

Expand Down Expand Up @@ -47,7 +51,7 @@ export function MockAuthenticationPage() {
<Button
sx={{ mr: 3 }}
variant="contained"
onClick={() => login(user as unknown as User)}
onClick={() => login(user as unknown as User, redirectUri)}
key={user.id}
>
Use app as {user.username}
Expand Down
20 changes: 17 additions & 3 deletions apps/polyflix/src/modules/authentication/pages/Redirect.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import { useInjection } from '@polyflix/di'
import { LoadingLayout } from '@core/layouts/Loading/Loading.layout'

import { AuthService } from '@auth/services/auth.service'
import { KeycloakLoginOptions } from 'keycloak-js'

interface Props {
redirectUri: string | undefined
}

/**
* The page we land on when keycloak auth is successful
*/
export const RedirectPage = () => {
export const RedirectPage = ({ redirectUri: redirectUri }: Props) => {
const authService = useInjection<AuthService>(AuthService)

const [isFetching, setIsFetching] = useState<boolean>(true)
Expand All @@ -28,9 +33,18 @@ export const RedirectPage = () => {
}
}, [keycloak])

if (!isKeycloakAuthenticated) keycloak.login()
const loginOptions: KeycloakLoginOptions = {
redirectUri,
}
if (!isKeycloakAuthenticated) {
if (redirectUri) {
keycloak.login(loginOptions)
} else {
keycloak.login()
}
}

if (!initialized || isFetching) return <LoadingLayout />

return <Redirect to="/" />
return <Redirect to={redirectUri ?? '/'} />
}

0 comments on commit 4690717

Please sign in to comment.