英文:
Supabase signup with react state email and password
问题
我正在尝试使用 react-jsx 实现标准的 Supabase 注册,但一直收到以下响应:
"您必须提供电子邮件或电话号码以及密码"
我的代码如下:
const [login, setLogin] = useState('')
const [password, setPassword] = useState('')
const signUpSubmitted = () => {
supabase.auth
.signUp({ login, password })
.then((response) => {response.error ? alert(response.error.message) : setToken(response)})
.catch((err) => { alert(err)})
}
以及表单:
<form id='sign-up'>
<h3>Sign Up</h3>
<label>Email:</label>
<input
type='email'
value={login}
onChange={(e) => setLogin(e.target.value)}
/>
<label>Password:</label>
<input
type='password'
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<input onClick={signUpSubmitted} type='submit'/>
</form>
我认为问题可能出在我尝试在传递给数据库之前将值保存在状态中。我不明白为什么会有问题,它们都应该是字符串,所以也许我理解错了。
英文:
I am trying to implement the standard Supabase signup, using react-jsx, but keep getting the response:
"You must provide either an email or phone number and a password"
My code looks as follows:
const [login, setLogin] = useState('')
const [password, setPassword] = useState('')
const signUpSubmitted = () => {
supabase.auth
.signUp({ login, password })
.then((response) => {response.error ? alert(response.error.message) : setToken(response)})
.catch((err) => { alert(err)})
}
and the form:
<form id='sign-up'>
<h3>Sign Up</h3>
<label>Email:</label>
<input
type='email'
value={login}
onChange={(e) => setLogin(e.target.value)}
/>
<label>Password:</label>
<input
type='password'
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<input onClick={signUpSubmitted} type='submit'/>
</form>
I assume the problem lies with me attempting to save the values in a state, before passing them to the database. I don't see why it should be a problem, they are both strings from what I understand, so maybe I'm way off.
答案1
得分: 1
根据Supabase文档,您需要传递email
而不是login
。
const { data, error } = await supabase.auth.signUp({
email: 'example@email.com',
password: 'example-password',
})
我还建议进行一些其他优化:
- 将您的提交处理程序移至
<form />
以支持使用Enter键提交表单。 - 在您的提交处理程序中添加
event.preventDefault()
以防止默认的表单浏览器重定向行为。 - 将您的提交输入更改为更语义化的
<button />
。 - 使用
htmlFor
和id
属性将标签和输入项链接在一起,以提高可访问性(只需确保它们是唯一的ID)。
更新后的组件:
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [token, setToken] = useState();
const handleSubmit = (event) => {
event.preventDefault();
supabase.auth
.signUp({ email, password })
.then((response) => {
response.error ? alert(response.error.message) : setToken(response)
})
.catch((err) => { alert(err) });
}
return (
<form id="sign-up" onSubmit={handleSubmit}>
<h3>Sign Up</h3>
<label htmlFor="email">Email:</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<label htmlFor="password">Password:</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Sign Up</button>
</form>
);
英文:
According to the Supabase docs, you need to pass email
instead of login
.
const { data, error } = await supabase.auth.signUp({
email: 'example@email.com',
password: 'example-password',
})
I would also suggest a few other optimizations:
- Move your submit handler to the
<form />
in order to support submitting the form with the Enter key. - Add
event.preventDefault()
to your submit handler to prevent the default form browser redirection behavior. - Change your submit input to a more semantic
<button />
. - Link your labels and inputs together with
htmlFor
andid
attributes for accessibility (just make sure they're unique ids).
Updated Component:
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [token, setToken] = useState();
const handleSubmit = (event) => {
event.preventDefault();
supabase.auth
.signUp({ email, password })
.then((response) => {
response.error ? alert(response.error.message) : setToken(response)
})
.catch((err) => { alert(err) });
}
return (
<form id="sign-up" onSubmit={handleSubmit}>
<h3>Sign Up</h3>
<label htmlFor="email">Email:</label>
<input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<label htmlFor="password">Password:</label>
<input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
<button type="submit">Sign Up</button>
</form>
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论