forked from EpamLifeSciencesTeam/cmwl_pipeline_fe
-
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.
Merge pull request EpamLifeSciencesTeam#10 from AlekseiLitkovetc/add-…
…authentication-logic feat: Add authentication logic
- Loading branch information
Showing
8 changed files
with
138 additions
and
129 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,10 +1,45 @@ | ||
import axios from 'axios'; | ||
import { useDispatch } from "react-redux"; | ||
import { identity } from "./utils/FunctionUtils"; | ||
import { sessionExpired } from "./store/auth/actions"; | ||
|
||
const axiosCromwell = axios.create({ | ||
baseURL: 'http://localhost:3001/', | ||
baseURL: 'http://localhost:8080/', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
axiosCromwell.interceptors.request.use( | ||
config => { | ||
const accessToken = localStorage.getItem('accessToken'); | ||
if (accessToken) { | ||
config.headers['Authorization'] = accessToken; | ||
} | ||
return config; | ||
}, | ||
Promise.reject | ||
); | ||
|
||
axiosCromwell.interceptors.response.use( | ||
identity, | ||
async error => { | ||
const originalRequest = error.config; | ||
const refreshToken = localStorage.getItem('refreshToken'); | ||
if (refreshToken && error.response.status === 401 && !originalRequest._retry) { | ||
originalRequest._retry = true; | ||
const tokensResponse = await axiosCromwell.get('/auth/refresh', {params: {refreshToken}}) | ||
if (tokensResponse.status === 200) { | ||
localStorage.setItem('accessToken', tokensResponse.headers['access-token']); | ||
localStorage.setItem('refreshToken', tokensResponse.headers['refresh-token']); | ||
return axios(originalRequest); | ||
} | ||
} else if (refreshToken && error.response.status === 401 && originalRequest._retry) { | ||
const dispatch = useDispatch(); | ||
dispatch(sessionExpired); | ||
} | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default axiosCromwell; |
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,31 +1,55 @@ | ||
import { | ||
AUTH_AUTHENTICATE_USER, | ||
AUTH_SESSION_EXPIRED, | ||
AUTH_SIGN_IN_SUCCESS, | ||
AUTH_SIGN_OUT, | ||
AUTH_SIGN_UP_SUCCESS, | ||
AuthActionTypes, | ||
AuthHeaders, | ||
SignInData, | ||
SignUpData | ||
} from './types'; | ||
import { CmwlThunkAction } from '../index'; | ||
import axiosCromwell from '../../axiosCromwell'; | ||
import { AxiosResponse } from "axios"; | ||
|
||
const signInSuccess = (): AuthActionTypes => ({ | ||
type: AUTH_SIGN_IN_SUCCESS | ||
const signInSuccess = (authHeaders: AuthHeaders): AuthActionTypes => ({ | ||
type: AUTH_SIGN_IN_SUCCESS, | ||
authHeaders: authHeaders | ||
}); | ||
|
||
const signUpSuccess = (): AuthActionTypes => ({ | ||
type: AUTH_SIGN_UP_SUCCESS | ||
const signUpSuccess = (authHeaders: AuthHeaders): AuthActionTypes => ({ | ||
type: AUTH_SIGN_UP_SUCCESS, | ||
authHeaders: authHeaders | ||
}); | ||
|
||
export const signIn = (signInData: SignInData): CmwlThunkAction => (dispatch => { | ||
dispatch(signInSuccess()); | ||
// ToDo: Add axios async call | ||
axiosCromwell.post('/auth/signIn', signInData).then(response => { | ||
const authHeaders = getAuthHeaders(response); | ||
dispatch(signInSuccess(authHeaders)); | ||
}).catch(error => console.log(error)) | ||
}); | ||
|
||
export const signUp = (signUpData: SignUpData): CmwlThunkAction => (dispatch => { | ||
dispatch(signUpSuccess()); | ||
// ToDo: Add axios async call | ||
axiosCromwell.post('/auth/signUp', signUpData).then(response => { | ||
const authHeaders = getAuthHeaders(response); | ||
dispatch(signUpSuccess(authHeaders)); | ||
}).catch(error => console.log(error)) | ||
}); | ||
|
||
export const signOut = (): AuthActionTypes => ({ | ||
export const authenticateUser: AuthActionTypes = { | ||
type: AUTH_AUTHENTICATE_USER | ||
} | ||
|
||
export const signOut: AuthActionTypes = { | ||
type: AUTH_SIGN_OUT | ||
} | ||
|
||
export const sessionExpired: AuthActionTypes = { | ||
type: AUTH_SESSION_EXPIRED | ||
} | ||
|
||
const getAuthHeaders = (response: AxiosResponse): AuthHeaders => ({ | ||
accessToken: response.headers['access-token'], | ||
refreshToken: response.headers['refresh-token'] | ||
}); |
Oops, something went wrong.