commit 456081d2c53364876175e8d5363b87a62397094e
parent 2b240cde465de4c6cac727f247c549076cfad1f2
Author: tanhengyeow <E0032242@u.nus.edu>
Date: Fri, 5 Jun 2020 01:15:28 +0800
Add route history functionaltiy and auth actions
Diffstat:
3 files changed, 89 insertions(+), 0 deletions(-)
diff --git a/frontend/src/actions/auth.tsx b/frontend/src/actions/auth.tsx
@@ -0,0 +1,73 @@
+/* eslint-disable @typescript-eslint/no-explicit-any */
+import { ThunkDispatch as Dispatch } from 'redux-thunk';
+import { Base64 } from 'js-base64';
+import * as constants from '../constants';
+
+export interface Authenticate {
+ type: constants.AUTHENTICATE;
+}
+const authenticate = (): Authenticate => {
+ return {
+ type: constants.AUTHENTICATE,
+ };
+};
+
+export interface Unauthenticate {
+ type: constants.UNAUTHENTICATE;
+}
+const unauthenticate = (): Unauthenticate => {
+ return {
+ type: constants.UNAUTHENTICATE,
+ };
+};
+
+export type AuthenticationAction = Authenticate | Unauthenticate;
+
+export const login = (nexusURL: string, username: string, password: string) => {
+ return async (dispatch: Dispatch<AuthenticationAction, {}, any>) => {
+ if (nexusURL && username && password) {
+ await fetch(`/user`, {
+ headers: new Headers({
+ Authorization: `Basic ${Base64.encode(`${username}:${password}`)}`,
+ }),
+ })
+ .then((response) => {
+ if (response.ok) {
+ return response.json();
+ }
+ throw new Error('Error connecting to server');
+ })
+ .then(async () => {
+ await window.localStorage.setItem('authenticated', 'true');
+ await window.localStorage.setItem(
+ 'authHeader',
+ `${Base64.encode(`${username}:${password}`)}`
+ );
+ dispatch(authenticate());
+ })
+ .catch((err) => {
+ console.log(err);
+ });
+ }
+ };
+};
+export const logout = () => {
+ return async (dispatch: Dispatch<AuthenticationAction, {}, any>) => {
+ await window.localStorage.setItem('authenticated', 'false');
+ await window.localStorage.setItem('authHeader', '');
+ dispatch(unauthenticate());
+ };
+};
+
+export const checkAuthentication = () => {
+ return async (dispatch: Dispatch<AuthenticationAction, {}, any>) => {
+ const auth = await window.localStorage.getItem('authenticated');
+ const formattedAuth = typeof auth === 'string' ? JSON.parse(auth) : null;
+
+ if (formattedAuth) {
+ dispatch(authenticate());
+ } else {
+ dispatch(unauthenticate());
+ }
+ };
+};
diff --git a/frontend/src/history.tsx b/frontend/src/history.tsx
@@ -0,0 +1,12 @@
+/* eslint-disable @typescript-eslint/no-explicit-any */
+import { createBrowserHistory } from 'history';
+
+declare global {
+ interface Window {
+ dataLayer: any;
+ }
+}
+
+const history = createBrowserHistory();
+
+export default history;
diff --git a/frontend/src/import-png.d.ts b/frontend/src/import-png.d.ts
@@ -0,0 +1,4 @@
+declare module '*.png' {
+ const value: any;
+ export default value;
+}