sdk: loginWithCustom facade + regenerated models
This commit is contained in:
+41
-4
@@ -1,15 +1,15 @@
|
||||
/**
|
||||
* AuthService — device-based player authentication.
|
||||
* AuthService — player authentication.
|
||||
*
|
||||
* Handles login via device ID (the primary auth flow for game clients),
|
||||
* logout (token clearing), and auth state observation.
|
||||
* Handles login via device ID (the primary auth flow for game clients) or a
|
||||
* custom backend webhook, logout (token clearing), and auth state observation.
|
||||
*/
|
||||
|
||||
import type { RudderContext } from '../core/context.js';
|
||||
import type { RemoteConfigShape } from '../state/RemoteConfigState.js';
|
||||
import { getOrCreateDeviceId } from '../device/DeviceId.js';
|
||||
import { api } from '../generated/api.js';
|
||||
import type { LoginViaDeviceResponse } from '../generated/auth.js';
|
||||
import type { LoginViaCustomResponse, LoginViaDeviceResponse } from '../generated/auth.js';
|
||||
|
||||
export type AuthState = 'signed-in' | 'signed-out';
|
||||
|
||||
@@ -24,6 +24,17 @@ export interface LoginWithDeviceOptions {
|
||||
nickname?: string;
|
||||
}
|
||||
|
||||
export interface LoginWithCustomOptions {
|
||||
/** Arbitrary payload forwarded to the developer's custom auth webhook. */
|
||||
customData: Record<string, unknown>;
|
||||
/** Player's region code (default: "global"). */
|
||||
region?: string;
|
||||
/** Player's language code (default: "en"). */
|
||||
language?: string;
|
||||
/** Optional player nickname; omitted from the request when not set. */
|
||||
nickname?: string;
|
||||
}
|
||||
|
||||
export class AuthService<TConfig extends RemoteConfigShape = RemoteConfigShape> {
|
||||
private readonly listeners = new Set<AuthStateListener>();
|
||||
private state: AuthState;
|
||||
@@ -81,6 +92,32 @@ export class AuthService<TConfig extends RemoteConfigShape = RemoteConfigShape>
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticates via the project's custom authorization webhook and returns
|
||||
* access + refresh tokens.
|
||||
*
|
||||
* The payload is forwarded to the developer's backend; on success, tokens are
|
||||
* saved to the client's TokenStore.
|
||||
*/
|
||||
async loginWithCustom(options: LoginWithCustomOptions): Promise<LoginViaCustomResponse> {
|
||||
const { customData, region = 'global', language = 'en', nickname } = options;
|
||||
const response = await api.loginViaCustom(this.ctx, {
|
||||
key: this.ctx.options.projectKey,
|
||||
customData,
|
||||
region,
|
||||
language,
|
||||
nickname,
|
||||
});
|
||||
|
||||
this.ctx.options.tokenStore.saveTokens(
|
||||
response.accessToken ?? '',
|
||||
response.refreshToken ?? '',
|
||||
);
|
||||
await this.startRuntime();
|
||||
this.setState('signed-in');
|
||||
return response;
|
||||
}
|
||||
|
||||
/** Clears all stored tokens (logout). */
|
||||
logout(): void {
|
||||
this.ctx.options.tokenStore.clear();
|
||||
|
||||
Reference in New Issue
Block a user