scrimba
Frontend Career Path
Advanced React
React Router
Authentication setup - sad path (FDCP)
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Authentication setup - sad path (FDCP)
AboutCommentsNotes
Authentication setup - sad path (FDCP)
Expand for more info
pages
Login.jsx
run
preview
console
import React from "react"
import { useNavigate, useLocation } from "react-router-dom"
import { loginUser } from "../api"

/**
* Challenge: Code the sad path 🙁
* 3. Add a `status` state and default it to "idle". When the
* login form begins submitting, set it to "submitting". When
* it's done submitting and the data has come back, set it
* to "idle" again
* 4. Disable the button when the `status` state is "submitting"
* (this may require some Googling if you haven't done this
* before).
* 5. Add an `error` state and default it to `null`. When the
* form is submitted, reset the errors to `null`. If there's
* an error from `loginUser` (add a .catch(err => {...}) in
* the promise chain), set the error to the error that
* comes back.
* 6. Display the error.message below the `h1` if there's ever
* an error in state
*/

export default function Login() {
const [loginFormData, setLoginFormData] = React.useState({ email: "", password: "" })
const location = useLocation()

function handleSubmit(e) {
e.preventDefault()
loginUser(loginFormData)
.then(data => console.log(data))
}

function handleChange(e) {
const { name, value } = e.target
setLoginFormData(prev => ({
...prev,
[name]: value
}))
}

return (
<div className="login-container">
{
location.state?.message &&
<h3 className="login-first">{location.state.message}</h3>
}
<h1>Sign in to your account</h1>
<form onSubmit={handleSubmit} className="login-form">
<input
name="email"
onChange={handleChange}
type="email"
placeholder="Email address"
value={loginFormData.email}
/>
<input
name="password"
onChange={handleChange}
type="password"
placeholder="Password"
value={loginFormData.password}
/>
<button>Log in</button>
</form>
</div>
)

}
Console
{user:
{id:
"123"
, email:
"b@b.com"
, name:
"Bob"
}
, token:
"Enjoy your pizza, here's your tokens."
}
,
/login
-7:28