Guest Mode
Learn how to implement guest authentication in your application
Guest accounts allow users to immediately start using your application without going through a full registration process. This feature is particularly useful for applications where you want to reduce friction in the user onboarding process.
Key Features#
- They are locally persisted, so guest users can leave and return to the same account on the same device.
- Locally persisted sessions.
- Fully functional embedded wallets.
- Upgradeable to fully logged-in accounts.
- They can be logged out and deleted as needed.
User data and embedded wallets from guest sessions cannot be merged into an existing user account — guest accounts can only be upgraded into a new user account. If a guest user wants to log in with an existing account, you must delete the guest user session first.
Implementation#
Create a guest account (client-side)#
Use the registerGuest
method from the Openfort SDK to create a guest account:
_11import openfort from "./openfortConfig";_11_11async function handleGuest() {_11 try {_11 const data = await openfort.registerGuest();_11 // Handle successful guest registration_11 // The response includes player information and authentication tokens_11 } catch (error) {_11 // Handle error_11 }_11}
Upon successful registration, you'll receive a response containing the player information and authentication tokens:
_10{_10 "player": {_10 "id": "pla_...",_10 "object": "player",_10 "createdAt": 1234567890,_10 "linkedAccounts": []_10 },_10 "token": "eyJhbG...",_10 "refreshToken": "eyJhbG..."_10}
- Display guest access alongside normal login options
- Use clear labeling to distinguish between guest and full account creation
_12const LoginOptions = () => {_12 return (_12 <div className="space-y-4">_12 <Button onClick={handleGuest}>_12 Continue as Guest_12 </Button>_12 <Button onClick={() => router.push("/login")}>_12 Login or Create Account_12 </Button>_12 </div>_12 );_12};
Upgrade a guest user to a logged-in user#
Simply call link method to enable the guest user to upgrade their account to a logged-in account using any authentication method of their choice.
Example Implementation#
Here's a complete example of a guest authentication flow:
_50import openfort from "./openfortConfig";_50import { useRouter } from "next/router";_50import { useState } from "react";_50_50function GuestAuth() {_50 const router = useRouter();_50 const [status, setStatus] = useState(null);_50_50 const handleGuest = async () => {_50 setStatus({_50 type: "loading",_50 title: "Creating guest account...",_50 });_50_50 try {_50 const data = await openfort.registerGuest();_50 _50 setStatus({_50 type: "success",_50 title: "Guest account created",_50 });_50 _50 // Store credentials and redirect_50 openfort.storeCredentials({_50 player: data.player.id,_50 accessToken: data.token,_50 refreshToken: data.refreshToken,_50 });_50 _50 router.push("/");_50 } catch (error) {_50 setStatus({_50 type: "error",_50 title: "Error creating guest account",_50 });_50 }_50 };_50_50 return (_50 <div>_50 <Button onClick={handleGuest}>_50 {status?.type === "loading" ? (_50 <Loading />_50 ) : (_50 "Continue as Guest"_50 )}_50 </Button>_50 </div>_50 );_50}