blob: 34070d794be49d03fe52f3ffeda16f3b07a38aa8 [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';
11import { Component } 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
Adrien Béraud023f7cf2022-09-18 14:57:53 -040029class SignInPage extends Component {
simond47ef9e2022-09-28 22:24:28 -040030 constructor(props) {
31 console.log('SignInPage ' + props.open);
32 super(props);
33 this.state = {
34 submitted: false,
35 loading: false,
36 };
37 this.handleSubmit = this.handleSubmit.bind(this);
38 this.localLogin = this.localLogin.bind(this);
39 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040040
simond47ef9e2022-09-28 22:24:28 -040041 handleusername(text) {
42 this.setState({ username: text.target.value });
43 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040044
simond47ef9e2022-09-28 22:24:28 -040045 handlePassword(text) {
46 this.setState({ password: text.target.value });
47 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040048
simond47ef9e2022-09-28 22:24:28 -040049 localLogin() {
50 this.setState({
51 submitted: true,
52 loading: true,
53 });
54 authManager.authenticate('admin', 'admin');
55 /*fetch('/api/localLogin?username=none&password=none', {
Adrien Béraud6ecaa402021-04-06 17:37:25 -040056 header: { "Content-Type": "application/json" },
57 method: "POST",
58 credentials: 'same-origin'
59 //body: JSON.stringify({ obj })
60 })
61 .then((res) => {
62 if (res.status === '200') {
63 this.setState({
64 redirect: true
65 });
66 } else if (res.status === '401') {
67 this.setState({
68 loading: false,
69 error: true,
70 open: true,
71 errorMessage: "Wrong credentials! Your are not allowed to connect"
72 })
73 }
74 //this.setState({ session: res });
75 }).catch((e) => {
76 this.setState({
77 loading: false,
78 error: true,
79 open: true,
80 errorMessage: e.toString()
81 })
82 })*/
simond47ef9e2022-09-28 22:24:28 -040083 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040084
simond47ef9e2022-09-28 22:24:28 -040085 handleSubmit(event) {
86 event.preventDefault();
87 let obj = {};
88 obj.username = this.state.username;
89 obj.password = this.state.password;
Adrien Béraud6ecaa402021-04-06 17:37:25 -040090
simond47ef9e2022-09-28 22:24:28 -040091 this.setState({
92 submitted: true,
93 loading: true,
94 });
95
96 fetch('/api/login?username=' + obj.username + '&password=' + obj.password, {
97 header: {
98 'Content-Type': 'application/json',
99 },
100 method: 'POST',
101 credentials: 'same-origin',
102 //body: JSON.stringify({ obj })
103 })
104 .then((res) => {
105 if (res.status === '200') {
106 this.setState({
107 redirect: true,
108 });
109 } else if (res.status === '401') {
110 this.setState({
111 loading: false,
112 error: true,
113 open: true,
114 errorMessage: 'Wrong credentials! Your are not allowed to connect',
115 });
116 }
117 //this.setState({ session: res });
118 })
119 .catch((e) => {
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400120 this.setState({
simond47ef9e2022-09-28 22:24:28 -0400121 loading: false,
122 error: true,
123 open: true,
124 errorMessage: e.toString(),
125 });
126 });
127 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400128
simond47ef9e2022-09-28 22:24:28 -0400129 render() {
130 console.log('SignInPage render ' + this.props.open);
131 return (
132 <Dialog open={this.props.open}>
133 <DialogTitle>Se connecter</DialogTitle>
134 <DialogContent>
135 <Button
136 type="submit"
137 fullWidth
138 variant="contained"
139 color="primary"
140 className="" /*{classes.submit}*/
141 onClick={() => {
142 this.localLogin();
143 }}
144 >
145 Compte local
146 </Button>
147 <TextField
148 variant="outlined"
149 margin="normal"
150 required
151 fullWidth
152 id="username"
153 label="LDAP Savoir-faire Linux"
154 name="username"
155 autoComplete="email"
156 autoFocus
157 onChange={(text) => {
158 this.handleusername(text);
159 }}
160 />
161 <TextField
162 variant="outlined"
163 margin="normal"
164 required
165 fullWidth
166 name="password"
167 label="Mot de passe"
168 type="password"
169 id="password"
170 autoComplete="current-password"
171 onChange={(text) => {
172 this.handlePassword(text);
173 }}
174 />
175 <FormControlLabel control={<Checkbox value="remember" color="primary" />} label="Se rapeller de moi" />
176 </DialogContent>
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400177
simond47ef9e2022-09-28 22:24:28 -0400178 <DialogActions>
179 <Button type="submit" size="medium" onClick={this.handleSubmit}>
180 Se connecter
181 </Button>
182 </DialogActions>
183 </Dialog>
184 );
185 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400186}
187
simond47ef9e2022-09-28 22:24:28 -0400188export default SignInPage;