load user, if valid token exists
This commit is contained in:
parent
80fd112658
commit
31dbda57df
40
src/App.js
40
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 { createBrowserHistory } from "history";
|
||||
|
||||
import { Provider } from 'react-redux';
|
||||
import store from './store';
|
||||
import { loadUser } from './actions/authActions';
|
||||
|
||||
import './App.css';
|
||||
|
||||
@ -27,24 +28,29 @@ const theme = createMuiTheme({
|
||||
}
|
||||
});
|
||||
|
||||
const customHistory = createBrowserHistory();
|
||||
class App extends Component {
|
||||
|
||||
componentDidMount(){
|
||||
store.dispatch(loadUser());
|
||||
}
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<ThemeProvider theme={theme}>
|
||||
<Provider store={store}>
|
||||
<Router history={customHistory}>
|
||||
<div className="wrapper">
|
||||
<Navbar />
|
||||
<Routes />
|
||||
<Cookies />
|
||||
<Footer />
|
||||
</div>
|
||||
</Router>
|
||||
</Provider>
|
||||
</ThemeProvider>
|
||||
);
|
||||
render() {
|
||||
const customHistory = createBrowserHistory();
|
||||
return (
|
||||
<ThemeProvider theme={theme}>
|
||||
<Provider store={store}>
|
||||
<Router history={customHistory}>
|
||||
<div className="wrapper">
|
||||
<Navbar />
|
||||
<Routes />
|
||||
<Cookies />
|
||||
<Footer />
|
||||
</div>
|
||||
</Router>
|
||||
</Provider>
|
||||
</ThemeProvider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
|
@ -4,36 +4,36 @@ import axios from 'axios';
|
||||
import { returnErrors, returnSuccess } from './messageActions'
|
||||
|
||||
|
||||
// // Check token & load user
|
||||
// export const loadUser = () => (dispatch) => {
|
||||
// // user loading
|
||||
// dispatch({
|
||||
// type: USER_LOADING
|
||||
// });
|
||||
// const config = {
|
||||
// success: res => {
|
||||
// dispatch({
|
||||
// type: USER_LOADED,
|
||||
// payload: res.data.user
|
||||
// });
|
||||
// },
|
||||
// error: err => {
|
||||
// if(err.response){
|
||||
// dispatch(returnErrors(err.response.data.message, err.response.status));
|
||||
// }
|
||||
// dispatch({
|
||||
// type: AUTH_ERROR
|
||||
// });
|
||||
// }
|
||||
// };
|
||||
// axios.get('/api/v1/user/me', config, dispatch(authInterceptor()))
|
||||
// .then(res => {
|
||||
// res.config.success(res);
|
||||
// })
|
||||
// .catch(err => {
|
||||
// err.config.error(err);
|
||||
// });
|
||||
// };
|
||||
// Check token & load user
|
||||
export const loadUser = () => (dispatch) => {
|
||||
// user loading
|
||||
dispatch({
|
||||
type: USER_LOADING
|
||||
});
|
||||
const config = {
|
||||
success: res => {
|
||||
dispatch({
|
||||
type: USER_LOADED,
|
||||
payload: res.data.user
|
||||
});
|
||||
},
|
||||
error: err => {
|
||||
if(err.response){
|
||||
dispatch(returnErrors(err.response.data.message, err.response.status));
|
||||
}
|
||||
dispatch({
|
||||
type: AUTH_ERROR
|
||||
});
|
||||
}
|
||||
};
|
||||
axios.get(`${process.env.REACT_APP_BLOCKLY_API}/user`, config, dispatch(authInterceptor()))
|
||||
.then(res => {
|
||||
res.config.success(res);
|
||||
})
|
||||
.catch(err => {
|
||||
err.config.error(err);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
var logoutTimerId;
|
||||
@ -49,7 +49,7 @@ export const login = ({ email, password }) => (dispatch) => {
|
||||
};
|
||||
// Request Body
|
||||
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 => {
|
||||
// Logout automatically if refreshToken "expired"
|
||||
const logoutTimer = () => setTimeout(
|
||||
@ -92,7 +92,7 @@ export const logout = () => (dispatch) => {
|
||||
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 => {
|
||||
res.config.success(res);
|
||||
})
|
||||
@ -140,7 +140,7 @@ export const authInterceptor = () => (dispatch, getState) => {
|
||||
originalRequest._retry = true;
|
||||
const refreshToken = getState().auth.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 => {
|
||||
if (res.status === 200) {
|
||||
clearTimeout(logoutTimerId);
|
||||
|
@ -30,7 +30,7 @@ export default function(state = initialState, action){
|
||||
console.log(action.payload);
|
||||
return {
|
||||
...state,
|
||||
user: action.payload.data.user,
|
||||
user: action.payload.user,
|
||||
token: action.payload.token,
|
||||
refreshToken: action.payload.refreshToken,
|
||||
isAuthenticated: true,
|
||||
|
Loading…
x
Reference in New Issue
Block a user