mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-29 00:10:54 +05:30
Суппорт jwt на фронте
This commit is contained in:
parent
404684b8d9
commit
14d2d8eac4
@ -1,6 +1,6 @@
|
|||||||
import { routeActions } from 'react-router-redux';
|
import { routeActions } from 'react-router-redux';
|
||||||
|
|
||||||
import { updateUser, logout as logoutUser } from 'components/user/actions';
|
import { updateUser, logout as logoutUser, fetchUserData } from 'components/user/actions';
|
||||||
import request from 'services/request';
|
import request from 'services/request';
|
||||||
|
|
||||||
export function login({login = '', password = '', rememberMe = false}) {
|
export function login({login = '', password = '', rememberMe = false}) {
|
||||||
@ -12,11 +12,15 @@ export function login({login = '', password = '', rememberMe = false}) {
|
|||||||
'/api/authentication/login',
|
'/api/authentication/login',
|
||||||
{login, password, rememberMe}
|
{login, password, rememberMe}
|
||||||
)
|
)
|
||||||
.then(() => {
|
.then((resp) => {
|
||||||
dispatch(updateUser({
|
dispatch(updateUser({
|
||||||
isGuest: false
|
isGuest: false,
|
||||||
|
token: resp.jwt
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
request.setAuthToken(resp.jwt);
|
||||||
|
dispatch(fetchUserData());
|
||||||
|
|
||||||
dispatch(redirectToGoal());
|
dispatch(redirectToGoal());
|
||||||
})
|
})
|
||||||
.catch((resp) => {
|
.catch((resp) => {
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import request from 'services/request';
|
||||||
|
|
||||||
export const UPDATE = 'USER_UPDATE';
|
export const UPDATE = 'USER_UPDATE';
|
||||||
/**
|
/**
|
||||||
* @param {string|Object} payload jwt token or user object
|
* @param {string|Object} payload jwt token or user object
|
||||||
@ -21,3 +23,24 @@ export function setUser(payload) {
|
|||||||
export function logout() {
|
export function logout() {
|
||||||
return setUser({isGuest: true});
|
return setUser({isGuest: true});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function fetchUserData() {
|
||||||
|
return (dispatch) =>
|
||||||
|
request.get('/api/users/current')
|
||||||
|
.then((resp) => {
|
||||||
|
dispatch(updateUser(resp));
|
||||||
|
})
|
||||||
|
.catch((resp) => {
|
||||||
|
/*
|
||||||
|
{
|
||||||
|
"name": "Unauthorized",
|
||||||
|
"message": "You are requesting with an invalid credential.",
|
||||||
|
"code": 0,
|
||||||
|
"status": 401,
|
||||||
|
"type": "yii\\web\\UnauthorizedHttpException"
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
console.log(resp);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -5,6 +5,9 @@ import RootPage from 'pages/root/RootPage';
|
|||||||
import IndexPage from 'pages/index/IndexPage';
|
import IndexPage from 'pages/index/IndexPage';
|
||||||
import AuthPage from 'pages/auth/AuthPage';
|
import AuthPage from 'pages/auth/AuthPage';
|
||||||
|
|
||||||
|
import request from 'services/request';
|
||||||
|
import { fetchUserData } from 'components/user/actions';
|
||||||
|
|
||||||
import OAuthInit from 'components/auth/OAuthInit';
|
import OAuthInit from 'components/auth/OAuthInit';
|
||||||
import Register from 'components/auth/Register';
|
import Register from 'components/auth/Register';
|
||||||
import Login from 'components/auth/Login';
|
import Login from 'components/auth/Login';
|
||||||
@ -38,6 +41,13 @@ export default function routesFactory(store) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const state = store.getState();
|
||||||
|
if (state.user.token) {
|
||||||
|
// authorizing user if it is possible
|
||||||
|
request.setAuthToken(state.user.token);
|
||||||
|
store.dispatch(fetchUserData());
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Route path="/" component={RootPage}>
|
<Route path="/" component={RootPage}>
|
||||||
<IndexRoute component={IndexPage} onEnter={checkAuth} />
|
<IndexRoute component={IndexPage} onEnter={checkAuth} />
|
||||||
|
@ -10,15 +10,27 @@ function serialize(data) {
|
|||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let authToken;
|
||||||
|
|
||||||
const toJSON = (resp) => resp.json();
|
const toJSON = (resp) => resp.json();
|
||||||
const handleResponse = (resp) => Promise[resp.success ? 'resolve' : 'reject'](resp);
|
// if resp.success does not exist - degradating to HTTP status codes
|
||||||
|
const handleResponse = (resp) => Promise[resp.success || typeof resp.success === 'undefined' ? 'resolve' : 'reject'](resp);
|
||||||
|
const getDefaultHeaders = () => {
|
||||||
|
const header = {Accept: 'application/json'};
|
||||||
|
|
||||||
|
if (authToken) {
|
||||||
|
header.Authorization = `Bearer ${authToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return header;
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
post(url, data) {
|
post(url, data) {
|
||||||
return fetch(url, {
|
return fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
...getDefaultHeaders(),
|
||||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||||
},
|
},
|
||||||
body: serialize(data)
|
body: serialize(data)
|
||||||
@ -27,6 +39,7 @@ export default {
|
|||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
;
|
;
|
||||||
},
|
},
|
||||||
|
|
||||||
get(url, data) {
|
get(url, data) {
|
||||||
if (typeof data === 'object') {
|
if (typeof data === 'object') {
|
||||||
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
||||||
@ -34,12 +47,14 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return fetch(url, {
|
return fetch(url, {
|
||||||
headers: {
|
headers: getDefaultHeaders()
|
||||||
Accept: 'application/json'
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.then(toJSON)
|
.then(toJSON)
|
||||||
.then(handleResponse)
|
.then(handleResponse)
|
||||||
;
|
;
|
||||||
|
},
|
||||||
|
|
||||||
|
setAuthToken(tkn) {
|
||||||
|
authToken = tkn;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user