-
Notifications
You must be signed in to change notification settings - Fork 0
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
8 changed files
with
144 additions
and
116 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
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,33 @@ | ||
import jwtDecode from 'jwt-decode'; | ||
import { getAuthData } from './storage'; | ||
|
||
export type Role = 'ROLE_OPERATOR' | 'ROLE_ADMIN'; | ||
|
||
export type TokenData = { | ||
exp: number; | ||
user_name: string; | ||
authorities: Role[]; | ||
}; | ||
|
||
export const getTokenData = (): TokenData | undefined => { | ||
try { | ||
return jwtDecode(getAuthData().access_token); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
}; | ||
export const isAuthenticated = (): boolean => { | ||
const tokenData = getTokenData(); | ||
return tokenData && tokenData.exp * 1000 > Date.now() ? true : false; | ||
}; | ||
export const hasAnyRoles = (roles: Role[]): boolean => { | ||
if (roles.length === 0) { | ||
return true; | ||
} | ||
const tokenData = getTokenData(); | ||
|
||
if (tokenData !== undefined) { | ||
return roles.some((role) => tokenData.authorities.includes(role)); | ||
} | ||
return false; | ||
}; |
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,104 +1,65 @@ | ||
import qs from 'qs' | ||
import axios, { AxiosRequestConfig } from 'axios' | ||
import history from './history' | ||
import jwtDecode from 'jwt-decode'; | ||
import qs from 'qs'; | ||
import axios, { AxiosRequestConfig } from 'axios'; | ||
import history from './history'; | ||
import { getAuthData } from './storage'; | ||
|
||
export type Role = 'ROLE_OPERATOR' | 'ROLE_ADMIN'; | ||
export const BASE_URL = | ||
process.env.REACT_APP_BACKEND_URL ?? 'http://localhost:8080'; | ||
|
||
export type TokenData = { | ||
exp: number, | ||
user_name: string, | ||
authorities: Role[] | ||
} | ||
|
||
type LoginResponse = { | ||
"access_token": string, | ||
"token_type": string, | ||
"expires_in": string, | ||
"scope": string, | ||
"userFirstName": string, | ||
"userId": string | ||
} | ||
|
||
export const BASE_URL = process.env.REACT_APP_BACKEND_URL ?? 'http://localhost:8080' | ||
const tokenKey = 'authData' | ||
const CLIENT_ID = process.env.REACT_APP_CLIENT_ID ?? 'dscatalog' | ||
const CLIENT_SECRET = process.env.REACT_APP_CLIENT_SECRET ?? 'dscatalog123' | ||
const basicHeader = () => 'Basic ' + window.btoa(CLIENT_ID + ':' + CLIENT_SECRET) | ||
const CLIENT_ID = process.env.REACT_APP_CLIENT_ID ?? 'dscatalog'; | ||
const CLIENT_SECRET = process.env.REACT_APP_CLIENT_SECRET ?? 'dscatalog123'; | ||
const basicHeader = () => | ||
'Basic ' + window.btoa(CLIENT_ID + ':' + CLIENT_SECRET); | ||
type LoginData = { | ||
username: string | ||
password: string | ||
} | ||
export const requestBackendLogin = (loginData : LoginData) => { | ||
const headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: basicHeader() | ||
} | ||
const data = qs.stringify ({ | ||
...loginData, | ||
grant_type: 'password' | ||
}) | ||
return axios({method: 'POST', baseURL: BASE_URL, url: '/oauth/token', data, headers}) | ||
} | ||
|
||
export const saveAuthData = (obj : LoginResponse) => { | ||
localStorage.setItem(tokenKey, JSON.stringify(obj)) | ||
} | ||
|
||
export const getAuthData = () => { | ||
const str = localStorage.getItem(tokenKey) ?? "{}" | ||
return JSON.parse(str) as LoginResponse | ||
} | ||
|
||
export const removeAuthData = () => { | ||
localStorage.removeItem(tokenKey); | ||
} | ||
username: string; | ||
password: string; | ||
}; | ||
export const requestBackendLogin = (loginData: LoginData) => { | ||
const headers = { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
Authorization: basicHeader(), | ||
}; | ||
const data = qs.stringify({ | ||
...loginData, | ||
grant_type: 'password', | ||
}); | ||
return axios({ | ||
method: 'POST', | ||
baseURL: BASE_URL, | ||
url: '/oauth/token', | ||
data, | ||
headers, | ||
}); | ||
}; | ||
|
||
export const requestBackend = (config: AxiosRequestConfig) => { | ||
const headers = config.withCredentials ? { | ||
const headers = config.withCredentials | ||
? { | ||
...config.headers, | ||
Authorization: "Bearer " + getAuthData().access_token | ||
} : config.headers; | ||
return axios({...config, baseURL: BASE_URL, headers}) | ||
} | ||
Authorization: 'Bearer ' + getAuthData().access_token, | ||
} | ||
: config.headers; | ||
return axios({ ...config, baseURL: BASE_URL, headers }); | ||
}; | ||
|
||
// Add a request interceptor | ||
axios.interceptors.request.use(function (config) { | ||
axios.interceptors.request.use( | ||
function (config) { | ||
return config; | ||
}, function (error) { | ||
}, | ||
function (error) { | ||
return Promise.reject(error); | ||
}); | ||
} | ||
); | ||
|
||
axios.interceptors.response.use(function (response) { | ||
axios.interceptors.response.use( | ||
function (response) { | ||
return response; | ||
}, function (error) { | ||
}, | ||
function (error) { | ||
if (error.response.status === 401) { | ||
history.push('/admin/auth') | ||
history.push('/admin/auth'); | ||
} | ||
return Promise.reject(error); | ||
}); | ||
|
||
export const getTokenData = () : TokenData | undefined => { | ||
try { | ||
return jwtDecode(getAuthData().access_token); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
|
||
export const isAuthenticated = () : boolean => { | ||
const tokenData = getTokenData(); | ||
return (tokenData && tokenData.exp * 1000 > Date.now()) ? true : false; | ||
} | ||
|
||
export const hasAnyRoles = (roles: Role[]) : boolean => { | ||
if (roles.length === 0) { | ||
return true; | ||
} | ||
const tokenData = getTokenData(); | ||
|
||
if (tokenData !== undefined) { | ||
return roles.some(role => tokenData.authorities.includes(role)) | ||
} | ||
return false; | ||
} | ||
); |
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 @@ | ||
const tokenKey = 'authData'; | ||
type LoginResponse = { | ||
access_token: string; | ||
token_type: string; | ||
expires_in: string; | ||
scope: string; | ||
userFirstName: string; | ||
userId: string; | ||
}; | ||
export const saveAuthData = (obj: LoginResponse) => { | ||
localStorage.setItem(tokenKey, JSON.stringify(obj)); | ||
}; | ||
|
||
export const getAuthData = () => { | ||
const str = localStorage.getItem(tokenKey) ?? '{}'; | ||
return JSON.parse(str) as LoginResponse; | ||
}; | ||
|
||
export const removeAuthData = () => { | ||
localStorage.removeItem(tokenKey); | ||
}; |