添加登录功能
This commit is contained in:
commit
a3f61a29d0
120 files changed
+3974
No files matched your search
@@ -0,0 +1,43 @@
|
||||
import { assertNotCancelled, GuestAccountStore, GuestError, GuestLogin, GuestPayload, GuestSession, GuestSettings, GuestSigner, GuestTransport } from './GuestContracts';
|
||||
import { GUEST_REGISTRATION_PATH } from './GuestSignature';
|
||||
|
||||
/** 两步认证的应用服务;不依赖场景或平台全局对象。 / Two-step authentication service independent of scenes and platform globals. */
|
||||
export class GuestLoginService implements GuestLogin {
|
||||
private current: Promise<GuestSession> | null = null;
|
||||
constructor(private readonly settings: GuestSettings, private readonly accounts: GuestAccountStore,
|
||||
private readonly signer: GuestSigner, private readonly transport: GuestTransport, private readonly makeIdentity: () => string) {}
|
||||
signIn(signal?: AbortSignal): Promise<GuestSession> {
|
||||
// 合并请求由首次调用者拥有;已取消的新调用者不加入等待。 / The first caller owns a coalesced request; an already-cancelled caller never joins.
|
||||
if (signal?.aborted) return Promise.reject(new GuestError('cancelled', 'Sign-in cancelled.'));
|
||||
if (this.current) return this.current;
|
||||
const operation = this.authenticate(signal);
|
||||
this.current = operation;
|
||||
const release = () => { if (this.current === operation) this.current = null; };
|
||||
void operation.then(release, release);
|
||||
return operation;
|
||||
}
|
||||
private async authenticate(signal?: AbortSignal): Promise<GuestSession> {
|
||||
assertNotCancelled(signal);
|
||||
if (!this.settings.product.trim() || !['iphone', 'ipad'].includes(this.settings.deviceType) ||
|
||||
!['web', 'android-google', 'ios'].includes(this.settings.registerDevice)) {
|
||||
throw new GuestError('configuration', 'Guest service configuration is unavailable.');
|
||||
}
|
||||
const guestId = this.accounts.identity(this.makeIdentity);
|
||||
const body: GuestPayload = {
|
||||
guest_id: guestId, pkg: this.settings.product, device_type: this.settings.deviceType,
|
||||
register_device: this.settings.registerDevice, install_fb: false, gyroscope_not_spinning: false,
|
||||
};
|
||||
await this.transport.post(GUEST_REGISTRATION_PATH, body, this.signer.headers(body), signal);
|
||||
assertNotCancelled(signal);
|
||||
// 换 token 不携带签名或认证头;始终使用同一游客身份。 / Exchange without signature/auth headers using the same identity.
|
||||
const response = await this.transport.post('/account/login', { guest_id: guestId, pkg: this.settings.product }, {}, signal);
|
||||
assertNotCancelled(signal);
|
||||
const value = response.data;
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) throw new GuestError('response', 'Invalid sign-in response. Please retry.');
|
||||
const data = value as Record<string, unknown>;
|
||||
if (typeof data.token !== 'string' || !data.token.trim()) throw new GuestError('response', 'Invalid sign-in response. Please retry.');
|
||||
const session: GuestSession = { guestId, token: data.token, expires: typeof data.expires === 'number' && Number.isFinite(data.expires) ? data.expires : 0 };
|
||||
this.accounts.save(session);
|
||||
return session;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user