Docs 菜单
Docs 主页
/ /
Atlas Device SDKs
/ /

AppProvider (@realm/react)

在此页面上

  • Props 对象
  • 配置 AppProvider
  • 与 AppProvider 一起使用的钩子
  • useAuth()
  • 结果
  • logIn(credentials)
  • logInWithAnonymous()
  • logInWithApiKey(key)
  • logInWithEmailPassword(credentials)
  • logInWithJWT(credentials)
  • logInWithGoogle(credentials)
  • logInWithApple(idToken)
  • logInWithFacebook(accessToken)
  • logInWithFunction(payload)
  • logOut()
  • useEmailPasswordAuth()
  • logIn(credentials)
  • logOut()
  • 注册(args)
  • Confirm(args)
  • resendConfirmationEmail(args)
  • retryCustomConfirmation(args)
  • sendResetPasswordEmail(args)
  • resetPassword(args)
  • callResetPasswordFunction(args, restArgs)
  • useApp()
类型签名
AppProvider(props, context?): null | ReactElement<any, any>

嵌套在 AppProvider中的组件可以访问您的 App Services App 并使用AppProvider钩子。

AppConfiguration的所有属性都可以作为属性传递给AppProvider

要设置 App 客户端,请将 App ID 字符串传递给AppProviderid属性。 使用AppProvider封装需要访问应用的所有组件。

import React from 'react';
import {AppProvider} from '@realm/react';
function AppWrapper() {
return (
<View>
<AppProvider id={APP_ID}>
<MyApp />
</AppProvider>
</View>
);
}

您可以创建多个应用客户端实例以连接多个应用。共享相同 App ID 的所有 App 客户端实例使用相同的底层连接。

重要

初始化应用后更改应用配置

在版本realm@12.6.0中进行了更改:不缓存baseUrl

初始化 App 客户端时,会在内部缓存配置。 尝试在同一进程中“关闭”并重新打开配置已更改的应用无效。 客户端继续使用缓存的配置。

从 React Native SDK 版本12开始。 6 。 0 , baseUrl中的AppConfiguration不会被缓存。这意味着您可以更改baseUrl ,应用客户端将使用更新后的配置。在早期 SDK 版本中,对缓存的应用配置中的baseUrl进行更改无效。

类型签名
useAuth(): UseAuth

useAuth 为每个 App Services 身份验证提供商提供了一种身份验证方法。

类型签名
result: AuthResult

身份验证钩子操作的结果。 例如, result.operation将为您提供当前操作的名称。

枚举值

  • state。可以是“未启动”、“待处理”、“成功”、“错误”。

  • operation。有关所有操作名称的列表,请参阅API 文档。

  • pending。可以是truefalse

  • success。可以是truefalse

  • error。基于错误的对象或未定义。

类型签名
logIn(credentials: Realm.Credentials): void

参数

  • credentials。任何受支持的 Realm 身份验证提供的 Realm 档案。

例子

使用 Realm 支持的任何身份验证机制让用户登录。 如果在用户登录时调用,则当前用户会切换到新用户。 通常,最好使用更具体的登录方法。

const {logIn, result} = useAuth();
useEffect(() => logIn(Realm.Credentials.anonymous()), []);
if(result.pending) {
return (<LoadingSpinner/>)
}
if(result.error) {
return (<ErrorComponent/>)
}
if(result.success) {
return (<SuccessComponent/>)
}
//...
类型签名
logInWithAnonymous(): void

例子

使用匿名身份验证提供者登录。

const {logInWithAnonymous, result} = useAuth();
const performLogin = () => {
logInWithAnonymous();
};
类型签名
logInWithApiKey(key: string): void

参数

  • key。链接到 App Services 用户的字符串。

例子

使用 API 密钥登录。

const {logInWithApiKey, result} = useAuth();
const performLogin = () => {
const key = getApiKey(); // user defined function
logInWithApiKey(key);
};
类型签名
logInWithEmailPassword(credentials: {
email: string;
password: string;
}): void

参数

  • credentials。具有emailpassword字段的对象。

例子

使用电子邮件/密码登录。

const {logInWithEmailPassword, result} = useAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const performLogin = () => {
logInWithEmailPassword({email, password});
};
类型签名
logInWithJWT(token: string): void

参数

  • credentials。用户 JSON web token 的字符串表示形式。

例子

使用 JSON web token (JWT) 登录。

const {logInWithJWT, result} = useAuth();
const performLogin = () => {
const token = authorizeWithCustomerProvider(); // user defined function
logInWithJWT(token);
};
类型签名
logInWithGoogle(credentials: {
idToken: string;
} | {
authCode: string;
}): void;

参数

  • credentials。具有idTokenauthCode字段的对象,该字段应包含从 Google Identity Services 获取的字符串令牌。

例子

使用 Google 登录。

const {logInWithGoogle, result} = useAuth();
const performLogin = () => {
const token = getGoogleToken(); // user defined function
logInWithGoogle({idToken: token});
};
类型签名
logInWithApple(idToken: string): void;

参数

  • idToken。从 Apple SDK 获取的字符串。

例子

使用 Apple 登录。

const {logInWithApple, result} = useAuth();
const performLogin = () => {
const token = getAppleToken(); // user defined function
logInWithApple(token);
};
类型签名
logInWithFacebook(accessToken: string): void;

参数

  • accessToken。从 Facebook SDK 获取的字符串。

例子

使用 Facebook 登录。

const {logInWithFacebook, result} = useAuth();
const performLogin = () => {
const token = getFacebookToken(); // user defined function
logInWithFacebook(token);
};
类型签名
logInWithFunction<PayloadType extends Record<string, unknown>>(payload: PayloadType): void;

参数

  • payload。一个对象,其中包含要传递给处理登录请求的 App Services 函数的用户信息。

例子

使用自定义函数登录。

const {logInWithFunction, result} = useAuth();
const performLogin = () => {
const customPayload = getAuthParams(); // user defined arguments
logInWithFunction(customPayload);
};
类型签名
logOut(): void;

例子

注销当前用户。

const {logOut, result} = useEmailPasswordAuth();
const performLogout = () => {
logOut();
}
类型签名
result: AuthResult

身份验证钩子操作的结果。 例如, result.operation将为您提供当前操作的名称。

枚举值

  • state。可以是“未启动”、“待处理”、“成功”、“错误”。

  • operation。有关所有操作名称的列表,请参阅API 文档。

  • pending。可以是truefalse

  • success。可以是truefalse

  • error。基于错误的对象或未定义。

类型签名
logIn(credentials: { email: string; password: string }): void;

参数

  • credentials。包含emailpassword属性的对象。

例子

使用电子邮件和密码登录用户。 您也可以调用logIn(Realm.Credentials.emailPassword(email, password)) 。 返回已登录用户的Realm.User实例。

const {logIn, result} = useEmailPasswordAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const performLogin = () => {
logIn({email, password});
};
if(result.pending) {
return (<LoadingSpinner/>)
}
if(result.error) {
return (<ErrorComponent/>)
}
if(result.success) {
return (<SuccessComponent/>)
}
//...
类型签名
logOut(): void;

例子

注销当前用户。

const {logOut, result} = useEmailPasswordAuth();
const performLogout = () => {
logOut();
}
类型签名
register(args: { email: string; password: string }): void;

参数

  • args。包含emailpassword属性的对象。

例子

注册新用户。 需要用户电子邮件和密码。

const {register, result} = useEmailPasswordAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const performRegister = () => {
register({email, password});
};
类型签名
confirm(args: { token: string; tokenId: string }): void;

参数

  • args。包含tokentokenId属性的对象。

例子

确认用户帐户。 需要tokentokenId

const {confirm, result} = useEmailPasswordAuth();
const performConfirmation = () => {
confirm({token, tokenId});
};
类型签名
resendConfirmationEmail(args: { email: string }): void;

参数

  • args。包含email属性的对象。

例子

重新发送确认电子邮件。

const {resendConfirmationEmail, result} = useEmailPasswordAuth();
const [email, setEmail] = useState('');
const performResendConfirmationEmail = () => {
resendConfirmationEmail({email});
};
类型签名
retryCustomConfirmation(args: { email: string }): void;

参数

  • args。包含email属性的对象。

例子

使用自定义函数重试确认。

const {retryCustomConfirmation, result} = useEmailPasswordAuth();
const [email, setEmail] = useState('');
const performRetryCustomConfirmation = () => {
retryCustomConfirmation({email});
};
类型签名
sendResetPasswordEmail(args: { email: string }): void;

参数

  • args。包含email属性的对象。

例子

发送密码重置电子邮件。

const {sendResetPasswordEmail, result} = useEmailPasswordAuth();
const [email, setEmail] = useState('');
const performSendResetPasswordEmail = () => {
sendResetPasswordEmail({email});
};
类型签名
resetPassword(args: { token: string; tokenId: string; password: string }): void;

参数

  • args。包含tokentokenIdpassword属性的对象。

例子

重置用户密码。

const {resetPassword, result} = useEmailPasswordAuth();
const [password, setPassword] = useState('');
const performResetPassword = () => {
resetPassword({token, tokenId, password});
};
类型签名
callResetPasswordFunction<Args extends unknown[] = []>(
args: {
email: string;
password: string;
},
...restArgs: Args
): void;

参数

  • args。包含emailpassword属性的对象。

  • restArgs。您需要传递给自定义重置密码函数的其他参数。

例子

调用 App Services 后端密码重置函数。 可根据需要向函数传递参数。

const {callResetPasswordFunction, result} = useEmailPasswordAuth();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const performResetPassword = () => {
callResetPasswordFunction({email, password}, "extraArg1", "extraArg2");
};
类型签名
useApp<FunctionsFactoryType, CustomDataType>(): Realm.App<FunctionsFactoryType, CustomDataType>

例子

useApp()钩子提供对Realm.App实例的访问权限。

import React from 'react';
import {useApp} from '@realm/react';
function MyApp() {
const app = useApp();
// Proceed to app logic...
}

返回:

  • Realm.App AppProvider创建的当前Realm.App的实例。

后退

RealmProvider (@realm /react)