Skip to main content

Authentication

Login is the only flow the game implements. There are two ways to do it:

  1. Built-in UI (recommended) — let the SDK render the login/register screen.
  2. Headless — call the auth methods yourself and draw your own UI.

After a successful login the SDK automatically shows its floating button, which handles top-up, account, and logout — so for most games, step 1 is all you need.

const sdk = await OEGWebSdk.init({ gameId: 38, baseUrl: 'https://api-sdk.oeg.vn' });

// Opens the SDK login modal (username/password, register, guest, and any
// social providers enabled for your game in the OEG CMS).
const result = await sdk.openLogin();
if (result.success) {
console.log('Logged in:', result.user?.username);
}

openLogin() reads your game's remote config and shows only the providers you've enabled. If the player is already logged in, it opens the account screen instead.

Social login is automatic

Google and Facebook run in the browser with no extra game code — just enable them for your game in the CMS. (Apple and TikTok web flows are not available yet.)

Option 2 — Headless (custom UI)

Use these when you build your own login screen. All methods return an AuthResult.

import { OEGAuth } from '@alogame/web-sdk';

// Username / password
const r1 = await OEGAuth.login('username', 'password');

// Register a new account — note: (fullname, username, password)
const r2 = await OEGAuth.register('Display Name', 'username', 'Password123!');

// Guest / Play Now
const r3 = await OEGAuth.loginGuest();

// Social — the game completes the OAuth flow and passes the access token
const r4 = await OEGAuth.socialLogin('google', googleAccessToken);

if (r1.success) {
console.log('User:', r1.user);
} else {
console.error(r1.error?.code, r1.error?.message);
}

Reacting to auth state

Subscribe once and update your game UI whenever the player logs in or out. This is also where the SDK shows/hides the floating button for you.

import { OEGAuth } from '@alogame/web-sdk';

const unsubscribe = OEGAuth.onAuthStateChanged((user) => {
if (user && !user.isPlayNow) {
showGame(user.username); // logged in
} else {
showLoginButton(); // logged out / guest
}
});

// Synchronous checks
if (OEGAuth.isLoggedIn) {
console.log('Current user:', OEGAuth.currentUser);
}

// Later, e.g. on scene teardown
unsubscribe();

Bind the player's game role (optional)

If your game has servers/characters, report the active role after login so Alogame can attribute sessions and payments correctly:

await OEGAuth.setGameRole(
'server-01', // serverId
'role-12345', // roleId
'Asia 1', // serverName (optional)
'DragonSlayer',// roleName (optional)
42 // level (optional)
);

Logout

await OEGAuth.logout();

Logout clears the session, fires onAuthStateChanged(null), and hides the floating button. The floating button also exposes a Đăng xuất action that calls this for you.

Reference

AuthResult

interface AuthResult {
success: boolean;
user?: AuthUser;
loginMetadata?: LoginMetadata; // account status, maintenance info
error?: { code: number; message: string };
}

AuthUser (common fields)

FieldTypeNotes
userIdstringNumeric user id from the backend
uuidstringStable account UUID
usernamestring?Login name
displayName / fullnamestring?Display name
email / phonestring?Masked contact info
emailVerified / phoneVerifiedbooleanVerification flags
isPlayNowbooleantrue for guest sessions
tokenstringSession token (managed by the SDK)

Common error codes

CodeMeaning
10011012Validation (invalid username, password, email, OTP…)
2001Not logged in
2002Session expired
2003Account merge required
3001/3002Network timeout / unavailable
3401Unauthorized

Next step

Floating Button — how top-up, account, and logout are handled.