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