This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
Lateinicus/src/pages/login.tsx

106 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-08-24 17:03:08 +00:00
import * as React from "react";
import Typography from "@material-ui/core/Typography";
import Paper from "@material-ui/core/Paper";
import TextField from "@material-ui/core/TextField";
import Grid from "@material-ui/core/Grid";
import Button from "@material-ui/core/Button";
import LinearProgress from "@material-ui/core/LinearProgress";
import Snackbar from "@material-ui/core/Snackbar";
2018-08-26 14:23:48 +00:00
import { Redirect } from "react-router-dom";
2018-09-16 15:16:24 +00:00
import { IUser } from "../models/user";
2018-08-24 17:03:08 +00:00
interface IProps {
2018-09-16 15:16:24 +00:00
login: (username: string, password: string) => Promise<IUser | {}>;
2018-09-18 16:59:15 +00:00
authenticated: boolean;
2018-08-24 17:03:08 +00:00
2018-09-18 16:59:15 +00:00
setLoading: (state: boolean) => void;
setSnackbar: (state: boolean, msg: string) => void;
2018-08-24 17:03:08 +00:00
loading: boolean;
2018-09-18 16:59:15 +00:00
snackOpen: boolean;
snackMsg: string;
2018-08-24 17:03:08 +00:00
}
2018-09-18 16:59:15 +00:00
/*
* interface IState {
* loading: boolean;
*
* snack: string; // The message
* open: boolean;
* } */
export default class LoginPage extends React.Component<IProps> {
private usernameRef: any = undefined;
private passwordRef: any = undefined;
performLogin = () => {
this.props.setLoading(true);
const username = this.usernameRef.value || "";
const password = this.passwordRef.value || "";
2018-09-16 15:16:24 +00:00
this.props.login(username, password).then((res: IUser) => {
// Set the session key
2018-09-16 15:16:24 +00:00
window.sessionStorage.setItem("sessionToken", res.sessionToken);
2018-08-24 17:03:08 +00:00
}, (err) => {
2018-09-18 16:59:15 +00:00
this.props.setLoading(false);
this.props.setSnackbar(true, "Failed to log in");
2018-08-24 17:03:08 +00:00
});
}
render() {
return <div>
<Grid
container
spacing={0}
direction="column"
alignItems="center"
justify="center"
style={{ minHeight: '100vh' }}>
<Grid item xs={12}>
<Paper className="paper">
<Typography variant="title">Login</Typography>
<Grid container direction="column" spacing={8}>
<Grid item>
<TextField
label="Username"
2018-09-18 16:59:15 +00:00
inputRef={node => this.usernameRef = node} />
2018-08-24 17:03:08 +00:00
</Grid>
<Grid item>
<TextField
label="Passwort"
type="password"
2018-09-18 16:59:15 +00:00
inputRef={node => this.passwordRef = node} />
2018-08-24 17:03:08 +00:00
</Grid>
<Grid item>
<Button
variant="contained"
color="primary"
className="login-btn"
onClick={() => this.performLogin()}>
Login
</Button>
{
2018-09-18 16:59:15 +00:00
this.props.loading ? (
2018-08-24 17:03:08 +00:00
<LinearProgress />
) : undefined
}
</Grid>
</Grid>
</Paper>
</Grid>
</Grid>
<Snackbar
2018-09-18 16:59:15 +00:00
open={this.props.snackOpen}
onClose={() => this.props.setSnackbar(false, "")}
message={this.props.snackMsg}
2018-08-24 17:03:08 +00:00
autoHideDuration={6000} />
2018-08-26 14:23:48 +00:00
{
2018-09-18 16:59:15 +00:00
this.props.authenticated ? (
2018-08-26 14:23:48 +00:00
<Redirect to="/dashboard" />
) : undefined
}
2018-08-24 17:03:08 +00:00
</div>;
}
};