blob: fb01ccd8a29253a2c50d589752b686aa304ee055 [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';
25import Link from '@mui/material/Link';
26import TextField from '@mui/material/TextField';
27import Typography from '@mui/material/Typography';
simonfe1de722022-10-02 00:21:43 -040028import { ChangeEvent, Component, MouseEvent } from 'react';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040029
simond47ef9e2022-09-28 22:24:28 -040030import authManager from '../AuthManager';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040031
32function Copyright() {
simond47ef9e2022-09-28 22:24:28 -040033 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éraud6ecaa402021-04-06 17:37:25 -040044}
45
simonfe1de722022-10-02 00:21:43 -040046type SignInPageProps = {
47 open: boolean;
48};
49type 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
60class SignInPage extends Component<SignInPageProps, SignInPageState> {
61 constructor(props: SignInPageProps) {
simond47ef9e2022-09-28 22:24:28 -040062 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éraud6ecaa402021-04-06 17:37:25 -040071
simonfe1de722022-10-02 00:21:43 -040072 handleusername(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
simond47ef9e2022-09-28 22:24:28 -040073 this.setState({ username: text.target.value });
74 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040075
simonfe1de722022-10-02 00:21:43 -040076 handlePassword(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
simond47ef9e2022-09-28 22:24:28 -040077 this.setState({ password: text.target.value });
78 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040079
simond47ef9e2022-09-28 22:24:28 -040080 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éraud6ecaa402021-04-06 17:37:25 -040087 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 })*/
simond47ef9e2022-09-28 22:24:28 -0400114 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400115
simonfe1de722022-10-02 00:21:43 -0400116 handleSubmit(event: MouseEvent<HTMLButtonElement>) {
simond47ef9e2022-09-28 22:24:28 -0400117 event.preventDefault();
simonfe1de722022-10-02 00:21:43 -0400118 const obj = {
119 username: this.state.username,
120 password: this.state.password,
121 };
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400122
simond47ef9e2022-09-28 22:24:28 -0400123 this.setState({
124 submitted: true,
125 loading: true,
126 });
127
128 fetch('/api/login?username=' + obj.username + '&password=' + obj.password, {
simonfe1de722022-10-02 00:21:43 -0400129 headers: {
simond47ef9e2022-09-28 22:24:28 -0400130 'Content-Type': 'application/json',
131 },
132 method: 'POST',
133 credentials: 'same-origin',
134 //body: JSON.stringify({ obj })
135 })
136 .then((res) => {
simonfe1de722022-10-02 00:21:43 -0400137 if (res.status === 200) {
simond47ef9e2022-09-28 22:24:28 -0400138 this.setState({
139 redirect: true,
140 });
simonfe1de722022-10-02 00:21:43 -0400141 } else if (res.status === 401) {
simond47ef9e2022-09-28 22:24:28 -0400142 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éraud6ecaa402021-04-06 17:37:25 -0400152 this.setState({
simond47ef9e2022-09-28 22:24:28 -0400153 loading: false,
154 error: true,
155 open: true,
156 errorMessage: e.toString(),
157 });
158 });
159 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400160
simond47ef9e2022-09-28 22:24:28 -0400161 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éraud6ecaa402021-04-06 17:37:25 -0400209
simond47ef9e2022-09-28 22:24:28 -0400210 <DialogActions>
211 <Button type="submit" size="medium" onClick={this.handleSubmit}>
212 Se connecter
213 </Button>
214 </DialogActions>
215 </Dialog>
216 );
217 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400218}
219
simond47ef9e2022-09-28 22:24:28 -0400220export default SignInPage;