blob: ee990bc0b92528d6f69c7d443f0c6669442e2c1e [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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éraudab519ff2022-05-03 15:34:48 -040018import Button from '@mui/material/Button';
Adrien Béraudab519ff2022-05-03 15:34:48 -040019import Checkbox from '@mui/material/Checkbox';
Adrien Béraudab519ff2022-05-03 15:34:48 -040020import Dialog from '@mui/material/Dialog';
21import DialogActions from '@mui/material/DialogActions';
22import DialogContent from '@mui/material/DialogContent';
simon07b4eb02022-09-29 17:50:26 -040023import DialogTitle from '@mui/material/DialogTitle';
24import FormControlLabel from '@mui/material/FormControlLabel';
simon07b4eb02022-09-29 17:50:26 -040025import TextField from '@mui/material/TextField';
simonfe1de722022-10-02 00:21:43 -040026import { ChangeEvent, Component, MouseEvent } from 'react';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040027
simond47ef9e2022-09-28 22:24:28 -040028import authManager from '../AuthManager';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040029
simonfe1de722022-10-02 00:21:43 -040030type SignInPageProps = {
31 open: boolean;
32};
33type 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
44class SignInPage extends Component<SignInPageProps, SignInPageState> {
45 constructor(props: SignInPageProps) {
simond47ef9e2022-09-28 22:24:28 -040046 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éraud6ecaa402021-04-06 17:37:25 -040055
simonfe1de722022-10-02 00:21:43 -040056 handleusername(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
simond47ef9e2022-09-28 22:24:28 -040057 this.setState({ username: text.target.value });
58 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040059
simonfe1de722022-10-02 00:21:43 -040060 handlePassword(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
simond47ef9e2022-09-28 22:24:28 -040061 this.setState({ password: text.target.value });
62 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040063
simond47ef9e2022-09-28 22:24:28 -040064 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éraud6ecaa402021-04-06 17:37:25 -040071 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 })*/
simond47ef9e2022-09-28 22:24:28 -040098 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040099
simonfe1de722022-10-02 00:21:43 -0400100 handleSubmit(event: MouseEvent<HTMLButtonElement>) {
simond47ef9e2022-09-28 22:24:28 -0400101 event.preventDefault();
simonfe1de722022-10-02 00:21:43 -0400102 const obj = {
103 username: this.state.username,
104 password: this.state.password,
105 };
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400106
simond47ef9e2022-09-28 22:24:28 -0400107 this.setState({
108 submitted: true,
109 loading: true,
110 });
111
112 fetch('/api/login?username=' + obj.username + '&password=' + obj.password, {
simonfe1de722022-10-02 00:21:43 -0400113 headers: {
simond47ef9e2022-09-28 22:24:28 -0400114 'Content-Type': 'application/json',
115 },
116 method: 'POST',
117 credentials: 'same-origin',
118 //body: JSON.stringify({ obj })
119 })
120 .then((res) => {
simonfe1de722022-10-02 00:21:43 -0400121 if (res.status === 200) {
simond47ef9e2022-09-28 22:24:28 -0400122 this.setState({
123 redirect: true,
124 });
simonfe1de722022-10-02 00:21:43 -0400125 } else if (res.status === 401) {
simond47ef9e2022-09-28 22:24:28 -0400126 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éraud6ecaa402021-04-06 17:37:25 -0400136 this.setState({
simond47ef9e2022-09-28 22:24:28 -0400137 loading: false,
138 error: true,
139 open: true,
140 errorMessage: e.toString(),
141 });
142 });
143 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400144
simond47ef9e2022-09-28 22:24:28 -0400145 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éraud6ecaa402021-04-06 17:37:25 -0400193
simond47ef9e2022-09-28 22:24:28 -0400194 <DialogActions>
195 <Button type="submit" size="medium" onClick={this.handleSubmit}>
196 Se connecter
197 </Button>
198 </DialogActions>
199 </Dialog>
200 );
201 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400202}
203
simond47ef9e2022-09-28 22:24:28 -0400204export default SignInPage;