1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { useState, useEffect, useRef } from 'react';
7 import { withStyles, WithStyles, StyleRulesCallback } from '@material-ui/core/styles';
8 import CircularProgress from '@material-ui/core/CircularProgress';
9 import { Button, Card, CardContent, TextField, CardActions } from '@material-ui/core';
10 import { green } from '@material-ui/core/colors';
11 import { AxiosPromise } from 'axios';
12 import { DispatchProp } from 'react-redux';
13 import { saveApiToken } from '~/store/auth/auth-action';
14 import { navigateToRootProject } from '~/store/navigation/navigation-action';
16 type CssRules = 'root' | 'loginBtn' | 'card' | 'wrapper' | 'progress';
18 const styles: StyleRulesCallback<CssRules> = theme => ({
23 margin: `${theme.spacing.unit} auto`
26 marginTop: theme.spacing.unit,
30 marginTop: theme.spacing.unit,
34 margin: theme.spacing.unit,
47 type LoginFormProps = DispatchProp<any> & WithStyles<CssRules> & {
48 handleSubmit: (username: string, password: string) => AxiosPromise;
51 export const LoginForm = withStyles(styles)(
52 ({ handleSubmit, dispatch, classes }: LoginFormProps) => {
53 const userInput = useRef<HTMLInputElement>(null);
54 const [username, setUsername] = useState('');
55 const [password, setPassword] = useState('');
56 const [isButtonDisabled, setIsButtonDisabled] = useState(true);
57 const [isSubmitting, setSubmitting] = useState(false);
58 const [helperText, setHelperText] = useState('');
59 const [error, setError] = useState(false);
64 if (username.trim() && password.trim()) {
65 setIsButtonDisabled(false);
67 setIsButtonDisabled(true);
69 }, [username, password]);
71 // This only runs once after render.
76 const setFocus = () => {
77 userInput.current!.focus();
80 const handleLogin = () => {
84 handleSubmit(username, password)
87 const apiToken = response.data.token;
89 dispatch<any>(saveApiToken(apiToken)).finally(
90 () => dispatch(navigateToRootProject));
93 setHelperText(response.data.message || 'Please try again');
100 setHelperText(`${err.response && err.response.data && err.response.data.errors[0] || 'Error logging in: '+err}`);
105 const handleKeyPress = (e: any) => {
106 if (e.keyCode === 13 || e.which === 13) {
107 if (!isButtonDisabled) {
115 <form className={classes.root} noValidate autoComplete="off">
116 <Card className={classes.card}>
117 <div className={classes.wrapper}>
121 disabled={isSubmitting}
122 error={error} fullWidth id="username" type="email"
123 label="Username" margin="normal"
124 onChange={(e) => setUsername(e.target.value)}
125 onKeyPress={(e) => handleKeyPress(e)}
128 disabled={isSubmitting}
129 error={error} fullWidth id="password" type="password"
130 label="Password" margin="normal"
131 helperText={helperText}
132 onChange={(e) => setPassword(e.target.value)}
133 onKeyPress={(e) => handleKeyPress(e)}
137 <Button variant="contained" size="large" color="primary"
138 className={classes.loginBtn} onClick={() => handleLogin()}
139 disabled={isSubmitting || isButtonDisabled}>
143 { isSubmitting && <CircularProgress color='secondary' className={classes.progress} />}