Add login failed message
This commit is contained in:
parent
29e6421c8c
commit
d5b6b8d1c3
|
|
@ -51,6 +51,7 @@ class Layout extends Component {
|
||||||
loggedIn: CurrentUserStore.isLoggedIn(),
|
loggedIn: CurrentUserStore.isLoggedIn(),
|
||||||
admin: CurrentUserStore.isAdmin(),
|
admin: CurrentUserStore.isAdmin(),
|
||||||
name: CurrentUserStore.getName(),
|
name: CurrentUserStore.getName(),
|
||||||
|
loginFailed: CurrentUserStore.isLoginFailed(),
|
||||||
version: Layout.defaultVersion,
|
version: Layout.defaultVersion,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -77,6 +78,7 @@ class Layout extends Component {
|
||||||
...this.state,
|
...this.state,
|
||||||
loggedIn: CurrentUserStore.isLoggedIn(),
|
loggedIn: CurrentUserStore.isLoggedIn(),
|
||||||
admin: CurrentUserStore.isAdmin(),
|
admin: CurrentUserStore.isAdmin(),
|
||||||
|
loginFailed: CurrentUserStore.isLoginFailed(),
|
||||||
name: CurrentUserStore.getName(),
|
name: CurrentUserStore.getName(),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
@ -85,7 +87,7 @@ class Layout extends Component {
|
||||||
showSettings = () => this.setState({...this.state, showSettings: true});
|
showSettings = () => this.setState({...this.state, showSettings: true});
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {name, admin, version, loggedIn, showSettings} = this.state;
|
const {name, admin, version, loggedIn, showSettings, loginFailed} = this.state;
|
||||||
const {classes} = this.props;
|
const {classes} = this.props;
|
||||||
const theme = this.state.darkTheme ? darkTheme : lightTheme;
|
const theme = this.state.darkTheme ? darkTheme : lightTheme;
|
||||||
return (
|
return (
|
||||||
|
|
@ -102,7 +104,7 @@ class Layout extends Component {
|
||||||
<main className={classes.content}>
|
<main className={classes.content}>
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route exact path="/login" render={() =>
|
<Route exact path="/login" render={() =>
|
||||||
(loggedIn ? (<Redirect to="/"/>) : (<Login/>))}/>
|
(loggedIn ? (<Redirect to="/"/>) : (<Login loginFailed={loginFailed}/>))}/>
|
||||||
{(loggedIn || getToken() != null) ? null : <Redirect to="/login"/>}
|
{(loggedIn || getToken() != null) ? null : <Redirect to="/login"/>}
|
||||||
<Route exact path="/" component={Messages}/>
|
<Route exact path="/" component={Messages}/>
|
||||||
<Route exact path="/messages/:id" component={Messages}/>
|
<Route exact path="/messages/:id" component={Messages}/>
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ export function login(username, password) {
|
||||||
}).then(function(resp) {
|
}).then(function(resp) {
|
||||||
setAuthorizationToken(resp.data.token);
|
setAuthorizationToken(resp.data.token);
|
||||||
GlobalAction.initialLoad();
|
GlobalAction.initialLoad();
|
||||||
|
}).catch(() => {
|
||||||
|
dispatcher.dispatch({type: 'LOGIN_FAILED'});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,7 +63,7 @@ export function fetchUsers() {
|
||||||
* @param {int} id the user id
|
* @param {int} id the user id
|
||||||
*/
|
*/
|
||||||
export function deleteUser(id) {
|
export function deleteUser(id) {
|
||||||
axios.delete(config.get('url') + 'user/' + id).then(function(resp) {
|
axios.delete(config.get('url') + 'user/' + id).then(function() {
|
||||||
fetchUsers();
|
fetchUsers();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +75,7 @@ export function deleteUser(id) {
|
||||||
* @param {bool} admin if true, the user is an administrator
|
* @param {bool} admin if true, the user is an administrator
|
||||||
*/
|
*/
|
||||||
export function createUser(name, pass, admin) {
|
export function createUser(name, pass, admin) {
|
||||||
axios.post(config.get('url') + 'user', {name, pass, admin}).then(function(resp) {
|
axios.post(config.get('url') + 'user', {name, pass, admin}).then(function() {
|
||||||
fetchUsers();
|
fetchUsers();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +88,7 @@ export function createUser(name, pass, admin) {
|
||||||
* @param {bool} admin if true, the user is an administrator
|
* @param {bool} admin if true, the user is an administrator
|
||||||
*/
|
*/
|
||||||
export function updateUser(id, name, pass, admin) {
|
export function updateUser(id, name, pass, admin) {
|
||||||
axios.post(config.get('url') + 'user/' + id, {name, pass, admin}).then(function(resp) {
|
axios.post(config.get('url') + 'user/' + id, {name, pass, admin}).then(function() {
|
||||||
fetchUsers();
|
fetchUsers();
|
||||||
fetchCurrentUser(); // just in case update current user
|
fetchCurrentUser(); // just in case update current user
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ axios.interceptors.response.use(null, (error) => {
|
||||||
dispatcher.dispatch({type: 'REMOVE_CURRENT_USER'});
|
dispatcher.dispatch({type: 'REMOVE_CURRENT_USER'});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.warn('Error status', error.response.status);
|
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,17 @@ import React, {Component} from 'react';
|
||||||
import Button from 'material-ui/Button';
|
import Button from 'material-ui/Button';
|
||||||
import Grid from 'material-ui/Grid';
|
import Grid from 'material-ui/Grid';
|
||||||
import TextField from 'material-ui/TextField';
|
import TextField from 'material-ui/TextField';
|
||||||
|
import Typography from 'material-ui/Typography';
|
||||||
import Container from '../component/Container';
|
import Container from '../component/Container';
|
||||||
import * as UserAction from '../actions/UserAction';
|
import * as UserAction from '../actions/UserAction';
|
||||||
import DefaultPage from '../component/DefaultPage';
|
import DefaultPage from '../component/DefaultPage';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
class Login extends Component {
|
class Login extends Component {
|
||||||
|
static propTypes = {
|
||||||
|
loginFailed: PropTypes.bool.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.state = {username: '', password: ''};
|
this.state = {username: '', password: ''};
|
||||||
|
|
@ -22,7 +28,7 @@ class Login extends Component {
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {username, password} = this.state;
|
const {username, password} = this.state;
|
||||||
|
const {loginFailed} = this.props;
|
||||||
return (
|
return (
|
||||||
<DefaultPage title="Login" maxWidth={250} hideButton={true}>
|
<DefaultPage title="Login" maxWidth={250} hideButton={true}>
|
||||||
<Grid item xs={12} style={{textAlign: 'center'}}>
|
<Grid item xs={12} style={{textAlign: 'center'}}>
|
||||||
|
|
@ -36,6 +42,7 @@ class Login extends Component {
|
||||||
style={{marginTop: 15, marginBottom: 5}} onClick={this.login}>
|
style={{marginTop: 15, marginBottom: 5}} onClick={this.login}>
|
||||||
Login
|
Login
|
||||||
</Button>
|
</Button>
|
||||||
|
{loginFailed && <Typography>Login Failed</Typography>}
|
||||||
</form>
|
</form>
|
||||||
</Container>
|
</Container>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,11 @@ class CurrentUserStore extends EventEmitter {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.currentUser = null;
|
this.currentUser = null;
|
||||||
|
this.loginFailed = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
isLoginFailed() {
|
||||||
|
return this.loginFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
get() {
|
get() {
|
||||||
|
|
@ -30,9 +35,14 @@ class CurrentUserStore extends EventEmitter {
|
||||||
|
|
||||||
handle(data) {
|
handle(data) {
|
||||||
if (data.type === 'REMOVE_CURRENT_USER') {
|
if (data.type === 'REMOVE_CURRENT_USER') {
|
||||||
|
this.loginFailed = false;
|
||||||
this.set(null);
|
this.set(null);
|
||||||
} else if (data.type === 'SET_CURRENT_USER') {
|
} else if (data.type === 'SET_CURRENT_USER') {
|
||||||
|
this.loginFailed = false;
|
||||||
this.set(data.payload);
|
this.set(data.payload);
|
||||||
|
} else if (data.type === 'LOGIN_FAILED') {
|
||||||
|
this.loginFailed = true;
|
||||||
|
this.emit('change');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue