-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { apiRequest } from '@department-of-veterans-affairs/platform-utilities/api'; | ||
|
||
let user = null; | ||
|
||
export async function fetchUser() { | ||
try { | ||
user = await apiRequest('/accredited_representative_portal/v0/user'); | ||
return user; | ||
} catch (error) { | ||
user = null; | ||
throw error; | ||
} | ||
} | ||
|
||
export function getUser() { | ||
return user; | ||
} | ||
|
||
export function isAuthenticated() { | ||
return user !== 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
5 changes: 5 additions & 0 deletions
5
src/applications/accredited-representative-portal/userContext.js
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,5 @@ | ||
import { createContext } from 'react'; | ||
|
||
const UserContext = createContext(null); | ||
|
||
export default UserContext; |
16 changes: 16 additions & 0 deletions
16
src/applications/accredited-representative-portal/userLoader.js
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,16 @@ | ||
import { redirect } from 'react-router'; | ||
import { fetchUser, isAuthenticated } from './auth'; | ||
import { SIGN_IN_URL } from './constants'; | ||
|
||
export async function userLoader() { | ||
try { | ||
const user = await fetchUser(); | ||
if (!isAuthenticated()) { | ||
redirect(SIGN_IN_URL); | ||
} | ||
return user; | ||
} catch (error) { | ||
redirect(SIGN_IN_URL); | ||
return null; | ||
} | ||
} |