-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
215 additions
and
61 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
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,65 @@ | ||
import React, { useState } from 'react'; | ||
import axios from 'axios'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
function LoginSignupForm() { | ||
const [formData, setFormData] = useState({ | ||
username: '', | ||
password: '', | ||
}); | ||
|
||
const { username, password } = formData; | ||
|
||
const onChange = e => | ||
setFormData({ ...formData, [e.target.name]: e.target.value }); | ||
|
||
const onSubmit = async e => { | ||
e.preventDefault(); | ||
|
||
const config = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
|
||
const body = JSON.stringify({ username, password }); | ||
|
||
try { | ||
// Use axios to send a POST request to the /login or /signup route | ||
// depending on whether the user is trying to login or sign up | ||
const res = await axios.post( | ||
'/login', | ||
body, | ||
config | ||
); | ||
|
||
console.log(res.data); | ||
} catch (err) { | ||
console.error(err.response.data); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={e => onSubmit(e)}> | ||
<h1>Login</h1> | ||
<input | ||
type='text' | ||
placeholder='Username' | ||
name='username' | ||
value={username} | ||
onChange={e => onChange(e)} | ||
/> | ||
<input | ||
type='password' | ||
placeholder='Password' | ||
name='password' | ||
value={password} | ||
onChange={e => onChange(e)} | ||
/> | ||
<button type='submit'>Log In</button> | ||
<Link to='/signup'>Sign Up</Link> | ||
</form> | ||
); | ||
} | ||
|
||
export default LoginSignupForm; |
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,64 @@ | ||
import React, { useState } from 'react'; | ||
import axios from 'axios'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
function SignUpPage() { | ||
const [formData, setFormData] = useState({ | ||
username: '', | ||
password: '', | ||
}); | ||
|
||
const { username, password } = formData; | ||
|
||
const onChange = e => | ||
setFormData({ ...formData, [e.target.name]: e.target.value }); | ||
|
||
const onSubmit = async e => { | ||
e.preventDefault(); | ||
|
||
const config = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
|
||
const body = JSON.stringify({ username, password }); | ||
|
||
try { | ||
// Use axios to send a POST request to the /signup route | ||
const res = await axios.post( | ||
'/register', | ||
body, | ||
config | ||
); | ||
|
||
console.log(res.data); | ||
} catch (err) { | ||
console.error(err.response.data); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={e => onSubmit(e)}> | ||
<h1>Sign Up</h1> | ||
<input | ||
type='text' | ||
placeholder='Username' | ||
name='username' | ||
value={username} | ||
onChange={e => onChange(e)} | ||
/> | ||
<input | ||
type='password' | ||
placeholder='Password' | ||
name='password' | ||
value={password} | ||
onChange={e => onChange(e)} | ||
/> | ||
<button type='submit'>Sign Up</button> | ||
<Link to='/login'>Log In</Link> | ||
</form> | ||
); | ||
} | ||
|
||
export default SignUpPage; |