- Added username persistence via localStorage with read-only input behavior.

- Added automatic connect() invocation on page load.
- Implemented robust login flow: register or auto-login based on local flags.
- Added logout support with clean WebSocket disconnect + state reset.
- Updated NakamaProvider with getSession(), autoLogin(), registerWithUsername().
- Connected logout button and integrated updated login behavior into UI.
This commit is contained in:
2025-11-29 02:09:51 +05:30
parent f7929b10ef
commit 94bdec8cb4
2 changed files with 80 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ export interface NakamaContextType {
matchId: string | null;
loginOrRegister(username: string): Promise<void>;
logout(): Promise<void>;
joinMatchmaker(mode: string): Promise<string>;
joinMatch(matchId: string): Promise<void>;
@@ -55,17 +56,63 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) {
const lastModeRef = React.useRef<string | null>(null);
const socketRef = React.useRef<Socket | null>(null);
async function autoLogin() {
const deviceId = getOrCreateDeviceId();
try {
return await client.authenticateDevice(
deviceId,
false
);
} catch (e) {
// fallback: treat as new user
localStorage.removeItem("registered");
throw e;
}
}
async function registerWithUsername(username: string) {
const deviceId = getOrCreateDeviceId();
// create + set username
const session = await client.authenticateDevice(
deviceId,
true,
username
);
// mark an account as registered
localStorage.setItem("registered", "yes");
localStorage.setItem("username", username);
return session;
}
async function getSession(username?: string) {
const isRegistered = localStorage.getItem("registered") === "yes";
if (!username && !isRegistered) {
throw new Error("No username provided and not registered");
}
let newSession;
if (!isRegistered) {
newSession = await registerWithUsername(username ?? "");
} else {
newSession = await autoLogin();
}
return newSession;
}
// ----------------------------------------------------
// LOGIN
// ----------------------------------------------------
async function loginOrRegister(username: string) {
const deviceId = getOrCreateDeviceId();
async function loginOrRegister(username?: string) {
// authenticate user
const newSession = await client.authenticateDevice(deviceId, true, username);
const newSession = await getSession(username);
setSession(newSession);
// create socket (new Nakama 3.x signature)
// create a socket (new Nakama 3.x signature)
const s = client.createSocket(undefined, undefined); // no SSL on localhost
await s.connect(newSession, true);
setSocket(s);
@@ -102,6 +149,25 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) {
};
}
async function logout() {
try {
// 1) Disconnect socket if present
if (socketRef.current) {
socketRef.current.disconnect(true);
console.log("[Nakama] WebSocket disconnected");
}
} catch (err) {
console.warn("[Nakama] Error while disconnecting socket:", err);
}
// 2) Clear state
setSocket(null);
socketRef.current = null;
setSession(null);
console.log("[Nakama] Clean logout completed");
}
// ----------------------------------------------------
// MATCHMAKING
// ----------------------------------------------------
@@ -187,6 +253,7 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) {
socket,
matchId,
loginOrRegister,
logout,
joinMatchmaker,
joinMatch,
sendMatchData,