blob: cdefa71cacb362660de729088576a5e49ff30d0a [file] [log] [blame]
Adrien Béraud6ecaa402021-04-06 17:37:25 -04001import React from 'react';
2
Adrien Béraudab519ff2022-05-03 15:34:48 -04003import Avatar from '@mui/material/Avatar';
4import Button from '@mui/material/Button';
5import TextField from '@mui/material/TextField';
6import FormControlLabel from '@mui/material/FormControlLabel';
7import Checkbox from '@mui/material/Checkbox';
8import Link from '@mui/material/Link';
9import Grid from '@mui/material/Grid';
10import Box from '@mui/material/Box';
11import Card from '@mui/material/Card';
12import CardContent from '@mui/material/CardContent';
13import CardActionArea from '@mui/material/CardActionArea';
14import CardActions from '@mui/material/CardActions';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040015
Adrien Béraudab519ff2022-05-03 15:34:48 -040016import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
17import Typography from '@mui/material/Typography';
18//import { makeStyles } from '@mui/material/styles';
19import Container from '@mui/material/Container';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040020import { Redirect } from 'react-router-dom';
Adrien Béraudab519ff2022-05-03 15:34:48 -040021import CircularProgress from '@mui/material/CircularProgress';
22import DialogTitle from '@mui/material/DialogTitle';
23import Dialog from '@mui/material/Dialog';
24import DialogActions from '@mui/material/DialogActions';
25import DialogContent from '@mui/material/DialogContent';
26import DialogContentText from '@mui/material/DialogContentText';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040027
28import authManager from '../AuthManager'
29
30function Copyright() {
31 return (
32 <Typography variant="body2" color="textSecondary" align="center">
33 {'Copyright © 2016-'}{new Date().getFullYear()}{' Savoir-faire Linux Inc.'}
34 <Link color="inherit" href="https://jami.net/">
35 Jami.net
36 </Link>{' '}
37 {'.'}
38 </Typography>
39 );
40}
41
42/*const useStyles = makeStyles((theme) => ({
43 paper: {
44 marginTop: theme.spacing(8),
45 display: 'flex',
46 flexDirection: 'column',
47 alignItems: 'center',
48 },
49 avatar: {
50 margin: theme.spacing(1),
51 backgroundColor: theme.palette.secondary.main,
52 },
53 form: {
54 width: '100%', // Fix IE 11 issue.
55 marginTop: theme.spacing(1),
56 },
57 submit: {
58 margin: theme.spacing(3, 0, 2),
59 },
60}));*/
61
62/*function SignIn() {
63 const classes = useStyles();
64
65
66}*/
67
68/*
69 TODO:
70 Use useState to handle username password and redirect states to render this page to
71 comply with material-ui usage of useStyles
72 Src: https://blog.logrocket.com/a-guide-to-usestate-in-react-ecb9952e406c/
73*/
74
75class SignInPage extends React.Component {
76
77 constructor(props) {
78 console.log("SignInPage " + props.open)
79 super(props)
80 this.state = {
81 /*username: '',
82 password: '',
83 redirect: false,
84 session: null,*/
85 submitted: false,
86 loading: false/*,
87 error: false,
88 open: false,
89 errorMessage: ''*/
90 }
91 this.handleSubmit = this.handleSubmit.bind(this);
92 this.localLogin = this.localLogin.bind(this);
93 }
94
95 handleusername(text) {
96 this.setState({ username: text.target.value })
97 }
98
99 handlePassword(text) {
100 this.setState({ password: text.target.value })
101 }
102
103 localLogin() {
104 this.setState({
105 submitted: true,
106 loading: true
107 })
Adrien Béraude74741b2021-04-19 13:22:54 -0400108 authManager.authenticate('admin', 'admin')
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400109 /*fetch('/api/localLogin?username=none&password=none', {
110 header: { "Content-Type": "application/json" },
111 method: "POST",
112 credentials: 'same-origin'
113 //body: JSON.stringify({ obj })
114 })
115 .then((res) => {
116 if (res.status === '200') {
117 this.setState({
118 redirect: true
119 });
120 } else if (res.status === '401') {
121 this.setState({
122 loading: false,
123 error: true,
124 open: true,
125 errorMessage: "Wrong credentials! Your are not allowed to connect"
126 })
127 }
128 //this.setState({ session: res });
129 }).catch((e) => {
130 this.setState({
131 loading: false,
132 error: true,
133 open: true,
134 errorMessage: e.toString()
135 })
136 })*/
137 }
138
139 handleSubmit(event) {
140 event.preventDefault();
141 let obj = {}
142 obj.username = this.state.username;
143 obj.password = this.state.password;
144
145 this.setState({
146 submitted: true,
147 loading: true
148 })
149
150 fetch('/api/login?username=' + obj.username + '&password=' + obj.password,
151 {
152 header: {
153 "Content-Type": "application/json"
154 },
155 method: "POST",
156 credentials: 'same-origin'
157 //body: JSON.stringify({ obj })
158 }
159 ).then((res) => {
160 if (res.status === '200') {
161 this.setState({
162 redirect: true
163 });
164 } else if (res.status === '401') {
165 this.setState({
166 loading: false,
167 error: true,
168 open: true,
169 errorMessage: "Wrong credentials! Your are not allowed to connect"
170 })
171 }
172 //this.setState({ session: res });
173 }).catch((e) => {
174 this.setState({
175 loading: false,
176 error: true,
177 open: true,
178 errorMessage: e.toString()
179 })
180 })
181 }
182
183 render() {
184 console.log("SignInPage render " + this.props.open)
185 return (
186 <Dialog open={this.props.open}>
187 <DialogTitle>Se connecter</DialogTitle>
188 <DialogContent>
189 <Button
190 type="submit"
191 fullWidth
192 variant="contained"
193 color="primary"
194 className=""/*{classes.submit}*/
195 onClick={() => { this.localLogin() }}
196 >
197 Compte local
198 </Button>
199
200 <TextField
201 variant="outlined"
202 margin="normal"
203 required
204 fullWidth
205 id="username"
206 label="LDAP Savoir-faire Linux"
207 name="username"
208 autoComplete="email"
209 autoFocus
210 onChange={(text) => { this.handleusername(text) }}
211 />
212 <TextField
213 variant="outlined"
214 margin="normal"
215 required
216 fullWidth
217 name="password"
218 label="Mot de passe"
219 type="password"
220 id="password"
221 autoComplete="current-password"
222 onChange={(text) => { this.handlePassword(text) }}
223 />
224 <FormControlLabel
225 control={<Checkbox value="remember" color="primary" />}
226 label="Se rapeller de moi"
227 />
228 </DialogContent>
229
230 <DialogActions>
231 <Button
232 type="submit"
233 size="medium"
234 onClick={this.handleSubmit}
235 >
236 Se connecter
237 </Button>
238 </DialogActions>
239 </Dialog>
240 );
241 }
242}
243
244
245export default SignInPage;