simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 18 | import Button from '@mui/material/Button'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 19 | import Checkbox from '@mui/material/Checkbox'; |
Adrien Béraud | ab519ff | 2022-05-03 15:34:48 -0400 | [diff] [blame] | 20 | import Dialog from '@mui/material/Dialog'; |
| 21 | import DialogActions from '@mui/material/DialogActions'; |
| 22 | import DialogContent from '@mui/material/DialogContent'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | import DialogTitle from '@mui/material/DialogTitle'; |
| 24 | import FormControlLabel from '@mui/material/FormControlLabel'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 25 | import TextField from '@mui/material/TextField'; |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 26 | import { ChangeEvent, Component, MouseEvent } from 'react'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 27 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 28 | import authManager from '../AuthManager'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 29 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 30 | type SignInPageProps = { |
| 31 | open: boolean; |
| 32 | }; |
| 33 | type SignInPageState = { |
| 34 | username?: string; |
| 35 | password?: string; |
| 36 | submitted?: boolean; |
| 37 | loading?: boolean; |
| 38 | redirect?: boolean; |
| 39 | error?: boolean; |
| 40 | open?: boolean; |
| 41 | errorMessage?: string; |
| 42 | }; |
| 43 | |
| 44 | class SignInPage extends Component<SignInPageProps, SignInPageState> { |
| 45 | constructor(props: SignInPageProps) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 46 | console.log('SignInPage ' + props.open); |
| 47 | super(props); |
| 48 | this.state = { |
| 49 | submitted: false, |
| 50 | loading: false, |
| 51 | }; |
| 52 | this.handleSubmit = this.handleSubmit.bind(this); |
| 53 | this.localLogin = this.localLogin.bind(this); |
| 54 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 55 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 56 | handleusername(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 57 | this.setState({ username: text.target.value }); |
| 58 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 59 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 60 | handlePassword(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 61 | this.setState({ password: text.target.value }); |
| 62 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 63 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 64 | localLogin() { |
| 65 | this.setState({ |
| 66 | submitted: true, |
| 67 | loading: true, |
| 68 | }); |
| 69 | authManager.authenticate('admin', 'admin'); |
| 70 | /*fetch('/api/localLogin?username=none&password=none', { |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 71 | header: { "Content-Type": "application/json" }, |
| 72 | method: "POST", |
| 73 | credentials: 'same-origin' |
| 74 | //body: JSON.stringify({ obj }) |
| 75 | }) |
| 76 | .then((res) => { |
| 77 | if (res.status === '200') { |
| 78 | this.setState({ |
| 79 | redirect: true |
| 80 | }); |
| 81 | } else if (res.status === '401') { |
| 82 | this.setState({ |
| 83 | loading: false, |
| 84 | error: true, |
| 85 | open: true, |
| 86 | errorMessage: "Wrong credentials! Your are not allowed to connect" |
| 87 | }) |
| 88 | } |
| 89 | //this.setState({ session: res }); |
| 90 | }).catch((e) => { |
| 91 | this.setState({ |
| 92 | loading: false, |
| 93 | error: true, |
| 94 | open: true, |
| 95 | errorMessage: e.toString() |
| 96 | }) |
| 97 | })*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 98 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 99 | |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 100 | handleSubmit(event: MouseEvent<HTMLButtonElement>) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 101 | event.preventDefault(); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 102 | const obj = { |
| 103 | username: this.state.username, |
| 104 | password: this.state.password, |
| 105 | }; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 106 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 107 | this.setState({ |
| 108 | submitted: true, |
| 109 | loading: true, |
| 110 | }); |
| 111 | |
| 112 | fetch('/api/login?username=' + obj.username + '&password=' + obj.password, { |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 113 | headers: { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 114 | 'Content-Type': 'application/json', |
| 115 | }, |
| 116 | method: 'POST', |
| 117 | credentials: 'same-origin', |
| 118 | //body: JSON.stringify({ obj }) |
| 119 | }) |
| 120 | .then((res) => { |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 121 | if (res.status === 200) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 122 | this.setState({ |
| 123 | redirect: true, |
| 124 | }); |
simon | fe1de72 | 2022-10-02 00:21:43 -0400 | [diff] [blame] | 125 | } else if (res.status === 401) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 126 | this.setState({ |
| 127 | loading: false, |
| 128 | error: true, |
| 129 | open: true, |
| 130 | errorMessage: 'Wrong credentials! Your are not allowed to connect', |
| 131 | }); |
| 132 | } |
| 133 | //this.setState({ session: res }); |
| 134 | }) |
| 135 | .catch((e) => { |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 136 | this.setState({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 137 | loading: false, |
| 138 | error: true, |
| 139 | open: true, |
| 140 | errorMessage: e.toString(), |
| 141 | }); |
| 142 | }); |
| 143 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 144 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 145 | render() { |
| 146 | console.log('SignInPage render ' + this.props.open); |
| 147 | return ( |
| 148 | <Dialog open={this.props.open}> |
| 149 | <DialogTitle>Se connecter</DialogTitle> |
| 150 | <DialogContent> |
| 151 | <Button |
| 152 | type="submit" |
| 153 | fullWidth |
| 154 | variant="contained" |
| 155 | color="primary" |
| 156 | className="" /*{classes.submit}*/ |
| 157 | onClick={() => { |
| 158 | this.localLogin(); |
| 159 | }} |
| 160 | > |
| 161 | Compte local |
| 162 | </Button> |
| 163 | <TextField |
| 164 | variant="outlined" |
| 165 | margin="normal" |
| 166 | required |
| 167 | fullWidth |
| 168 | id="username" |
| 169 | label="LDAP Savoir-faire Linux" |
| 170 | name="username" |
| 171 | autoComplete="email" |
| 172 | autoFocus |
| 173 | onChange={(text) => { |
| 174 | this.handleusername(text); |
| 175 | }} |
| 176 | /> |
| 177 | <TextField |
| 178 | variant="outlined" |
| 179 | margin="normal" |
| 180 | required |
| 181 | fullWidth |
| 182 | name="password" |
| 183 | label="Mot de passe" |
| 184 | type="password" |
| 185 | id="password" |
| 186 | autoComplete="current-password" |
| 187 | onChange={(text) => { |
| 188 | this.handlePassword(text); |
| 189 | }} |
| 190 | /> |
| 191 | <FormControlLabel control={<Checkbox value="remember" color="primary" />} label="Se rapeller de moi" /> |
| 192 | </DialogContent> |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 193 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 194 | <DialogActions> |
| 195 | <Button type="submit" size="medium" onClick={this.handleSubmit}> |
| 196 | Se connecter |
| 197 | </Button> |
| 198 | </DialogActions> |
| 199 | </Dialog> |
| 200 | ); |
| 201 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 202 | } |
| 203 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 204 | export default SignInPage; |