1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 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';
15 import { replace } from 'react-router-redux';
17 type CssRules = 'root' | 'loginBtn' | 'card' | 'wrapper' | 'progress';
19 const styles: StyleRulesCallback<CssRules> = theme => ({
24 margin: `${theme.spacing.unit} auto`
27 marginTop: theme.spacing.unit,
31 marginTop: theme.spacing.unit,
35 margin: theme.spacing.unit,
48 type LoginFormProps = DispatchProp<any> & WithStyles<CssRules> & {
49 handleSubmit: (username: string, password: string) => AxiosPromise;
53 export const LoginForm = withStyles(styles)(
54 ({ handleSubmit, loginLabel, dispatch, classes }: LoginFormProps) => {
55 const userInput = useRef<HTMLInputElement>(null);
56 const [username, setUsername] = useState('');
57 const [password, setPassword] = useState('');
58 const [isButtonDisabled, setIsButtonDisabled] = useState(true);
59 const [isSubmitting, setSubmitting] = useState(false);
60 const [helperText, setHelperText] = useState('');
61 const [error, setError] = useState(false);
66 if (username.trim() && password.trim()) {
67 setIsButtonDisabled(false);
69 setIsButtonDisabled(true);
71 }, [username, password]);
73 // This only runs once after render.
78 const setFocus = () => {
79 userInput.current!.focus();
82 const handleLogin = () => {
86 handleSubmit(username, password)
89 if (response.data.uuid && response.data.api_token) {
90 const apiToken = `v2/${response.data.uuid}/${response.data.api_token}`;
91 const rd = new URL(window.location.href);
92 const rdUrl = rd.pathname + rd.search;
93 dispatch<any>(saveApiToken(apiToken)).finally(
94 () => rdUrl === '/' ? dispatch(navigateToRootProject) : dispatch(replace(rdUrl))
98 setHelperText(response.data.message || 'Please try again');
104 setSubmitting(false);
105 setHelperText(`${(err.response && err.response.data && err.response.data.errors[0]) || 'Error logging in: '+err}`);
110 const handleKeyPress = (e: any) => {
111 if (e.keyCode === 13 || e.which === 13) {
112 if (!isButtonDisabled) {
120 <form className={classes.root} noValidate autoComplete="off">
121 <Card className={classes.card}>
122 <div className={classes.wrapper}>
126 disabled={isSubmitting}
127 error={error} fullWidth id="username" type="email"
128 label="Username" margin="normal"
129 onChange={(e) => setUsername(e.target.value)}
130 onKeyPress={(e) => handleKeyPress(e)}
133 disabled={isSubmitting}
134 error={error} fullWidth id="password" type="password"
135 label="Password" margin="normal"
136 helperText={helperText}
137 onChange={(e) => setPassword(e.target.value)}
138 onKeyPress={(e) => handleKeyPress(e)}
142 <Button variant="contained" size="large" color="primary"
143 className={classes.loginBtn} onClick={() => handleLogin()}
144 disabled={isSubmitting || isButtonDisabled}>
145 {loginLabel || 'Log in'}
148 { isSubmitting && <CircularProgress color='secondary' className={classes.progress} />}