rsnext/examples/with-magic/pages/login.js
Paul van Dyk feb10f45c1
Update with-magic example (#19207)
fixes the event argument inside the function call
- argument is `e` but `event` was used instead
2020-11-16 16:50:58 +00:00

65 lines
1.5 KiB
JavaScript

import { useState } from 'react'
import Router from 'next/router'
import { useUser } from '../lib/hooks'
import Layout from '../components/layout'
import Form from '../components/form'
import { Magic } from 'magic-sdk'
const Login = () => {
useUser({ redirectTo: '/', redirectIfFound: true })
const [errorMsg, setErrorMsg] = useState('')
async function handleSubmit(e) {
e.preventDefault()
if (errorMsg) setErrorMsg('')
const body = {
email: e.currentTarget.email.value,
}
try {
const magic = new Magic(process.env.NEXT_PUBLIC_MAGIC_PUBLISHABLE_KEY)
const didToken = await magic.auth.loginWithMagicLink({
email: body.email,
})
const res = await fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + didToken,
},
body: JSON.stringify(body),
})
if (res.status === 200) {
Router.push('/')
} else {
throw new Error(await res.text())
}
} catch (error) {
console.error('An unexpected error happened occurred:', error)
setErrorMsg(error.message)
}
}
return (
<Layout>
<div className="login">
<Form errorMessage={errorMsg} onSubmit={handleSubmit} />
</div>
<style jsx>{`
.login {
max-width: 21rem;
margin: 0 auto;
padding: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
`}</style>
</Layout>
)
}
export default Login