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