-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.js
More file actions
32 lines (30 loc) · 874 Bytes
/
Copy pathcommon.js
File metadata and controls
32 lines (30 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { API_ROUTES } from '../utils/constants';
import axios from 'axios';
export function storeTokenInLocalStorage(token) {
localStorage.setItem('token', token);
}
export function getTokenFromLocalStorage() {
return localStorage.getItem('token');
}
export async function getAuthenticatedUser() {
const defaultReturnObject = { authenticated: false, user: null };
try {
const token = getTokenFromLocalStorage();
if (!token) {
return defaultReturnObject;
}
const response = await axios({
method: 'GET',
url: API_ROUTES.GET_USER,
headers: {
Authorization: `Bearer ${token}`
}
});
const { authenticated = false } = response.data;
return authenticated ? response.data : false;
}
catch (err) {
console.log('getAuthenticatedUser, Something Went Wrong', err);
return defaultReturnObject;
}
}