Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import Avatar from '@material-ui/core/Avatar'; |
| 3 | import Button from '@material-ui/core/Button'; |
| 4 | import CssBaseline from '@material-ui/core/CssBaseline'; |
| 5 | import TextField from '@material-ui/core/TextField'; |
| 6 | import FormControlLabel from '@material-ui/core/FormControlLabel'; |
| 7 | import Checkbox from '@material-ui/core/Checkbox'; |
| 8 | import Link from '@material-ui/core/Link'; |
| 9 | import Grid from '@material-ui/core/Grid'; |
| 10 | import Box from '@material-ui/core/Box'; |
| 11 | import LockOutlinedIcon from '@material-ui/icons/LockOutlined'; |
| 12 | import Typography from '@material-ui/core/Typography'; |
| 13 | //import { makeStyles } from '@material-ui/core/styles'; |
| 14 | import Container from '@material-ui/core/Container'; |
| 15 | import { Redirect } from 'react-router-dom'; |
| 16 | import CircularProgress from '@material-ui/core/CircularProgress'; |
| 17 | import DialogTitle from '@material-ui/core/DialogTitle'; |
| 18 | import Dialog from '@material-ui/core/Dialog'; |
| 19 | |
| 20 | |
| 21 | function Copyright() { |
| 22 | return ( |
| 23 | <Typography variant="body2" color="textSecondary" align="center"> |
| 24 | {'Copyright © 2016-'}{new Date().getFullYear()}{' Savoir-faire Linux Inc. GNU '} |
| 25 | <Link color="inherit" href="https://jami.net/"> |
| 26 | Jami.net |
| 27 | </Link>{' '} |
| 28 | |
| 29 | {'.'} |
| 30 | </Typography> |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | /*const useStyles = makeStyles((theme) => ({ |
| 35 | paper: { |
| 36 | marginTop: theme.spacing(8), |
| 37 | display: 'flex', |
| 38 | flexDirection: 'column', |
| 39 | alignItems: 'center', |
| 40 | }, |
| 41 | avatar: { |
| 42 | margin: theme.spacing(1), |
| 43 | backgroundColor: theme.palette.secondary.main, |
| 44 | }, |
| 45 | form: { |
| 46 | width: '100%', // Fix IE 11 issue. |
| 47 | marginTop: theme.spacing(1), |
| 48 | }, |
| 49 | submit: { |
| 50 | margin: theme.spacing(3, 0, 2), |
| 51 | }, |
| 52 | }));*/ |
| 53 | |
| 54 | /*function SignIn() { |
| 55 | const classes = useStyles(); |
| 56 | |
| 57 | |
| 58 | }*/ |
| 59 | |
| 60 | /* |
| 61 | TODO: |
| 62 | Use useState to handle username password and redirect states to render this page to |
| 63 | comply with material-ui usage of useStyles |
| 64 | Src: https://blog.logrocket.com/a-guide-to-usestate-in-react-ecb9952e406c/ |
| 65 | */ |
| 66 | |
| 67 | class SignInPage extends React.Component { |
| 68 | |
| 69 | constructor() { |
| 70 | super() |
| 71 | this.state = { |
| 72 | username: '', |
| 73 | password: '', |
| 74 | redirect: false, |
| 75 | session: null, |
| 76 | submitted: false, |
| 77 | loading: false, |
| 78 | error: false, |
| 79 | open: false, |
| 80 | errorMessage: '' |
| 81 | } |
| 82 | this.handleSubmit = this.handleSubmit.bind(this); |
| 83 | } |
| 84 | |
| 85 | handleusername(text) { |
| 86 | this.setState({ username: text.target.value }) |
| 87 | } |
| 88 | |
| 89 | handlePassword(text) { |
| 90 | this.setState({ password: text.target.value }) |
| 91 | } |
| 92 | |
| 93 | handleSubmit(event) { |
| 94 | event.preventDefault(); |
| 95 | let obj = {} |
| 96 | obj.username = this.state.username; |
| 97 | obj.password = this.state.password; |
| 98 | |
| 99 | this.setState({ |
| 100 | submitted: true, |
| 101 | loading: true |
| 102 | }) |
| 103 | |
| 104 | fetch('/api/login?username=' + obj.username + '&password=' + obj.password, |
| 105 | { |
| 106 | header: { |
| 107 | "Content-Type": "application/json" |
| 108 | }, |
| 109 | method: "POST", |
| 110 | credentials: 'same-origin' |
| 111 | //body: JSON.stringify({ obj }) |
| 112 | } |
| 113 | ).then((res) => { |
| 114 | if (res.status == '200') { |
| 115 | this.setState({ |
| 116 | redirect: true |
| 117 | }); |
| 118 | } else if (res.status == '401') { |
| 119 | this.setState({ |
| 120 | loading: false, |
| 121 | error: true, |
| 122 | open: true, |
| 123 | errorMessage: "Wrong credentials! Your are not allowed to connect" |
| 124 | }) |
| 125 | } |
| 126 | //this.setState({ session: res }); |
| 127 | }).catch((e) => { |
| 128 | this.setState({ |
| 129 | loading: false, |
| 130 | error: true, |
| 131 | open: true, |
| 132 | errorMessage: e.toString() |
| 133 | }) |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | |
| 138 | render() { |
| 139 | if (this.state.redirect) { |
| 140 | return <Redirect to="/jaas" /> |
| 141 | } |
| 142 | |
| 143 | return ( |
| 144 | <Container component="main" maxWidth="xs" > |
| 145 | <CssBaseline /> |
| 146 | <div className=""/*{classes.paper}*/> |
| 147 | <Typography component="h1" variant="h5"> |
| 148 | Se connecter |
| 149 | </Typography> |
| 150 | <form className=""/*{classes.form}*/ onSubmit={this.handleSubmit} > |
| 151 | <TextField |
| 152 | variant="outlined" |
| 153 | margin="normal" |
| 154 | required |
| 155 | fullWidth |
| 156 | id="username" |
| 157 | label="LDAP Savoir-faire Linux" |
| 158 | name="username" |
| 159 | autoComplete="email" |
| 160 | autoFocus |
| 161 | onChange={(text) => { this.handleusername(text) }} |
| 162 | /> |
| 163 | <TextField |
| 164 | variant="outlined" |
| 165 | margin="normal" |
| 166 | required |
| 167 | fullWidth |
| 168 | name="password" |
| 169 | label="Mot de passe" |
| 170 | type="password" |
| 171 | id="password" |
| 172 | autoComplete="current-password" |
| 173 | onChange={(text) => { this.handlePassword(text) }} |
| 174 | /> |
| 175 | <FormControlLabel |
| 176 | control={<Checkbox value="remember" color="primary" />} |
| 177 | label="Se rapeller de moi" |
| 178 | /> |
| 179 | <Button |
| 180 | type="submit" |
| 181 | fullWidth |
| 182 | variant="contained" |
| 183 | color="primary" |
| 184 | className=""/*{classes.submit}*/ |
| 185 | // onClick={() => { this.login() }} |
| 186 | > |
| 187 | Se connecter |
| 188 | </Button> |
| 189 | <Grid container> |
| 190 | <Grid item xs> |
| 191 | <Link href="#" variant="body2"> |
| 192 | Mot de passe oublié ? |
| 193 | </Link> |
| 194 | </Grid> |
| 195 | <Grid item> |
| 196 | <Link href="#" variant="body2"> |
| 197 | {"Tu n'as pas de compte? Inscris-toi"} |
| 198 | </Link> |
| 199 | </Grid> |
| 200 | </Grid> |
| 201 | </form> |
| 202 | </div> |
| 203 | <Box mt={8}> |
| 204 | <Copyright /> |
| 205 | </Box> |
| 206 | {this.state.submitted && this.state.loading && <CircularProgress |
| 207 | style={{ position: 'relative' }} |
| 208 | size={40} |
| 209 | top={10} |
| 210 | style={{ marginLeft: '50%' }} |
| 211 | />} |
| 212 | { |
| 213 | this.state.error && <Dialog aria-labelledby="simple-dialog-title" open={this.state.open} > |
| 214 | <DialogTitle id="simple-dialog-title">{this.state.errorMessage}</DialogTitle> |
| 215 | </Dialog> |
| 216 | } |
| 217 | </Container> |
| 218 | ); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | export default SignInPage; |