MoonSDK supports OAuth2 authentication with various providers, allowing users to log in using their existing accounts on these platforms. This documentation page explains how to use the MoonSDK class functions for OAuth2 authentication and provides examples of the process of redirection to code exchange for a session token.
MoonSDK supports the following OAuth2 providers:
- Discord
- GitHub
The OAuth2 authentication process involves the following steps:
- Redirect the user to the OAuth2 login page of the provider.
- The user logs in to their account on the provider's website.
- The provider redirects the user back to your application with a code.
- Exchange the code for a session token.
Here's an example of how to use the performDiscordOAuth
and performDiscordOauthCodeExchange
functions to authenticate a user with Discord:
- Redirect the user to the Discord OAuth2 login page:
import { MoonSDK } from '@moonup/moon-sdk';
const sdk = new MoonSDK({
clientId: 'YOUR_CLIENT_ID',
});
sdk.performDiscordOAuth();
- The user logs in to their Discord account and is redirected back to your application with a code in the URL query string.
- Exchange the code for a session token:
const code = new URLSearchParams(window.location.search).get('code');
const token = await sdk.performDiscordOauthCodeExchange(code);
The performDiscordOauthCodeExchange
function sends a request to the Discord API to exchange the code for a session token and returns the token.
Here's an example of how to use the performGithubOAuth
and performGithubOauthCodeExchange
functions to authenticate a user with GitHub:
- Redirect the user to the GitHub OAuth2 login page:
import { MoonSDK } from '@moonup/moon-sdk';
const sdk = new MoonSDK({
clientId: 'YOUR_CLIENT_ID',
});
sdk.performGithubOAuth();
- The user logs in to their GitHub account and is redirected back to your application with a code in the URL query string.
- Exchange the code for a session token:
const code = new URLSearchParams(window.location.search).get('code');
const token = await sdk.performGithubOauthCodeExchange(code);
The performGithubOauthCodeExchange
function sends a request to the GitHub API to exchange the code for a session token and returns the token.
Here's an example of how to use the performGoogleOAuth
and performGoogleOauthCodeExchange
functions to authenticate a user with Google:
- Redirect the user to the Google OAuth2 login page:
import { MoonSDK } from '@moonup/moon-sdk';
const sdk = new MoonSDK({
clientId: 'YOUR_CLIENT_ID',
});
sdk.performGoogleOAuth();
- The user logs in to their Google account and is redirected back to your application with a code in the URL query string.
- Exchange the code for a session token:
const code = new URLSearchParams(window.location.search).get('code');
const token = await sdk.performGoogleOauthCodeExchange(code);
The performGoogleOauthCodeExchange
function sends a request to the Google API to exchange the code for a session token and returns the token.
Here's an example of how to use the performTwitterOauth
and performTwitterOauthCodeExchange
functions to authenticate a user with Twitter:
- Redirect the user to the Twitter OAuth2 login page:
import { MoonSDK } from '@moonup/moon-sdk';
const sdk = new MoonSDK({
clientId: 'YOUR_CLIENT_ID',
});
sdk.performTwitterOauth();
- The user logs in to their Twitter account and is redirected back to your application with a code in the URL query string.
- Exchange the code for a session token:
const code = new URLSearchParams(window.location.search).get('code');
const token = await sdk.performTwitterOauthCodeExchange(code);
The performTwitterOauthCodeExchange
function sends a request to the Twitter API to exchange the code for a session token and returns the token.
MoonSDK supports OAuth2 authentication with various providers, allowing users to log in using their existing accounts on these platforms. This documentation page explains how to use the MoonSDK class functions for OAuth2 authentication and provides examples of the process of redirection to code exchange for a session token. Whether you're building a new application or looking to improve the authentication flow of an existing one, MoonSDK's OAuth2 support is a great choice.