load user, if valid token exists
This commit is contained in:
parent
80fd112658
commit
31dbda57df
14
src/App.js
14
src/App.js
@ -1,10 +1,11 @@
|
|||||||
import React from 'react';
|
import React, { Component } from 'react';
|
||||||
|
|
||||||
import { BrowserRouter as Router } from 'react-router-dom';
|
import { BrowserRouter as Router } from 'react-router-dom';
|
||||||
import { createBrowserHistory } from "history";
|
import { createBrowserHistory } from "history";
|
||||||
|
|
||||||
import { Provider } from 'react-redux';
|
import { Provider } from 'react-redux';
|
||||||
import store from './store';
|
import store from './store';
|
||||||
|
import { loadUser } from './actions/authActions';
|
||||||
|
|
||||||
import './App.css';
|
import './App.css';
|
||||||
|
|
||||||
@ -27,10 +28,14 @@ const theme = createMuiTheme({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
class App extends Component {
|
||||||
|
|
||||||
|
componentDidMount(){
|
||||||
|
store.dispatch(loadUser());
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
const customHistory = createBrowserHistory();
|
const customHistory = createBrowserHistory();
|
||||||
|
|
||||||
|
|
||||||
function App() {
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider theme={theme}>
|
<ThemeProvider theme={theme}>
|
||||||
<Provider store={store}>
|
<Provider store={store}>
|
||||||
@ -46,5 +51,6 @@ function App() {
|
|||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
@ -4,36 +4,36 @@ import axios from 'axios';
|
|||||||
import { returnErrors, returnSuccess } from './messageActions'
|
import { returnErrors, returnSuccess } from './messageActions'
|
||||||
|
|
||||||
|
|
||||||
// // Check token & load user
|
// Check token & load user
|
||||||
// export const loadUser = () => (dispatch) => {
|
export const loadUser = () => (dispatch) => {
|
||||||
// // user loading
|
// user loading
|
||||||
// dispatch({
|
dispatch({
|
||||||
// type: USER_LOADING
|
type: USER_LOADING
|
||||||
// });
|
});
|
||||||
// const config = {
|
const config = {
|
||||||
// success: res => {
|
success: res => {
|
||||||
// dispatch({
|
dispatch({
|
||||||
// type: USER_LOADED,
|
type: USER_LOADED,
|
||||||
// payload: res.data.user
|
payload: res.data.user
|
||||||
// });
|
});
|
||||||
// },
|
},
|
||||||
// error: err => {
|
error: err => {
|
||||||
// if(err.response){
|
if(err.response){
|
||||||
// dispatch(returnErrors(err.response.data.message, err.response.status));
|
dispatch(returnErrors(err.response.data.message, err.response.status));
|
||||||
// }
|
}
|
||||||
// dispatch({
|
dispatch({
|
||||||
// type: AUTH_ERROR
|
type: AUTH_ERROR
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// axios.get('/api/v1/user/me', config, dispatch(authInterceptor()))
|
axios.get(`${process.env.REACT_APP_BLOCKLY_API}/user`, config, dispatch(authInterceptor()))
|
||||||
// .then(res => {
|
.then(res => {
|
||||||
// res.config.success(res);
|
res.config.success(res);
|
||||||
// })
|
})
|
||||||
// .catch(err => {
|
.catch(err => {
|
||||||
// err.config.error(err);
|
err.config.error(err);
|
||||||
// });
|
});
|
||||||
// };
|
};
|
||||||
|
|
||||||
|
|
||||||
var logoutTimerId;
|
var logoutTimerId;
|
||||||
@ -49,7 +49,7 @@ export const login = ({ email, password }) => (dispatch) => {
|
|||||||
};
|
};
|
||||||
// Request Body
|
// Request Body
|
||||||
const body = JSON.stringify({ email, password });
|
const body = JSON.stringify({ email, password });
|
||||||
axios.post('https://api.opensensemap.org/users/sign-in', body, config)
|
axios.post(`${process.env.REACT_APP_BLOCKLY_API}/user`, body, config)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
// Logout automatically if refreshToken "expired"
|
// Logout automatically if refreshToken "expired"
|
||||||
const logoutTimer = () => setTimeout(
|
const logoutTimer = () => setTimeout(
|
||||||
@ -92,7 +92,7 @@ export const logout = () => (dispatch) => {
|
|||||||
clearTimeout(logoutTimerId);
|
clearTimeout(logoutTimerId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
axios.post('https://api.opensensemap.org/users/sign-out', {}, config, dispatch(authInterceptor()))
|
axios.post('https://api.opensensemap.org/users/sign-out', {}, config)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
res.config.success(res);
|
res.config.success(res);
|
||||||
})
|
})
|
||||||
@ -140,7 +140,7 @@ export const authInterceptor = () => (dispatch, getState) => {
|
|||||||
originalRequest._retry = true;
|
originalRequest._retry = true;
|
||||||
const refreshToken = getState().auth.refreshToken;
|
const refreshToken = getState().auth.refreshToken;
|
||||||
// request to refresh the token, in request-body is the refreshToken
|
// request to refresh the token, in request-body is the refreshToken
|
||||||
axios.post('/api/v1/user/token/refresh', {"refreshToken": refreshToken})
|
axios.post('https://api.opensensemap.org/users/refresh-auth', {"token": refreshToken})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
clearTimeout(logoutTimerId);
|
clearTimeout(logoutTimerId);
|
||||||
|
@ -30,7 +30,7 @@ export default function(state = initialState, action){
|
|||||||
console.log(action.payload);
|
console.log(action.payload);
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
user: action.payload.data.user,
|
user: action.payload.user,
|
||||||
token: action.payload.token,
|
token: action.payload.token,
|
||||||
refreshToken: action.payload.refreshToken,
|
refreshToken: action.payload.refreshToken,
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user