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