-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement authorization using DRF-JWT.
- Loading branch information
1 parent
52117c7
commit 0f9946c
Showing
12 changed files
with
167 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import axios from 'axios' | ||
import jwtDecode from 'jwt-decode' | ||
|
||
import { LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE } from '../constants/ActionTypes' | ||
|
||
export const loginRequest = (username) => { | ||
return { | ||
type: LOGIN_REQUEST, | ||
payload: username | ||
} | ||
} | ||
|
||
export const loginSuccess = (user) => { | ||
return { | ||
type: LOGIN_SUCCESS, | ||
payload: user | ||
} | ||
} | ||
|
||
export const loginFailure = (error) => { | ||
return { | ||
type: LOGIN_FAILURE, | ||
payload: error | ||
} | ||
} | ||
|
||
export const setAuthorizationToken = (token) => { | ||
if (token) { | ||
axios.defaults.headers.common['Authorization'] = `JWT ${token}` | ||
} else { | ||
delete axios.defaults.headers.common['Authorization'] | ||
} | ||
} | ||
|
||
export const login = (username, password) => { | ||
return dispatch => { | ||
dispatch(loginRequest(username)) | ||
axios.post('/api-token-auth/', { username, password }) | ||
.then((res) => { | ||
let token = res.data.token | ||
localStorage.setItem('jwtToken', token) | ||
setAuthorizationToken(token) | ||
dispatch(loginSuccess(jwtDecode(token))) | ||
}) | ||
.catch((err) => { | ||
dispatch(loginFailure(err)) | ||
}) | ||
} | ||
} | ||
|
||
export const logout = () => { | ||
return dispatch => { | ||
localStorage.removeItem('jwtToken') | ||
setAuthorizationToken(false) | ||
dispatch(loginSuccess({})) | ||
} | ||
} | ||
|
||
export const renewAuthorizationToken = (token) => { | ||
return dispatch => { | ||
axios.post('/api-token-refresh/', { token }) | ||
} | ||
} | ||
|
||
export const checkAuthorizationToken = (token) => { | ||
return dispatch => { | ||
axios.post('/api-token-verify/', { token }) | ||
.then((res) => { | ||
dispatch(renewAuthorizationToken(res.data.token)) | ||
}) | ||
.catch((err) => { | ||
if (err.response.status === 400 && err.response.data.non_field_errors[0] === 'Signature has expired.') { | ||
dispatch(logout()) | ||
} | ||
}) | ||
} | ||
} |
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,24 @@ | ||
import _ from 'lodash' | ||
|
||
import { LOGIN_SUCCESS } from '../constants/ActionTypes' | ||
|
||
const initialState = { | ||
isAuthenticated: false, | ||
user: {} | ||
} | ||
|
||
const auth = (state=initialState, action={}) => { | ||
switch (action.type) { | ||
case LOGIN_SUCCESS: | ||
return { | ||
isAuthenticated: !_.isEmpty(action.payload), | ||
user: action.payload | ||
} | ||
|
||
default: | ||
return state | ||
} | ||
} | ||
|
||
|
||
export default auth |
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