如何从Supabase默认表中获取用户ID,React Native中。

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

how to get userid from supabase default table react native

问题

在我的移动应用程序中,我有一个用户信息表,其中有一个主键,该主键引用了Supabase中的默认用户表,现在当尝试注册时,我无法插入到user_info表,因为我无法从auth.users中获取id。

如何获取用户ID以插入到user_info表中,以便它们可以关联?

const handlesignup = async () => {
  try {
    let { user, error } = await supabase.auth.signUp({
      email: email,
      password: password,
    });

    if (error) {
      console.log('注册错误:', error.message);
      return;
    }

    const { user1, error3 } = await supabase.auth.signInWithPassword({
      email: email,
      password: password,
    });

    if (error3) {
      console.log('登录错误:', error3.message);
      return;
    }

    const userId = user1?.id;

    const { data, error: insertError } = await supabase
      .from('user_info')
      .insert([
        {
          name: name,
          lname: lname,
          id: userId,
        },
      ]);

    if (insertError) {
      console.log('插入错误:', insertError.message);
      return;
    }

    console.log('注册成功!');
    console.log('用户ID:', userId);
  } catch (error) {
    console.log('错误:', error.message);
  }
};

希望这可以帮助您将用户ID插入到user_info表中。

英文:

in my mobile app I have a userinfo table witch has a primary key that references the default user table in supabase now when trying to sign up i can insert into user_info because I cant get the id from auth.users

const handlesignup = async () => {
try {
  
  let { user, error } = await supabase.auth.signUp({
    email: email,
    password: password,
  });

  if (error) {
    console.log('Sign up error:', error.message);
    return;
  }

  const {user1, error3 } = await supabase.auth.signInWithPassword({
    email: email,
    password: password,
  });

  if (error3) {
    console.log('Sign in error:', error3.message);
    return;
  }

  const userId = user1?.id;

  
  const { data, error: insertError } = await supabase
    .from('user_info')
    .insert([
      {
        name: name,
        lname: lname,
        id: userId,
      },
    ]);

  if (insertError) {
    console.log('Insert error:', insertError.message);
    return;
  }

 
  console.log('Sign up successful!');
  console.log('User ID:', userId);
} catch (error) {
  console.log('Error:', error.message);
}

};

how do i get the userid to insert into the user_info table so they can be linked

答案1

得分: 0

我使用了从登录返回的数据,这是我访问它的方式。

const handlesignup = async () => {
  try {
    let { error } = await supabase.auth.signUp({
      email: email,
      password: password,
    });

    if (error) {
      console.log('Sign up error:', error.message);
      return;
    }

    const { data, error: error2 } = await supabase.auth.signInWithPassword({
      email: email,
      password: password,
    });

    if (error2) {
      console.log('Sign in error:', error2.message);
      return;
    }

    const userId = data.user.id;

    const { data1, error: insertError } = await supabase
      .from('user_info')
      .insert([
        {
          name: name,
          lname: lname,
          id: userId,
        },
      ]);

    if (insertError) {
      console.log('Insert error:', insertError.message);
      return;
    }

    navigation.navigate('User-home');
  } catch (error) {
    console.log('Error:', error.message);
  }
};
英文:

i used the data returned from the login and that's how i accessed it

 const handlesignup = async () => {
try {
  let { error } = await supabase.auth.signUp({
    email: email,
    password: password,
  });

  if (error) {
    console.log('Sign up error:', error.message);
    return;
  }

  const { data, error:error2 } = await supabase.auth.signInWithPassword({
    email: email,
    password: password,
  })

  if (error2) {
    console.log('Sign in error:', error2.message);
    return;
  }

  
  const userId = data.user.id;
  
  //console.log('userId:', data.user);

  
  const { data1, error: insertError } = await supabase
    .from('user_info')
    .insert([
      {
        name: name,
        lname: lname,
        id: userId,
      },
    ]);

  if (insertError) {
    console.log('Insert error:', insertError.message);
    return;
  }

  navigation.navigate('User-home');
  //console.log('Sign up successful!');
  //console.log('User ID:', userId);
} catch (error) {
  console.log('Error:', error.message);
}

};

huangapple
  • 本文由 发表于 2023年6月22日 17:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530196.html
匿名

发表评论

匿名网友

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

确定