ee372f472f
* Add with-next-connect example * Update README * Fix code and lint * Fix typo * Run prettier * Include username * Rename example * Match with-passport styling * Add comments in code * Run prettier * Rewrite example * Add some comments * Update README.md * keys -> secret * Updated package.json and readme * UX changes * Securely encrypt cookie with @hapi/iron * Update README * Abstract db related actions and update README * security: add note on password hashing * remove unused dep * Updated readme Co-authored-by: Luis Alvarez <luis@zeit.co>
61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
import { useState, useEffect } from 'react'
|
|
import Router from 'next/router'
|
|
import Link from 'next/link'
|
|
import { useUser } from '../lib/hooks'
|
|
|
|
export default function LoginPage() {
|
|
const [user, { mutate }] = useUser()
|
|
const [errorMsg, setErrorMsg] = useState('')
|
|
|
|
async function onSubmit(e) {
|
|
e.preventDefault()
|
|
|
|
const body = {
|
|
username: e.currentTarget.username.value,
|
|
password: e.currentTarget.password.value,
|
|
}
|
|
const res = await fetch('/api/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
if (res.status === 200) {
|
|
const userObj = await res.json()
|
|
// set user to useSWR state
|
|
mutate(userObj)
|
|
} else {
|
|
setErrorMsg('Incorrect username or password. Try better!')
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
// redirect to home if user is authenticated
|
|
if (user) Router.push('/')
|
|
}, [user])
|
|
|
|
return (
|
|
<>
|
|
<h1>Login to Example</h1>
|
|
{errorMsg && <p className="error">{errorMsg}</p>}
|
|
<div className="form-container">
|
|
<form onSubmit={onSubmit}>
|
|
<label>
|
|
<span>Username</span>
|
|
<input type="text" name="username" required />
|
|
</label>
|
|
<label>
|
|
<span>Password</span>
|
|
<input type="password" name="password" required />
|
|
</label>
|
|
<div className="submit">
|
|
<button type="submit">Login</button>
|
|
<Link href="/signup">
|
|
<a>I don't have an account</a>
|
|
</Link>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</>
|
|
)
|
|
}
|