githubEdit

Context

MoonContextProvider Documentation

The MoonContextProvider is a React context provider that provides access to the Moon SDK and Ethers.js provider instances, as well as various authentication and session management functions. This documentation page will guide you through the basics of using the MoonContextProvider in your React Native project.

Table of Contents

  • Installation

  • Usage

  • Context Values

  • Example

Installation

To use the MoonContextProvider in your project, you'll need to install the @moonup/moon-react-native package. You can install it using npm:

npm install @moonup/moon-react-native

Usage

To use the MoonContextProvider, you'll need to wrap your application with it. Here's an example of how to do this:

import React from 'react';
import { MoonContextProvider } from '@moonup/moon-react-native';
import App from './App';

export default function Main() {
  return (
    <MoonContextProvider>
      <App />
    </MoonContextProvider>
  );
}

API Reference

Context Provider Functions

connect(accessToken?: string, refreshToken?: string)

Connects to the Moon API using the provided access and refresh tokens.

Example:

disconnect()

Disconnects from the Moon API.

Example:

getUserSession()

Retrieves the current user session.

Example:

connectEthers()

Connects to the Ethers.js provider.

Example:

disconnectEthers()

Disconnects from the Ethers.js provider.

Example:

request(args: RequestArguments)

Sends a request to the Ethers.js provider.

Example:

signUp(email: string, password: string)

Signs up a new user with the provided email and password.

Example:

signInWithPassword(email: string, password: string)

Signs in a user with the provided email and password.

Example:

signOut()

Signs out the current user.

Example:

signInWithDiscord()

Signs in a user using Discord OAuth.

Example:

signInWithGithub()

Signs in a user using GitHub OAuth.

Example:

signInWithTwitter()

Signs in a user using Twitter OAuth.

Example:

signInWithGoogle()

Signs in a user using Google OAuth.

Example:

sendMagicLink(email: string)

Sends a magic link to the provided email address.

Example:

Context Provider Variables

user

The current user object, or null if the user is not authenticated.

Example:

session

The current session object, or null if the user is not authenticated.

Example:

initialized

A boolean value indicating whether the authentication state has been initialized.

Example:

moon

An instance of the Moon SDK.

Example:

ethers

An instance of the Ethers.js provider.

Example:

Example

Here's an example of how to use the MoonContextProvider to sign in a user with Discord OAuth:

In this example, we import the useMoon hook from the @moonup/moon-react-native package and use it to access all of the context provider functions and variables. We then use these functions and variables to handle various authentication and session management tasks, such as signing up a new user, signing in a user with a password, signing out a user, connecting to the Moon API, disconnecting from the Moon API, getting the current user session, connecting to the Ethers.js provider, disconnecting from the Ethers.js provider, sending a request to the Ethers.js provider, and sending a magic link to a user's email address.

In this example, we import the useMoon hook from the @moonup/moon-react-native package and use it to access the signInWithDiscord function. We then use this function to sign in a user with Discord OAuth when the button is pressed.

Last updated