-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from polito-WA1-AW1-2021/with_auth
now with authentication
- Loading branch information
Showing
5 changed files
with
159 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ It will be developed in phases, in several weeks, to illustrate the different fe | |
* _Phase 2_: add a form and its management. | ||
* _Phase 3_: edit form and client-side routing. The application now has 3 different pages. | ||
* _Phase 4_: add interaction with the server for all the operations. Basic error handling. | ||
* _Phase 5_: add login and logout. Sample credentials: [email protected] (psw: student) and [email protected] (psw: student). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Form, Button, Alert, Col } from 'react-bootstrap'; | ||
import { useState } from 'react'; | ||
//import { Redirect } from 'react-router'; | ||
|
||
function LoginForm(props) { | ||
const [username, setUsername] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
const [errorMessage, setErrorMessage] = useState('') ; | ||
|
||
const handleSubmit = (event) => { | ||
event.preventDefault(); | ||
setErrorMessage(''); | ||
const credentials = { username, password }; | ||
|
||
// SOME VALIDATION, ADD MORE!!! | ||
let valid = true; | ||
if(username === '' || password === '' || password.length < 6) | ||
valid = false; | ||
|
||
if(valid) | ||
{ | ||
props.login(credentials); | ||
} | ||
else { | ||
// show a better error message... | ||
setErrorMessage('Error(s) in the form, please fix it.') | ||
} | ||
}; | ||
|
||
return ( | ||
<Form> | ||
{errorMessage ? <Alert variant='danger'>{errorMessage}</Alert> : ''} | ||
<Form.Group controlId='username'> | ||
<Form.Label>email</Form.Label> | ||
<Form.Control type='email' value={username} onChange={ev => setUsername(ev.target.value)} /> | ||
</Form.Group> | ||
<Form.Group controlId='password'> | ||
<Form.Label>Password</Form.Label> | ||
<Form.Control type='password' value={password} onChange={ev => setPassword(ev.target.value)} /> | ||
</Form.Group> | ||
<Button onClick={handleSubmit}>Login</Button> | ||
</Form>) | ||
} | ||
|
||
function LogoutButton(props) { | ||
return( | ||
<Col> | ||
<Button variant="outline-primary" onClick={props.logout}>Logout</Button> | ||
</Col> | ||
) | ||
} | ||
|
||
export { LoginForm, LogoutButton }; |