Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | |
| 3 | import Avatar from '@material-ui/core/Avatar'; |
| 4 | import Button from '@material-ui/core/Button'; |
| 5 | import TextField from '@material-ui/core/TextField'; |
| 6 | import FormControlLabel from '@material-ui/core/FormControlLabel'; |
| 7 | import Checkbox from '@material-ui/core/Checkbox'; |
| 8 | import Link from '@material-ui/core/Link'; |
| 9 | import Grid from '@material-ui/core/Grid'; |
| 10 | import Box from '@material-ui/core/Box'; |
| 11 | import Card from '@material-ui/core/Card'; |
| 12 | import CardContent from '@material-ui/core/CardContent'; |
| 13 | import CardActionArea from '@material-ui/core/CardActionArea'; |
| 14 | import CardActions from '@material-ui/core/CardActions'; |
| 15 | |
| 16 | import LockOutlinedIcon from '@material-ui/icons/LockOutlined'; |
| 17 | import Typography from '@material-ui/core/Typography'; |
| 18 | //import { makeStyles } from '@material-ui/core/styles'; |
| 19 | import Container from '@material-ui/core/Container'; |
| 20 | import { Redirect } from 'react-router-dom'; |
| 21 | import CircularProgress from '@material-ui/core/CircularProgress'; |
| 22 | import DialogTitle from '@material-ui/core/DialogTitle'; |
| 23 | import Dialog from '@material-ui/core/Dialog'; |
| 24 | import DialogActions from '@material-ui/core/DialogActions'; |
| 25 | import DialogContent from '@material-ui/core/DialogContent'; |
| 26 | import DialogContentText from '@material-ui/core/DialogContentText'; |
| 27 | |
| 28 | import authManager from '../AuthManager' |
| 29 | |
| 30 | function 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 | |
| 75 | class 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 | }) |
| 108 | authManager.authenticate() |
| 109 | /*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 | |
| 245 | export default SignInPage; |