2

我一直在尝试在裸 react-native 项目中使用以下库和 supabase 实现与谷歌和苹果的登录。

它们与使用 firebase sdk的此处所述的 firebase 配合得非常好。这个概念相当简单,apple 和 google sign 的本机 sdk 在成功登录后返回用户凭证。这是来自https://rnfirebase.io/auth/social-auth的示例

有谷歌凭证

import auth from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-google-signin/google-signin';

async function onGoogleButtonPress() {
  // Get the users ID token
  const { idToken } = await GoogleSignin.signIn();

  // Create a Google credential with the token
  const googleCredential = auth.GoogleAuthProvider.credential(idToken);

  // Sign-in the user with the credential
  return auth().signInWithCredential(googleCredential);
}

有苹果凭证

import auth from '@react-native-firebase/auth';
import { appleAuth } from '@invertase/react-native-apple-authentication';

async function onAppleButtonPress() {
  // Start the sign-in request
  const appleAuthRequestResponse = await appleAuth.performRequest({
    requestedOperation: appleAuth.Operation.LOGIN,
    requestedScopes: [appleAuth.Scope.EMAIL, appleAuth.Scope.FULL_NAME],
  });

  // Ensure Apple returned a user identityToken
  if (!appleAuthRequestResponse.identityToken) {
    throw 'Apple Sign-In failed - no identify token returned';
  }

  // Create a Firebase credential from the response
  const { identityToken, nonce } = appleAuthRequestResponse;
  const appleCredential = auth.AppleAuthProvider.credential(identityToken, nonce);

  // Sign the user in with the credential
  return auth().signInWithCredential(appleCredential);
}

我已经通过了 supabase 提供的身份验证supabase-js auth 我能找到的与上述实现相关的最接近的事情是使用刷新令牌,如此处所示 使用刷新令牌登录

// An example using Expo's `AuthSession`
const redirectUri = AuthSession.makeRedirectUri({ useProxy: false });
const provider = 'google';

AuthSession.startAsync({
  authUrl: `https://MYSUPABASEAPP.supabase.co/auth/v1/authorize?provider=${provider}&redirect_to=${redirectUri}`,
  returnUrl: redirectUri,
}).then(async (response: any) => {
  if (!response) return;
  const { user, session, error } = await supabase.auth.signIn({
    refreshToken: response.params?.refresh_token,
  });
});

上面的示例使用refreshToken参数 auth.sign()为已注册的用户创建会话。

如果我错了,请纠正我。根据 auth 文档,没有一种方法可以让人们使用 auth 凭证创建用户。这是因为 gotrue social auth 是如何实现的。我现在能想到的唯一解决方案是将整个身份验证逻辑移动到后端并在网络上处理它。这一点都不优雅,因为它必须涉及丑陋的深度链接或 webview 实现才能使用 supbase 会话和访问令牌。

鉴于上述情况,有没有办法在 react-native 中使用本机社交 sdk 使用用户凭据在 supabase 中创建一个新用户帐户,而不必强制他们使用电子邮件链接、密码或网络?欢迎任何想法,答案或更正。

4

1 回答 1

0

在撰写本文时,使用第三方凭据登录尚未在 gotrue(用于 supabase 中的身份验证的模块)中实现。如果你有一个原生应用、flutter 应用和 react-native 应用需要使用凭证进行社交验证,请跟进这个WIP:PR。有关详细信息,请参阅代码交换证明密钥 (PKCE)

于 2021-09-24T11:25:49.827 回答