-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/WG-10: Add projects and user queries. (#137)
* Add login/logout and protected routes * Refactor * Fix tests * Fix linting * Fix linting * Add projects and user queries. * Fix bug. * Fix test. * Add prettier. * Update authUtils.ts * Update react/src/redux/api/geoapi.ts * Add link to related jira issue * Fix prettier issue --------- Co-authored-by: Nathan Franklin <[email protected]>
- Loading branch information
1 parent
8eb641e
commit fbe4f81
Showing
13 changed files
with
130 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import MainMenu from './MainMenu'; | ||
import { Provider } from 'react-redux'; | ||
import store from '../../redux/store'; | ||
|
||
test('renders menu', () => { | ||
const { getByText } = render(<MainMenu />); | ||
const { getByText } = render( | ||
<Provider store={store}> | ||
<MainMenu /> | ||
</Provider> | ||
); | ||
expect(getByText(/Main Menu/)).toBeDefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { geoapi } from './api/geoapi'; | ||
|
||
const slice = createSlice({ | ||
name: 'projects', | ||
initialState: { projects: [] }, | ||
reducers: {}, | ||
extraReducers: (builder) => { | ||
builder.addMatcher( | ||
geoapi.endpoints.getGeoapiProjects.matchFulfilled, | ||
(state, { payload }) => { | ||
state.projects = payload; | ||
} | ||
); | ||
}, | ||
}); | ||
|
||
export default slice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { combineReducers } from 'redux'; | ||
import { geoapi } from '../api/geoapi'; | ||
import authReducer from '../authSlice'; | ||
import projectsReducer from '../projectsSlice'; | ||
|
||
export const reducer = combineReducers({ | ||
auth: authReducer, | ||
projects: projectsReducer, | ||
[geoapi.reducerPath]: geoapi.reducer, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { configureStore } from '@reduxjs/toolkit'; | ||
import { reducer } from './reducers/reducers'; | ||
import { geoapi } from './api/geoapi'; | ||
|
||
const store = configureStore({ | ||
reducer: reducer, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware({ serializableCheck: false }).concat( | ||
geoapi.middleware | ||
), | ||
}); | ||
|
||
export type RootState = ReturnType<typeof store.getState>; | ||
export type AppDispatch = typeof store.dispatch; | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface AuthenticatedUser { | ||
username: string | null; | ||
email: string | null; | ||
} | ||
|
||
export interface AuthToken { | ||
token: string | null; | ||
expires: number | null; | ||
} | ||
|
||
export interface AuthState { | ||
user: AuthenticatedUser | null; | ||
token: AuthToken | null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,37 @@ | ||
import { AuthState } from '../redux/authSlice'; | ||
import { AuthToken } from '../types'; | ||
|
||
export const AUTH_KEY = 'auth'; | ||
|
||
export function isTokenValid(auth: AuthState): boolean { | ||
if (!auth.expires) { | ||
export function isTokenValid(token: AuthToken | null): boolean { | ||
if (token) { | ||
if (!token.expires) { | ||
return false; | ||
} | ||
|
||
const now = Date.now(); | ||
return now < token.expires; | ||
} else { | ||
return false; | ||
} | ||
|
||
const now = Date.now(); | ||
return now < auth.expires; | ||
} | ||
|
||
export function getAuthFromLocalStorage(): AuthState { | ||
export function getTokenFromLocalStorage(): AuthToken { | ||
try { | ||
const tokenStr = localStorage.getItem(AUTH_KEY); | ||
if (tokenStr) { | ||
const auth = JSON.parse(tokenStr); | ||
return { token: auth.token, expires: auth.expires }; | ||
return auth; | ||
} | ||
} catch (e: any) { | ||
console.error('Error loading state from localStorage:', e); | ||
} | ||
return { token: null, expires: null }; | ||
} | ||
|
||
export function setAuthToLocalStorage(auth: AuthState) { | ||
localStorage.setItem(AUTH_KEY, JSON.stringify(auth)); | ||
export function setTokenToLocalStorage(token: AuthToken) { | ||
localStorage.setItem(AUTH_KEY, JSON.stringify(token)); | ||
} | ||
|
||
export function removeAuthFromLocalStorage() { | ||
export function removeTokenFromLocalStorage() { | ||
localStorage.removeItem(AUTH_KEY); | ||
} |