Social Login (OAuth)
Use initOAuth() to initiate OAuth sign-in. Provide a redirectTo URL that points to a callback route in your application.
The supported providers are GOOGLE, APPLE, TWITTER, DISCORD, FACEBOOK, LINE, and EPIC_GAMES.
Initiate OAuth
Call initOAuth with the provider and redirect URL. The method returns the OAuth authorization URL as a string:
import { OAuthProvider } from '@openfort/openfort-js';
import openfort from "./openfortConfig"
function AuthButton() {
return (
<button onClick={async () => {
const url = await openfort.auth.initOAuth({
provider: OAuthProvider.GOOGLE,
redirectTo: 'https://your-website.com/auth/callback',
});
window.location.href = url;
}}>
Continue with Google
</button>
);
}Handle callback
After the user completes the OAuth flow, they're redirected to your redirectTo URL with query parameters containing the session token and user ID:
https://your-website.com/auth/callback?token=...&user_id=...
Store the credentials to complete authentication:
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
const userId = urlParams.get('user_id');
openfort.auth.storeCredentials({
token: token,
userId: userId,
});Login with ID token
Use logInWithIdToken to authenticate users who already have an OAuth token from an external provider:
import { OAuthProvider } from '@openfort/openfort-js';
import openfort from "./openfortConfig"
async function loginWithExternalToken(token: string) {
const result = await openfort.auth.logInWithIdToken({
provider: OAuthProvider.GOOGLE,
token: token,
});
console.log('User logged in:', result.user.id);
}