Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UTOPIA-1429] Access link redirects to PIA list #1574

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/frontend/src/components/common/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useFetchKeycloakUserInfo } from '../../../hooks/userFetchKeycloakUserIn
import { AuthContext } from '../../../hooks/useAuth';
import {
logMeOut,
login,
ConfigStorageKeys,
isAuthenticated,
storeAuthTokens,
Expand Down Expand Up @@ -98,10 +99,6 @@ function Header() {
}
}, [accessToken, keycloakUserDetail, userInfoError]);

const login = () => {
win.location = API_ROUTES.KEYCLOAK_LOGIN;
};

const logout = async () => {
setAuthenticated(false);

Expand Down
15 changes: 8 additions & 7 deletions src/frontend/src/components/common/Unauthorized/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { Link } from 'react-router-dom';
import UnAuthorizedImg from '../../../assets/401.svg';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faUser } from '@fortawesome/free-solid-svg-icons';
import { API_ROUTES } from '../../../constant/apiRoutes';
import { TokenStorageKeys, login } from '../../../utils/auth';
import { AppStorage } from '../../../utils/storage';

const Unauthorized = () => {
const win: Window = window;

const login = () => {
win.location = API_ROUTES.KEYCLOAK_LOGIN;
};

const redirect = AppStorage.getItem(TokenStorageKeys.POST_LOGIN_REDIRECT_URL);
return (
<div className="bcgovPageContainer notfound-container">
<div className="container">
Expand All @@ -30,6 +26,11 @@ const Unauthorized = () => {
You do not have permission to view this page.
</p>
</div>
<div className="row ">
<p className="d-flex justify-content-center">
After login you will be redirected to {redirect}
</p>
</div>
<div className="row d-flex justify-content-center gap-3">
<button
className="bcgovbtn bcgovbtn__primary"
Expand Down
9 changes: 1 addition & 8 deletions src/frontend/src/pages/LandingPage/LandingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@ import problem from '../../assets/public_homepage/problem.svg';
import Callout from '../../components/common/Callout';
import { Link } from 'react-router-dom';
import hero from '../../assets/public_homepage/hero.svg';
import { API_ROUTES } from '../../constant/apiRoutes';
import { isAuthenticated } from '../../utils/auth';
import { isAuthenticated, login } from '../../utils/auth';

function LandingPage() {
// https://github.com/microsoft/TypeScript/issues/48949
// workaround
const win: Window = window;
const login = () => {
win.location = API_ROUTES.KEYCLOAK_LOGIN;
};
return (
<div>
<section className="hero-section wrapper">
Expand Down
11 changes: 11 additions & 0 deletions src/frontend/src/pages/PIAActiveListPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { AppStorage } from '../../utils/storage';
import ListViewRender from '../../components/public/ListViewRender';
import { TokenStorageKeys } from '../../utils/auth';

const PIAActiveList = () => {
const freshLoginState = AppStorage.getItem(
TokenStorageKeys.FRESH_LOGIN_STATE,
);
const redirect = AppStorage.getItem(TokenStorageKeys.POST_LOGIN_REDIRECT_URL);
BradyMitch marked this conversation as resolved.
Show resolved Hide resolved
if (freshLoginState && redirect) {
AppStorage.setItem(TokenStorageKeys.FRESH_LOGIN_STATE, false);
window.location.href = redirect;
}

return <ListViewRender showCompleted={false} title={`Active PIA's`} />;
};

Expand Down
44 changes: 41 additions & 3 deletions src/frontend/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export enum TokenStorageKeys {
TOKENS_LAST_REFRESHED_AT = 'tokensLastRefreshedAt',
LAST_ACTIVITY_AT = 'lastActivityAt',
IS_AUTO_LOGOUT_WARNING_POPUP_OPEN = 'isAutoLogoutWarningPopupOpen',
POST_LOGIN_REDIRECT_URL = 'postLoginRedirectUrl',
FRESH_LOGIN_STATE = 'freshLoginState',
}

export enum ConfigStorageKeys {
Expand All @@ -33,6 +35,23 @@ export const isAuthenticated = () => {
return true;
}

// Set post login redirect url
const freshLoginState = AppStorage.getItem(
TokenStorageKeys.FRESH_LOGIN_STATE,
);
if (
!freshLoginState &&
window.location.pathname !== '' &&
window.location.pathname !== '/' &&
window.location.pathname !== '/not-authorized' &&
window.location.pathname !== 'pia/list' &&
!window.location.pathname.includes('pia-list?session_state')
)
AppStorage.setItem(
TokenStorageKeys.POST_LOGIN_REDIRECT_URL,
window.location.href,
);

return false;
};

Expand Down Expand Up @@ -74,10 +93,24 @@ export const getAuthTokens = () => {
};
};

export const login = () => {
AppStorage.setItem(TokenStorageKeys.FRESH_LOGIN_STATE, true);
window.location.href = API_ROUTES.KEYCLOAK_LOGIN;
};

export const logMeOut = async (unauthorized = false, cb?: () => void) => {
const baseUrl = `${window.location.protocol}//${window.location.host}`;
const unauthRedirectUrl = `${baseUrl}/not-authorized`;
const loginRedirectUrl = `${baseUrl}/`;
const unauthRedirectUrl = `${window.location.origin}/not-authorized`;
const loginRedirectUrl = `${window.location.origin}/`;

// Set post login redirect url
let postLoginRedirectUrl;
if (
window.location.pathname !== '' &&
window.location.pathname !== '/' &&
window.location.pathname !== '/not-authorized' &&
window.location.pathname !== 'pia/list'
)
postLoginRedirectUrl = window.location.href;

const redirectUrl = unauthorized ? unauthRedirectUrl : loginRedirectUrl;

Expand All @@ -103,6 +136,11 @@ export const logMeOut = async (unauthorized = false, cb?: () => void) => {

clearStorage();

AppStorage.setItem(
TokenStorageKeys.POST_LOGIN_REDIRECT_URL,
postLoginRedirectUrl,
);

cb?.();
};

Expand Down
Loading