From 31dbda57df33c78efbae2a4afc1e088b4b313c59 Mon Sep 17 00:00:00 2001
From: Delucse <46593742+Delucse@users.noreply.github.com>
Date: Thu, 3 Dec 2020 15:38:47 +0100
Subject: [PATCH] load user, if valid token exists
---
src/App.js | 40 ++++++++++++----------
src/actions/authActions.js | 66 ++++++++++++++++++-------------------
src/reducers/authReducer.js | 2 +-
3 files changed, 57 insertions(+), 51 deletions(-)
diff --git a/src/App.js b/src/App.js
index 6f21ed6..db72ff0 100644
--- a/src/App.js
+++ b/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 (
-
-
-
-
-
-
-
-
-
-
-
-
- );
+ render() {
+ const customHistory = createBrowserHistory();
+ return (
+
+
+
+
+
+
+
+
+
+
+
+
+ );
+ }
}
export default App;
diff --git a/src/actions/authActions.js b/src/actions/authActions.js
index 5b142af..c29f7cf 100644
--- a/src/actions/authActions.js
+++ b/src/actions/authActions.js
@@ -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);
diff --git a/src/reducers/authReducer.js b/src/reducers/authReducer.js
index cc48b84..653ecb0 100644
--- a/src/reducers/authReducer.js
+++ b/src/reducers/authReducer.js
@@ -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,