2

替代标题是“如何检查通过特定身份验证提供程序登录的已登录 Realm 用户?” 或“如何检查用户是否使用特定的身份验证提供程序/方法?”


对于从匿名用户开始的应用程序,然后使用user.linkUser(credentials: credential). 由于user始终具有匿名用户或链接用户的值。

我如何知道当前登录的用户是否已经与另一个身份验证提供程序链接,例如“使用 Apple 登录”或“Google”?需要知道此信息才能隐藏身份验证提供程序登录按钮。

4

2 回答 2

1

在 RealmSwift 10.12.0 中

下有一个identifiers属性user。它是一个数组RLMUserIdentity。用户身份包含一个providerType字符串,https://docs.mongodb.com/realm-sdks/objc/latest/Classes/RLMUserIdentity.html#/c: objc(cs)RLMUserIdentity(py)providerType

下面是一个示例输出

print(">>> DEBUG:", user.identities.map { identity in (identity.identifier, identity.providerType)

[("611a27f9a1575af5ed15234e-lnnaeteekatdftrnsmpbpldr", "anon-user"), ("000766.23cbd125344c140b18ef0baa4deccaf32.61234", "oauth2-apple")]

现在您可以检查用户身份是否包含您关心的提供者并隐藏该提供者的“登录”按钮/链接

https://github.com/realm/realm-cocoa/blob/d407cdc1c8be5f04c3decd37b88524855edfa7e8/Realm/RLMCredentials.mm

于 2021-08-16T09:14:26.940 回答
0

当你初始化一个领域应用程序时,它默认检查访问令牌、刷新令牌和其他领域在成功登录后将用户数据存储在设备上的东西。因此,声明当前用户时的默认值应该从 Realm app instance 中检索。就我而言,我使用 Web 开发,对于 React 应用程序看起来像这样。

import React, { useState } from 'react';    
import LoginPage from 'containers/LoginPage';
import { RealmApp, getCustomCredentials } from './RealmApp';

function App() {
  const [currentUser, setCurrentUser] = useState(RealmApp.currentUser);

  const onAuth = async (data) => {
    const credentials = getCustomCredentials(data);
    const user = await RealmApp.logIn(credentials);
    setCurrentUser(user);
  };

  const App = () => {
    return (
      <Box className="app-root-component" sx={{ display: 'flex' }}>
        <h1>App</>
      </Box>
    );
  };

  return currentUser ? <App /> : <LoginPage onAuth={onAuth} />;
}

export default App;

在这里我有我的变量&它的 setter 函数

const [currentUser, setCurrentUser] = useState(RealmApp.currentUser);

我希望这可以帮助您与苹果登录更接近

于 2021-12-22T22:03:01.717 回答