stacker.news/components/login.js

108 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-10-15 18:05:34 +00:00
import { signIn } from 'next-auth/client'
import styles from './login.module.css'
import { Form, Input, SubmitButton } from '../components/form'
2022-06-02 22:55:23 +00:00
import { useState } from 'react'
2021-10-15 18:05:34 +00:00
import Alert from 'react-bootstrap/Alert'
import { useRouter } from 'next/router'
2023-01-18 18:49:20 +00:00
import { LightningAuthWithExplainer, SlashtagsAuth } from './lightning-auth'
import LoginButton from './login-button'
2023-02-08 19:38:04 +00:00
import { emailSchema } from '../lib/validate'
2021-10-15 18:05:34 +00:00
2023-01-10 00:33:44 +00:00
export function EmailLoginForm ({ text, callbackUrl }) {
2022-06-02 22:55:23 +00:00
return (
<Form
initial={{
email: ''
}}
2023-02-08 19:38:04 +00:00
schema={emailSchema}
2022-06-02 22:55:23 +00:00
onSubmit={async ({ email }) => {
signIn('email', { email, callbackUrl })
}}
>
<Input
label='Email'
name='email'
placeholder='email@example.com'
required
autoFocus
/>
2023-01-10 00:33:44 +00:00
<SubmitButton variant='secondary' className={styles.providerButton}>{text || 'Login'} with Email</SubmitButton>
2022-06-02 22:55:23 +00:00
</Form>
)
}
2023-01-10 00:33:44 +00:00
export default function Login ({ providers, callbackUrl, error, text, Header, Footer }) {
2021-10-15 18:05:34 +00:00
const errors = {
Signin: 'Try signing with a different account.',
OAuthSignin: 'Try signing with a different account.',
OAuthCallback: 'Try signing with a different account.',
OAuthCreateAccount: 'Try signing with a different account.',
EmailCreateAccount: 'Try signing with a different account.',
Callback: 'Try signing with a different account.',
OAuthAccountNotLinked: 'To confirm your identity, sign in with the same account you used originally.',
EmailSignin: 'Check your email address.',
2023-01-18 18:49:20 +00:00
CredentialsSignin: 'Auth failed',
2021-10-15 18:05:34 +00:00
default: 'Unable to sign in.'
}
const [errorMessage, setErrorMessage] = useState(error && (errors[error] ?? errors.default))
const router = useRouter()
if (router.query.type === 'lightning') {
2023-01-18 18:49:20 +00:00
return <LightningAuthWithExplainer callbackUrl={callbackUrl} text={text} />
}
if (router.query.type === 'slashtags') {
return <SlashtagsAuth callbackUrl={callbackUrl} text={text} />
}
2021-10-15 18:05:34 +00:00
return (
<div className={styles.login}>
{Header && <Header />}
{errorMessage &&
<Alert
variant='danger'
onClose={() => setErrorMessage(undefined)}
dismissible
>{errorMessage}
</Alert>}
{providers && Object.values(providers).map(provider => {
2023-01-18 18:49:20 +00:00
switch (provider.name) {
case 'Email':
return (
<div className='w-100' key={provider.name}>
<div className='mt-2 text-center text-muted font-weight-bold'>or</div>
<EmailLoginForm text={text} callbackUrl={callbackUrl} />
</div>
)
case 'Lightning':
case 'Slashtags':
return (
<LoginButton
className={`mt-2 ${styles.providerButton}`}
key={provider.name}
type={provider.name.toLowerCase()}
onClick={() => router.push({
pathname: router.pathname,
2023-05-03 00:22:23 +00:00
query: { callbackUrl: router.query.callbackUrl, type: provider.name.toLowerCase() }
2023-01-18 18:49:20 +00:00
})}
text={`${text || 'Login'} with`}
/>
)
default:
return (
<LoginButton
className={`mt-2 ${styles.providerButton}`}
key={provider.name}
type={provider.name.toLowerCase()}
onClick={() => signIn(provider.id, { callbackUrl })}
text={`${text || 'Login'} with`}
/>
)
}
})}
{Footer && <Footer />}
</div>
2021-10-15 18:05:34 +00:00
)
}