blob: e98cc79ba171f6cd30bbc7923ba6cb0c0635757a [file] [log] [blame]
Adrien Béraudab519ff2022-05-03 15:34:48 -04001import Button from '@mui/material/Button';
Adrien Béraudab519ff2022-05-03 15:34:48 -04002import Checkbox from '@mui/material/Checkbox';
Adrien Béraudab519ff2022-05-03 15:34:48 -04003import Dialog from '@mui/material/Dialog';
4import DialogActions from '@mui/material/DialogActions';
5import DialogContent from '@mui/material/DialogContent';
simon07b4eb02022-09-29 17:50:26 -04006import DialogTitle from '@mui/material/DialogTitle';
7import FormControlLabel from '@mui/material/FormControlLabel';
8import Link from '@mui/material/Link';
9import TextField from '@mui/material/TextField';
10import Typography from '@mui/material/Typography';
simonfe1de722022-10-02 00:21:43 -040011import { ChangeEvent, Component, MouseEvent } from 'react';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040012
simond47ef9e2022-09-28 22:24:28 -040013import authManager from '../AuthManager';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040014
15function Copyright() {
simond47ef9e2022-09-28 22:24:28 -040016 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éraud6ecaa402021-04-06 17:37:25 -040027}
28
simonfe1de722022-10-02 00:21:43 -040029type SignInPageProps = {
30 open: boolean;
31};
32type 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
43class SignInPage extends Component<SignInPageProps, SignInPageState> {
44 constructor(props: SignInPageProps) {
simond47ef9e2022-09-28 22:24:28 -040045 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éraud6ecaa402021-04-06 17:37:25 -040054
simonfe1de722022-10-02 00:21:43 -040055 handleusername(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
simond47ef9e2022-09-28 22:24:28 -040056 this.setState({ username: text.target.value });
57 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040058
simonfe1de722022-10-02 00:21:43 -040059 handlePassword(text: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
simond47ef9e2022-09-28 22:24:28 -040060 this.setState({ password: text.target.value });
61 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040062
simond47ef9e2022-09-28 22:24:28 -040063 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éraud6ecaa402021-04-06 17:37:25 -040070 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 })*/
simond47ef9e2022-09-28 22:24:28 -040097 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040098
simonfe1de722022-10-02 00:21:43 -040099 handleSubmit(event: MouseEvent<HTMLButtonElement>) {
simond47ef9e2022-09-28 22:24:28 -0400100 event.preventDefault();
simonfe1de722022-10-02 00:21:43 -0400101 const obj = {
102 username: this.state.username,
103 password: this.state.password,
104 };
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400105
simond47ef9e2022-09-28 22:24:28 -0400106 this.setState({
107 submitted: true,
108 loading: true,
109 });
110
111 fetch('/api/login?username=' + obj.username + '&password=' + obj.password, {
simonfe1de722022-10-02 00:21:43 -0400112 headers: {
simond47ef9e2022-09-28 22:24:28 -0400113 'Content-Type': 'application/json',
114 },
115 method: 'POST',
116 credentials: 'same-origin',
117 //body: JSON.stringify({ obj })
118 })
119 .then((res) => {
simonfe1de722022-10-02 00:21:43 -0400120 if (res.status === 200) {
simond47ef9e2022-09-28 22:24:28 -0400121 this.setState({
122 redirect: true,
123 });
simonfe1de722022-10-02 00:21:43 -0400124 } else if (res.status === 401) {
simond47ef9e2022-09-28 22:24:28 -0400125 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éraud6ecaa402021-04-06 17:37:25 -0400135 this.setState({
simond47ef9e2022-09-28 22:24:28 -0400136 loading: false,
137 error: true,
138 open: true,
139 errorMessage: e.toString(),
140 });
141 });
142 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400143
simond47ef9e2022-09-28 22:24:28 -0400144 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éraud6ecaa402021-04-06 17:37:25 -0400192
simond47ef9e2022-09-28 22:24:28 -0400193 <DialogActions>
194 <Button type="submit" size="medium" onClick={this.handleSubmit}>
195 Se connecter
196 </Button>
197 </DialogActions>
198 </Dialog>
199 );
200 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400201}
202
simond47ef9e2022-09-28 22:24:28 -0400203export default SignInPage;