Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 1 | import Button from '@mui/material/Button'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 2 | import Checkbox from '@mui/material/Checkbox'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 3 | import Dialog from '@mui/material/Dialog'; |
| 4 | import DialogActions from '@mui/material/DialogActions'; |
| 5 | import DialogContent from '@mui/material/DialogContent'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 6 | import DialogTitle from '@mui/material/DialogTitle'; |
| 7 | import FormControlLabel from '@mui/material/FormControlLabel'; |
| 8 | import Link from '@mui/material/Link'; |
| 9 | import TextField from '@mui/material/TextField'; |
| 10 | import Typography from '@mui/material/Typography'; |
| 11 | import { Component } from 'react'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 12 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 13 | import authManager from '../AuthManager'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 14 | |
| 15 | function Copyright() { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 16 | return ( |
| 17 | <Typography variant="body2" color="textSecondary" align="center"> |
| 18 | {'Copyright © 2016-'} |
| 19 | {new Date().getFullYear()} |
| 20 | {' Savoir-faire Linux Inc.'} |
| 21 | <Link color="inherit" href="https://jami.net/"> |
| 22 | Jami.net |
| 23 | </Link>{' '} |
| 24 | {'.'} |
| 25 | </Typography> |
| 26 | ); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Adrien Béraud | 023f7cf | 2022-09-18 14:57:53 -0400 | [diff] [blame] | 29 | class SignInPage extends Component { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | constructor(props) { |
| 31 | console.log('SignInPage ' + props.open); |
| 32 | super(props); |
| 33 | this.state = { |
| 34 | submitted: false, |
| 35 | loading: false, |
| 36 | }; |
| 37 | this.handleSubmit = this.handleSubmit.bind(this); |
| 38 | this.localLogin = this.localLogin.bind(this); |
| 39 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 40 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 41 | handleusername(text) { |
| 42 | this.setState({ username: text.target.value }); |
| 43 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 44 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 45 | handlePassword(text) { |
| 46 | this.setState({ password: text.target.value }); |
| 47 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 48 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 49 | localLogin() { |
| 50 | this.setState({ |
| 51 | submitted: true, |
| 52 | loading: true, |
| 53 | }); |
| 54 | authManager.authenticate('admin', 'admin'); |
| 55 | /*fetch('/api/localLogin?username=none&password=none', { |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 56 | header: { "Content-Type": "application/json" }, |
| 57 | method: "POST", |
| 58 | credentials: 'same-origin' |
| 59 | //body: JSON.stringify({ obj }) |
| 60 | }) |
| 61 | .then((res) => { |
| 62 | if (res.status === '200') { |
| 63 | this.setState({ |
| 64 | redirect: true |
| 65 | }); |
| 66 | } else if (res.status === '401') { |
| 67 | this.setState({ |
| 68 | loading: false, |
| 69 | error: true, |
| 70 | open: true, |
| 71 | errorMessage: "Wrong credentials! Your are not allowed to connect" |
| 72 | }) |
| 73 | } |
| 74 | //this.setState({ session: res }); |
| 75 | }).catch((e) => { |
| 76 | this.setState({ |
| 77 | loading: false, |
| 78 | error: true, |
| 79 | open: true, |
| 80 | errorMessage: e.toString() |
| 81 | }) |
| 82 | })*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 83 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 84 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 85 | handleSubmit(event) { |
| 86 | event.preventDefault(); |
| 87 | let obj = {}; |
| 88 | obj.username = this.state.username; |
| 89 | obj.password = this.state.password; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 90 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 91 | this.setState({ |
| 92 | submitted: true, |
| 93 | loading: true, |
| 94 | }); |
| 95 | |
| 96 | fetch('/api/login?username=' + obj.username + '&password=' + obj.password, { |
| 97 | header: { |
| 98 | 'Content-Type': 'application/json', |
| 99 | }, |
| 100 | method: 'POST', |
| 101 | credentials: 'same-origin', |
| 102 | //body: JSON.stringify({ obj }) |
| 103 | }) |
| 104 | .then((res) => { |
| 105 | if (res.status === '200') { |
| 106 | this.setState({ |
| 107 | redirect: true, |
| 108 | }); |
| 109 | } else if (res.status === '401') { |
| 110 | this.setState({ |
| 111 | loading: false, |
| 112 | error: true, |
| 113 | open: true, |
| 114 | errorMessage: 'Wrong credentials! Your are not allowed to connect', |
| 115 | }); |
| 116 | } |
| 117 | //this.setState({ session: res }); |
| 118 | }) |
| 119 | .catch((e) => { |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 120 | this.setState({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 121 | loading: false, |
| 122 | error: true, |
| 123 | open: true, |
| 124 | errorMessage: e.toString(), |
| 125 | }); |
| 126 | }); |
| 127 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 128 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 129 | render() { |
| 130 | console.log('SignInPage render ' + this.props.open); |
| 131 | return ( |
| 132 | <Dialog open={this.props.open}> |
| 133 | <DialogTitle>Se connecter</DialogTitle> |
| 134 | <DialogContent> |
| 135 | <Button |
| 136 | type="submit" |
| 137 | fullWidth |
| 138 | variant="contained" |
| 139 | color="primary" |
| 140 | className="" /*{classes.submit}*/ |
| 141 | onClick={() => { |
| 142 | this.localLogin(); |
| 143 | }} |
| 144 | > |
| 145 | Compte local |
| 146 | </Button> |
| 147 | <TextField |
| 148 | variant="outlined" |
| 149 | margin="normal" |
| 150 | required |
| 151 | fullWidth |
| 152 | id="username" |
| 153 | label="LDAP Savoir-faire Linux" |
| 154 | name="username" |
| 155 | autoComplete="email" |
| 156 | autoFocus |
| 157 | onChange={(text) => { |
| 158 | this.handleusername(text); |
| 159 | }} |
| 160 | /> |
| 161 | <TextField |
| 162 | variant="outlined" |
| 163 | margin="normal" |
| 164 | required |
| 165 | fullWidth |
| 166 | name="password" |
| 167 | label="Mot de passe" |
| 168 | type="password" |
| 169 | id="password" |
| 170 | autoComplete="current-password" |
| 171 | onChange={(text) => { |
| 172 | this.handlePassword(text); |
| 173 | }} |
| 174 | /> |
| 175 | <FormControlLabel control={<Checkbox value="remember" color="primary" />} label="Se rapeller de moi" /> |
| 176 | </DialogContent> |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 177 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 178 | <DialogActions> |
| 179 | <Button type="submit" size="medium" onClick={this.handleSubmit}> |
| 180 | Se connecter |
| 181 | </Button> |
| 182 | </DialogActions> |
| 183 | </Dialog> |
| 184 | ); |
| 185 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 186 | } |
| 187 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 188 | export default SignInPage; |