blob: 022562c3fd66005993fe65688aca1a4bc6ea49e2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
import styles from 'styles/Main.module.scss'
import { useState } from 'react'
import useUser from 'hooks/useUser'
import useSettings from 'hooks/useSettings'
import fetchJson from 'helpers/fetchJson'
import { Layout } from 'components'
import Splash from 'components/Splash'
const Verify = () => {
const { t } = useSettings()
const [errorMsg, setErrorMsg] = useState('')
const [loading, setLoading] = useState(false)
const [sending, setSending] = useState(false)
const [resent, setResent] = useState(false)
const { user, mutateUser } = useUser({
redirectToLogin: true,
redirectToApps: true
})
const handleSendMail = async e => {
e.preventDefault()
setErrorMsg('')
setSending(true)
try {
await fetch('/api/verify')
setResent(true)
} catch (error) {
setErrorMsg(t('verification_mail_error'))
} finally {
setSending(false)
}
}
const handleKey = async e => {
e.preventDefault()
setLoading(true)
const key = e.currentTarget.key.value
try {
mutateUser(
await fetchJson('/api/verify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ _id: user._id, key })
})
)
} catch (err) {
setErrorMsg(t('verification_error'))
} finally {
setLoading(false)
}
}
return (
<Layout>
{!user || loading
? (
<Splash fixed />
)
: (
<div className={`window window--popup ${styles.verify}`}>
<p>{t('verification_title')}</p>
<p>{`${t('verification_text')} ${user.email}`}</p>
<form onSubmit={handleKey}>
<input type='text' placeholder={t('verification_key')} name='key' />
<button className='window__button' type='submit'>{t('verify')}</button>
</form>
{
sending
? (
<p>{t('sending')}</p>
)
: (
resent
? (
<p>{t('verification_sent_again')}</p>
)
: (
<p>{t('verification_not_received')}
<span className={styles.email} onClick={handleSendMail}>{t('verification_send_again')}</span>.
</p>
)
)
}
{errorMsg && <p className={styles.error}>{errorMsg}</p>}
</div>
)}
</Layout>
)
}
export default Verify
|