rsnext/examples/with-userbase/components/modal/index.js
Allen Hai 0c6ee2c760
Add userbase example (#12150)
* add userbase example

* update readme and add now.json with build.env

* update deploy button import path for testing

* update readme

* update readme with canary tree path for deploy button

* remove prepopulated test username + password

* Apply lint-fix

* add deployed demo link

* remove merge conflict arrows

* fix lint errors

* run yarn lint-fix

* add .env.example and update readme with config steps

* add dotenv and update next.config.js

Co-authored-by: Matthew Sweeney <mail@msweeneydev.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2020-04-23 19:56:42 -05:00

114 lines
2.9 KiB
JavaScript

import { useState, useEffect } from 'react'
import userbase from 'userbase-js'
function LoginModal({ toggle, modalType, setUser }) {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [loading, setLoading] = useState()
const [error, setError] = useState()
useEffect(() => {
setError('')
}, [modalType])
async function handleSignUp(e) {
e.preventDefault()
setLoading(true)
try {
const user = await userbase.signUp({
username,
password,
rememberMe: 'none',
})
setUser(user)
setLoading(false)
toggle(false)
} catch (e) {
setLoading(false)
setError(e.message)
}
}
async function handleLogIn(e) {
e.preventDefault()
setLoading(true)
try {
const user = await userbase.signIn({
username,
password,
rememberMe: 'none',
})
setUser(user)
setLoading(false)
toggle(false)
} catch (e) {
setLoading(false)
setError(e.message)
}
}
return (
<form className="bg-white shadow-md rounded p-8">
<div className="mb-4">
<label
className="block text-purple-700 text-sm font-bold mb-2"
htmlFor="username"
>
Username
</label>
<input
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="username"
type="text"
placeholder="Username"
value={username}
onChange={e => setUsername(e.target.value)}
/>
</div>
<div className="mb-4">
<label
className="block text-purple-700 text-sm font-bold mb-2"
htmlFor="username"
>
Password
</label>
<input
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="password"
type="password"
placeholder="*******"
value={password}
onChange={e => setPassword(e.target.value)}
/>
</div>
<div className="flex items-center justify-between">
<span
className="font-bold cursor-pointer"
onClick={() => toggle(false)}
>
Cancel
</span>
{modalType === 'logIn' ? (
<button
disabled={loading}
className="btn-yellow"
onClick={handleLogIn}
>
{loading ? 'Logging In ...' : 'Log In'}
</button>
) : (
<button
disabled={loading}
className="btn-yellow"
onClick={handleSignUp}
>
{loading ? 'Signing up ...' : 'Sign Up'}
</button>
)}
</div>
<p className="text-red-500 font-bold">{error}</p>
</form>
)
}
export default LoginModal