Supabase使用React状态注册电子邮件和密码

huangapple go评论66阅读模式
英文:

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(&#39;&#39;)
  const [password, setPassword] = useState(&#39;&#39;)

  const signUpSubmitted = () =&gt; {
    supabase.auth
      .signUp({ login, password })
      .then((response) =&gt; {response.error ? alert(response.error.message) : setToken(response)})
      .catch((err) =&gt; { alert(err)})
  }

and the form:

&lt;form id=&#39;sign-up&#39;&gt;
                &lt;h3&gt;Sign Up&lt;/h3&gt;
                &lt;label&gt;Email:&lt;/label&gt;
                &lt;input 
                type=&#39;email&#39; 
                value={login}
                onChange={(e) =&gt; setLogin(e.target.value)}
                /&gt;
                
                &lt;label&gt;Password:&lt;/label&gt;
                &lt;input 
                type=&#39;password&#39;
                value={password}
                onChange={(e) =&gt; setPassword(e.target.value)} 
                /&gt;

                &lt;input onClick={signUpSubmitted} type=&#39;submit&#39;/&gt;
            &lt;/form&gt;

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 />
  • 使用htmlForid属性将标签和输入项链接在一起,以提高可访问性(只需确保它们是唯一的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: &#39;example@email.com&#39;,
  password: &#39;example-password&#39;,
})

I would also suggest a few other optimizations:

  • Move your submit handler to the &lt;form /&gt; 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 &lt;button /&gt;.
  • Link your labels and inputs together with htmlFor and id attributes for accessibility (just make sure they're unique ids).

Updated Component:

const [email, setEmail] = useState(&#39;&#39;);
const [password, setPassword] = useState(&#39;&#39;);
const [token, setToken] = useState();

const handleSubmit = (event) =&gt; {
  event.preventDefault();
  supabase.auth
    .signUp({ email, password })
    .then((response) =&gt; {
      response.error ? alert(response.error.message) : setToken(response)
    })
    .catch((err) =&gt; { alert(err) });
}

return (
  &lt;form id=&quot;sign-up&quot; onSubmit={handleSubmit}&gt;
    &lt;h3&gt;Sign Up&lt;/h3&gt;

    &lt;label htmlFor=&quot;email&quot;&gt;Email:&lt;/label&gt;
    &lt;input
      id=&quot;email&quot;
      type=&quot;email&quot; 
      value={email}
      onChange={(e) =&gt; setEmail(e.target.value)}
    /&gt;
    
    &lt;label htmlFor=&quot;password&quot;&gt;Password:&lt;/label&gt;
    &lt;input
      id=&quot;password&quot;
      type=&quot;password&quot;
      value={password}
      onChange={(e) =&gt; setPassword(e.target.value)}
    /&gt;

    &lt;button type=&quot;submit&quot;&gt;Sign Up&lt;/button&gt;
  &lt;/form&gt;
);

huangapple
  • 本文由 发表于 2023年2月13日 23:16:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75437776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定