AppProvider (@realm/react)
On this page
- Props
- Configure AppProvider
- Hooks Used with AppProvider
- useAuth()
- result
- logIn(credentials)
- logInWithAnonymous()
- logInWithApiKey(key)
- logInWithEmailPassword(credentials)
- logInWithJWT(credentials)
- logInWithGoogle(credentials)
- logInWithApple(idToken)
- logInWithFacebook(accessToken)
- logInWithFunction(payload)
- logOut()
- useEmailPasswordAuth()
- logIn(credentials)
- logOut()
- register(args)
- confirm(args)
- resendConfirmationEmail(args)
- retryCustomConfirmation(args)
- sendResetPasswordEmail(args)
- resetPassword(args)
- callResetPasswordFunction(args, restArgs)
- useApp()
AppProvider(props, context?): null | ReactElement<any, any>
Components nested within AppProvider
can access your App Services App
and use the AppProvider
hooks.
Props
All properties of AppConfiguration can be passed as props to AppProvider
.
Configure AppProvider
To set up your App client, pass the App ID string
to the id
prop of the AppProvider
.
Wrap any components that need to access the App with AppProvider
.
import React from 'react'; import {AppProvider} from '@realm/react'; function AppWrapper() { return ( <View> <AppProvider id={APP_ID}> <MyApp /> </AppProvider> </View> ); }
You can create multiple App client instances to connect to multiple Apps. All App client instances that share the same App ID use the same underlying connection.
Important
Changing an App Config After Initializing the App
Changed in version realm@12.6.0
: baseUrl
is not cached
When you initialize the App client, the configuration is cached internally. Attempting to "close" and then re-open an App with a changed configuration within the same process has no effect. The client continues to use the cached configuration.
Starting with React Native SDK version 12.6.0, the baseUrl
in the AppConfiguration
is not cached. This means that you can change the baseUrl
and the App client will use the updated configuration. In earlier SDK
versions, changes to the baseUrl
in a cached App configuration have no
effect.
Hooks Used with AppProvider
useAuth()
useAuth(): UseAuth
useAuth
has an authentication method for every App Services
authentication provider.
result
result: AuthResult
Result of authentication hook operation. For example, result.operation
gives
you the name of the current operation.
Enum values
state
. Can be "not-started", "pending", "success", "error".operation
. For a list of all operation names, refer to the API documentation.pending
. Can betrue
orfalse
.success
. Can betrue
orfalse
.error
. Error-based object or undefined.
logIn(credentials)
logIn(credentials: Realm.Credentials): void
Parameters
credentials
. A Realm credential supplied by any supported Realm authentication.
Example
Logs in a user with any authentication mechanism supported by Realm. If called when a user is logged in, the current user switches to the new user. Usually, it's better to use the more specific login methods.
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()
logInWithAnonymous(): void
Example
Log in with the anonymous authentication provider.
const {logInWithAnonymous, result} = useAuth(); const performLogin = () => { logInWithAnonymous(); };
logInWithApiKey(key)
logInWithApiKey(key: string): void
Parameters
key
. A string that is linked to an App Services user.
Example
Log in with an API key.
const {logInWithApiKey, result} = useAuth(); const performLogin = () => { const key = getApiKey(); // user defined function logInWithApiKey(key); };
logInWithEmailPassword(credentials)
logInWithEmailPassword(credentials: { email: string; password: string; }): void
Parameters
credentials
. An object withemail
andpassword
fields.
Example
Log in with Email/Password.
const {logInWithEmailPassword, result} = useAuth(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const performLogin = () => { logInWithEmailPassword({email, password}); };
logInWithJWT(credentials)
logInWithJWT(token: string): void
Parameters
credentials
. A string representation of a user's JWT.
Example
Log in with a JSON Web Token (JWT).
const {logInWithJWT, result} = useAuth(); const performLogin = () => { const token = authorizeWithCustomerProvider(); // user defined function logInWithJWT(token); };
logInWithGoogle(credentials)
logInWithGoogle(credentials: { idToken: string; } | { authCode: string; }): void;
Parameters
credentials
. An object with anidToken
orauthCode
field that should contain the string token you get from Google Identity Services.
Example
Log in with Google.
const {logInWithGoogle, result} = useAuth(); const performLogin = () => { const token = getGoogleToken(); // user defined function logInWithGoogle({idToken: token}); };
logInWithApple(idToken)
logInWithApple(idToken: string): void;
Parameters
idToken
. A string you get from the Apple SDK.
Example
Log in with Apple.
const {logInWithApple, result} = useAuth(); const performLogin = () => { const token = getAppleToken(); // user defined function logInWithApple(token); };
logInWithFacebook(accessToken)
logInWithFacebook(accessToken: string): void;
Parameters
accessToken
. A string you get from the Facebook SDK.
Example
Log in with Facebook.
const {logInWithFacebook, result} = useAuth(); const performLogin = () => { const token = getFacebookToken(); // user defined function logInWithFacebook(token); };
logInWithFunction(payload)
logInWithFunction<PayloadType extends Record<string, unknown>>(payload: PayloadType): void;
Parameters
payload
. An object that contains user information you want to pass to the App Services function that processes log in requests.
Example
Log in with a custom function.
const {logInWithFunction, result} = useAuth(); const performLogin = () => { const customPayload = getAuthParams(); // user defined arguments logInWithFunction(customPayload); };
logOut()
logOut(): void;
Example
Logs out the current user.
const {logOut, result} = useEmailPasswordAuth(); const performLogout = () => { logOut(); }
useEmailPasswordAuth()
result: AuthResult
Result of authentication hook operation. For example, result.operation
gives
you the name of the current operation.
Enum values
state
. Can be "not-started", "pending", "success", "error".operation
. For a list of all operation names, refer to the API documentation.pending
. Can betrue
orfalse
.success
. Can betrue
orfalse
.error
. Error-based object or undefined.
logIn(credentials)
logIn(credentials: { email: string; password: string }): void;
Parameters
credentials
. An object that containsemail
andpassword
properties.
Example
Logs a user in using an email and password. You could also call
logIn(Realm.Credentials.emailPassword(email, password))
. Returns a
Realm.User
instance of the logged-in 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()
logOut(): void;
Example
Logs out the current user.
const {logOut, result} = useEmailPasswordAuth(); const performLogout = () => { logOut(); }
register(args)
register(args: { email: string; password: string }): void;
Parameters
args
. An object that containsemail
andpassword
properties.
Example
Registers a new user. Requires a user email and password.
const {register, result} = useEmailPasswordAuth(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const performRegister = () => { register({email, password}); };
confirm(args)
confirm(args: { token: string; tokenId: string }): void;
Parameters
args
. An object that containstoken
andtokenId
properties.
Example
Confirms a user account. Requires a token
and tokenId
.
const {confirm, result} = useEmailPasswordAuth(); const performConfirmation = () => { confirm({token, tokenId}); };
resendConfirmationEmail(args)
resendConfirmationEmail(args: { email: string }): void;
Parameters
args
. An object that contains anemail
property.
Example
Resends a confirmation email.
const {resendConfirmationEmail, result} = useEmailPasswordAuth(); const [email, setEmail] = useState(''); const performResendConfirmationEmail = () => { resendConfirmationEmail({email}); };
retryCustomConfirmation(args)
retryCustomConfirmation(args: { email: string }): void;
Parameters
args
. An object that contains anemail
property.
Example
Retries confirmation with a custom function.
const {retryCustomConfirmation, result} = useEmailPasswordAuth(); const [email, setEmail] = useState(''); const performRetryCustomConfirmation = () => { retryCustomConfirmation({email}); };
sendResetPasswordEmail(args)
sendResetPasswordEmail(args: { email: string }): void;
Parameters
args
. An object that contains anemail
property.
Example
Sends a password reset email.
const {sendResetPasswordEmail, result} = useEmailPasswordAuth(); const [email, setEmail] = useState(''); const performSendResetPasswordEmail = () => { sendResetPasswordEmail({email}); };
resetPassword(args)
resetPassword(args: { token: string; tokenId: string; password: string }): void;
Parameters
args
. An object that containstoken
,tokenId
, andpassword
properties.
Example
Resets a user's password.
const {resetPassword, result} = useEmailPasswordAuth(); const [password, setPassword] = useState(''); const performResetPassword = () => { resetPassword({token, tokenId, password}); };
callResetPasswordFunction(args, restArgs)
callResetPasswordFunction<Args extends unknown[] = []>( args: { email: string; password: string; }, ...restArgs: Args ): void;
Parameters
args
. An object that containsemail
andpassword
properties.restArgs
. Additional arguments that you need to pass to your custom reset password function.
Example
Calls your App Services backend password reset function. Can pass arguments to the function as needed.
const {callResetPasswordFunction, result} = useEmailPasswordAuth(); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const performResetPassword = () => { callResetPasswordFunction({email, password}, "extraArg1", "extraArg2"); };
useApp()
useApp<FunctionsFactoryType, CustomDataType>(): Realm.App<FunctionsFactoryType, CustomDataType>
Example
The useApp()
hook provides access to a Realm.App
instance.
import React from 'react'; import {useApp} from '@realm/react';
function MyApp() { const app = useApp(); // Proceed to app logic... }
Returns
Realm.App
An instance of the currentRealm.App
created byAppProvider
.