mirror of
https://git.rwth-aachen.de/acs/public/villas/web/
synced 2025-03-09 00:00:01 +01:00
Fix user login
This commit is contained in:
parent
cbc413232c
commit
0e655f2613
3 changed files with 33 additions and 16 deletions
|
@ -95,6 +95,13 @@ class App extends Component {
|
|||
}
|
||||
|
||||
componentWillUpdate(nextProps, nextState) {
|
||||
// check if user is still logged in
|
||||
if (UserStore.getState().token == null) {
|
||||
this.props.router.push('/login');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// open connection to each required simulator
|
||||
const requiredSimulators = this.requiredSimulatorsBySimulations();
|
||||
|
||||
|
|
|
@ -32,22 +32,24 @@ class Login extends Component {
|
|||
|
||||
componentWillUpdate(nextProps, nextState) {
|
||||
// if token stored locally, request user
|
||||
const token = localStorage.getItem('token');
|
||||
if (nextState.token == null) {
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
if (token != null && token !== '') {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'users/logged-in',
|
||||
token: token
|
||||
});
|
||||
}
|
||||
if (token != null && token !== '' && nextState.currentUser == null) {
|
||||
AppDispatcher.dispatch({
|
||||
type: 'users/logged-in',
|
||||
token: token
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// check if logged in
|
||||
if (nextState.currentUser != null) {
|
||||
// save login in local storage
|
||||
localStorage.setItem('token', nextState.token);
|
||||
|
||||
// check if logged in
|
||||
if (nextState.currentUser != null) {
|
||||
// save login in local storage
|
||||
localStorage.setItem('token', this.state.token);
|
||||
|
||||
// transition to index
|
||||
this.props.router.push('/');
|
||||
// transition to index
|
||||
nextProps.router.push('/');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,8 @@ class UserStore extends ReduceStore {
|
|||
}
|
||||
|
||||
reduce(state, action) {
|
||||
console.log(action.type);
|
||||
|
||||
switch (action.type) {
|
||||
case 'users/login':
|
||||
UsersDataManager.login(action.username, action.password);
|
||||
|
@ -37,13 +39,19 @@ class UserStore extends ReduceStore {
|
|||
|
||||
case 'users/logged-in':
|
||||
// request logged-in user data
|
||||
console.log(action.token);
|
||||
UsersDataManager.getCurrentUser(action.token);
|
||||
|
||||
return { token: action.token };
|
||||
return Object.assign({}, state, { token: action.token });
|
||||
|
||||
case 'users/current-user':
|
||||
// save logged-in user
|
||||
return { currentUser: action.user };
|
||||
return Object.assign({}, state, { currentUser: action.user });
|
||||
|
||||
case 'users/current-user-error':
|
||||
// discard user token
|
||||
//return { currentUser: null, token: null };
|
||||
return state;
|
||||
|
||||
case 'users/login-error':
|
||||
console.log(action);
|
||||
|
|
Loading…
Add table
Reference in a new issue