generated from Code-the-Dream-School/react-node-practicum-front-template
-
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.
Merge pull request #37 from Code-the-Dream-School/TokenUserInfo
Token user info
- Loading branch information
Showing
9 changed files
with
160 additions
and
37 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,37 +1,83 @@ | ||
import { Link } from 'react-router-dom'; | ||
import { Link, Form, redirect, useNavigation, useActionData } from 'react-router-dom'; | ||
import styled from 'styled-components'; | ||
import { FormRow, Logo } from '../components'; | ||
import { toast } from 'react-toastify'; | ||
import customFetch from '../util/customFetch'; | ||
|
||
// export const action = async ({ request }) => { | ||
// const formData = await request.formData(); | ||
// const data = Object.fromEntries(formData); | ||
// const errors = { msg: '' }; | ||
// if (data.password.length < 3) { | ||
// errors.msg = 'Password too short'; | ||
// return errors; | ||
// } | ||
// try { | ||
// await customFetch.post('/auth/login', data); | ||
// toast.success('Login Successful'); | ||
// return redirect('/dashboard'); | ||
// } catch (error) { | ||
// toast.error(error?.response?.data?.msg); | ||
// } | ||
// return null; | ||
// }; | ||
|
||
export const action = async ({ request }) => { | ||
const formData = await request.formData(); | ||
const data = Object.fromEntries(formData); | ||
|
||
const errors = { msg: '' }; | ||
|
||
if (data.password.length < 3) { | ||
errors.msg = 'Password too short'; | ||
return errors; | ||
} | ||
|
||
try { | ||
const response = await customFetch.post('/auth/login', data); | ||
const { token, user } = response.data; | ||
//SET THEM IN LOCAL STORAGE | ||
localStorage.setItem('token', token); | ||
localStorage.setItem('userId', user.id); | ||
toast.success('Login Successful'); | ||
return redirect('/dashboard'); | ||
} catch (error) { | ||
// Handle errors | ||
toast.error(error?.response?.data?.msg || 'Something went wrong'); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const Login = () => { | ||
const errors = useActionData(); | ||
const navigation = useNavigation(); | ||
const isSubmitting = navigation.state === 'submitting'; | ||
|
||
return ( | ||
<Wrapper> | ||
<form className="form"> | ||
<Form method="post" className="form"> | ||
<Logo /> | ||
<h4>Login</h4> | ||
<FormRow | ||
type="email" | ||
name="email" | ||
labelText="email" | ||
defaultValue="[email protected]" | ||
placeholder="EMAIL" | ||
/> | ||
{errors?.msg && <p style={{ color: 'red' }}>{errors.msg}</p>} | ||
|
||
<FormRow type="email" name="email" labelText="email" placeholder="EMAIL" /> | ||
<FormRow | ||
type="password" | ||
name="password" | ||
labelText="password" | ||
defaultValue="CodeTheDreamPassword" | ||
placeholder="PASSWORD" | ||
placeholder="password" | ||
/> | ||
<button type="submit" className="btn btn-block"> | ||
Submit | ||
<button type="submit" className="btn btn-block" disabled={isSubmitting}> | ||
{isSubmitting ? 'submitting...' : 'submit'} | ||
</button> | ||
<p> | ||
Not Registered Yet? | ||
<Link to="/register" className="member-btn"> | ||
Register! | ||
</Link> | ||
</p> | ||
</form> | ||
</Form> | ||
</Wrapper> | ||
); | ||
}; | ||
|
@@ -56,7 +102,6 @@ const Wrapper = styled.section` | |
} | ||
.form-input { | ||
background-color: var(--grey-50); | ||
text-transform: uppercase; | ||
} | ||
h4 { | ||
text-align: center; | ||
|
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