-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthOverlay.jsx
118 lines (108 loc) · 4.92 KB
/
AuthOverlay.jsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React, { useEffect, useState } from "react"
import { useForm } from "react-hook-form";
import { useIdentityContext } from 'react-netlify-identity-gotrue'
import LoginForm from './LoginForm'
const AuthOverlay = () => {
const identity = useIdentityContext()
const { register, handleSubmit, errors } = useForm()
const [formError, setFormError] = useState()
const [formProcessing, setFormProcessing] = useState(false)
const [forceShowOverlay, setForceShowOverlay] = useState(false)
useEffect(() => {
if (identity.provisionalUser) {
setForceShowOverlay('Please check your email for an account confirmation email!')
const timeoutId = setTimeout(() => setForceShowOverlay(false), 5000)
return () => clearTimeout(timeoutId)
}
}, [identity.provisionalUser])
const onSubmit = async (data) => {
setFormProcessing(true)
setFormError()
await identity.completeUrlTokenTwoStep(data)
.catch(_ => setFormError('Having an issue.. please try later'))
setFormProcessing(false)
}
return (
<>
{(identity.urlToken || forceShowOverlay) &&
<div className="w-full h-full fixed block top-0 left-0 bg-gray-200 bg-opacity-75 z-50 flex justify-center items-center">
<div className="w-full p-4 max-w-xs opacity-100 bg-white shadow-xl rounded-lg">
{identity.urlToken?.type === "confirmation" &&
<p>Confirming User...</p>
}
{identity.urlToken?.type === "email_change" && (
identity.user
? <p>Changing Email...</p>
: <>
<p>In order to confirm your email change, you must log in with your prior credentials.</p>
<LoginForm />
</>
)}
{forceShowOverlay &&
<p>{forceShowOverlay}</p>
}
{(identity.urlToken?.type === "passwordRecovery" || identity.urlToken?.type === "invite") &&
<>
{identity.urlToken.type === "passwordRecovery" &&
<h2>Reset Password</h2>
}
{identity.urlToken.type === "invite" &&
<>
<h2>Welcome</h2>
<p className="mb-0">Let's complete the rest of your account info</p>
</>
}
<form className="pt-6" onSubmit={handleSubmit(onSubmit)}>
{identity.urlToken.type === "invite" &&
<div className="mb-2">
<label htmlFor="user_metadata.full_name" className="block text-gray-700 text-sm font-bold mb-2">
Name
</label>
<input
ref={register({ required: true })}
className={`shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline ${formProcessing && 'opacity-75'}`}
disabled={formProcessing}
name="user_metadata.full_name"
type="text"
placeholder="Jane Doe">
</input>
{errors.password && <p className="text-red-500 text-xs italic">Password is required</p>}
</div>
}
<div className="mb-6">
<label htmlFor="password" className="block text-gray-700 text-sm font-bold mb-2">
New Password
</label>
<input
ref={register({ required: true })}
className={`shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline ${formProcessing && 'opacity-75'}`}
disabled={formProcessing}
name="password"
type="password"
placeholder="******************">
</input>
{errors.password && <p className="text-red-500 text-xs italic">Password is required</p>}
</div>
<div className="flex items-center justify-between">
<button
className={`bg-blue-500 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline ${formProcessing ? 'opacity-75 cursor-not-allowed' : 'hover:bg-blue-700'}`}
disabled={formProcessing}
type="submit">
Set New Password
</button>
</div>
{formError &&
<div className="pt-2">
<p className="text-red-500 text-xs italic">Oops! We're having trouble right now.</p>
</div>
}
</form>
</>
}
</div>
</div>
}
</>
)
}
export default AuthOverlay