blob: 9a7ff0cc8defbf33018bd9de28b51e4c790560e0 [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001import { Component } from 'react';
Adrien Béraud6ecaa402021-04-06 17:37:25 -04002
Adrien Béraudab519ff2022-05-03 15:34:48 -04003import Button from '@mui/material/Button';
4import TextField from '@mui/material/TextField';
5import FormControlLabel from '@mui/material/FormControlLabel';
6import Checkbox from '@mui/material/Checkbox';
7import Link from '@mui/material/Link';
Adrien Béraudab519ff2022-05-03 15:34:48 -04008import Typography from '@mui/material/Typography';
Adrien Béraudab519ff2022-05-03 15:34:48 -04009import DialogTitle from '@mui/material/DialogTitle';
10import Dialog from '@mui/material/Dialog';
11import DialogActions from '@mui/material/DialogActions';
12import DialogContent from '@mui/material/DialogContent';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040013
simond47ef9e2022-09-28 22:24:28 -040014import authManager from '../AuthManager';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040015
16function Copyright() {
simond47ef9e2022-09-28 22:24:28 -040017 return (
18 <Typography variant="body2" color="textSecondary" align="center">
19 {'Copyright © 2016-'}
20 {new Date().getFullYear()}
21 {' Savoir-faire Linux Inc.'}
22 <Link color="inherit" href="https://jami.net/">
23 Jami.net
24 </Link>{' '}
25 {'.'}
26 </Typography>
27 );
Adrien Béraud6ecaa402021-04-06 17:37:25 -040028}
29
Adrien Béraud023f7cf2022-09-18 14:57:53 -040030class SignInPage extends Component {
simond47ef9e2022-09-28 22:24:28 -040031 constructor(props) {
32 console.log('SignInPage ' + props.open);
33 super(props);
34 this.state = {
35 submitted: false,
36 loading: false,
37 };
38 this.handleSubmit = this.handleSubmit.bind(this);
39 this.localLogin = this.localLogin.bind(this);
40 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040041
simond47ef9e2022-09-28 22:24:28 -040042 handleusername(text) {
43 this.setState({ username: text.target.value });
44 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040045
simond47ef9e2022-09-28 22:24:28 -040046 handlePassword(text) {
47 this.setState({ password: text.target.value });
48 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040049
simond47ef9e2022-09-28 22:24:28 -040050 localLogin() {
51 this.setState({
52 submitted: true,
53 loading: true,
54 });
55 authManager.authenticate('admin', 'admin');
56 /*fetch('/api/localLogin?username=none&password=none', {
Adrien Béraud6ecaa402021-04-06 17:37:25 -040057 header: { "Content-Type": "application/json" },
58 method: "POST",
59 credentials: 'same-origin'
60 //body: JSON.stringify({ obj })
61 })
62 .then((res) => {
63 if (res.status === '200') {
64 this.setState({
65 redirect: true
66 });
67 } else if (res.status === '401') {
68 this.setState({
69 loading: false,
70 error: true,
71 open: true,
72 errorMessage: "Wrong credentials! Your are not allowed to connect"
73 })
74 }
75 //this.setState({ session: res });
76 }).catch((e) => {
77 this.setState({
78 loading: false,
79 error: true,
80 open: true,
81 errorMessage: e.toString()
82 })
83 })*/
simond47ef9e2022-09-28 22:24:28 -040084 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040085
simond47ef9e2022-09-28 22:24:28 -040086 handleSubmit(event) {
87 event.preventDefault();
88 let obj = {};
89 obj.username = this.state.username;
90 obj.password = this.state.password;
Adrien Béraud6ecaa402021-04-06 17:37:25 -040091
simond47ef9e2022-09-28 22:24:28 -040092 this.setState({
93 submitted: true,
94 loading: true,
95 });
96
97 fetch('/api/login?username=' + obj.username + '&password=' + obj.password, {
98 header: {
99 'Content-Type': 'application/json',
100 },
101 method: 'POST',
102 credentials: 'same-origin',
103 //body: JSON.stringify({ obj })
104 })
105 .then((res) => {
106 if (res.status === '200') {
107 this.setState({
108 redirect: true,
109 });
110 } else if (res.status === '401') {
111 this.setState({
112 loading: false,
113 error: true,
114 open: true,
115 errorMessage: 'Wrong credentials! Your are not allowed to connect',
116 });
117 }
118 //this.setState({ session: res });
119 })
120 .catch((e) => {
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400121 this.setState({
simond47ef9e2022-09-28 22:24:28 -0400122 loading: false,
123 error: true,
124 open: true,
125 errorMessage: e.toString(),
126 });
127 });
128 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400129
simond47ef9e2022-09-28 22:24:28 -0400130 render() {
131 console.log('SignInPage render ' + this.props.open);
132 return (
133 <Dialog open={this.props.open}>
134 <DialogTitle>Se connecter</DialogTitle>
135 <DialogContent>
136 <Button
137 type="submit"
138 fullWidth
139 variant="contained"
140 color="primary"
141 className="" /*{classes.submit}*/
142 onClick={() => {
143 this.localLogin();
144 }}
145 >
146 Compte local
147 </Button>
148 <TextField
149 variant="outlined"
150 margin="normal"
151 required
152 fullWidth
153 id="username"
154 label="LDAP Savoir-faire Linux"
155 name="username"
156 autoComplete="email"
157 autoFocus
158 onChange={(text) => {
159 this.handleusername(text);
160 }}
161 />
162 <TextField
163 variant="outlined"
164 margin="normal"
165 required
166 fullWidth
167 name="password"
168 label="Mot de passe"
169 type="password"
170 id="password"
171 autoComplete="current-password"
172 onChange={(text) => {
173 this.handlePassword(text);
174 }}
175 />
176 <FormControlLabel control={<Checkbox value="remember" color="primary" />} label="Se rapeller de moi" />
177 </DialogContent>
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400178
simond47ef9e2022-09-28 22:24:28 -0400179 <DialogActions>
180 <Button type="submit" size="medium" onClick={this.handleSubmit}>
181 Se connecter
182 </Button>
183 </DialogActions>
184 </Dialog>
185 );
186 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400187}
188
simond47ef9e2022-09-28 22:24:28 -0400189export default SignInPage;