Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@authsignal/browser",
"version": "1.12.5",
"version": "1.12.6",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down
24 changes: 18 additions & 6 deletions src/api/push-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
import {buildHeaders} from "./helpers";
import {buildHeaders, handleTokenExpired} from "./helpers";
import {ApiClientOptions, ErrorResponse} from "./types/shared";
import {PushChallengeResponse, PushVerifyResponse} from "./types/push";

export class PushApiClient {
tenantId: string;
baseUrl: string;
onTokenExpired?: () => void;

constructor({baseUrl, tenantId}: ApiClientOptions) {
constructor({baseUrl, tenantId, onTokenExpired}: ApiClientOptions) {
this.tenantId = tenantId;
this.baseUrl = baseUrl;
this.onTokenExpired = onTokenExpired;
}

async challenge({action}: {action: string}): Promise<PushChallengeResponse | ErrorResponse> {
async challenge({action, token}: {action: string; token: string}): Promise<PushChallengeResponse | ErrorResponse> {
const body = {action};

const response = await fetch(`${this.baseUrl}/client/challenge/push`, {
method: "POST",
headers: buildHeaders({tenantId: this.tenantId}),
headers: buildHeaders({token, tenantId: this.tenantId}),
body: JSON.stringify(body),
});

const responseJson = await response.json();

handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired});

return responseJson;
}

async verify({challengeId}: {challengeId: string}): Promise<PushVerifyResponse | ErrorResponse> {
async verify({
challengeId,
token,
}: {
challengeId: string;
token: string;
}): Promise<PushVerifyResponse | ErrorResponse> {
const body = {challengeId};

const response = await fetch(`${this.baseUrl}/client/verify/push`, {
method: "POST",
headers: buildHeaders({tenantId: this.tenantId}),
headers: buildHeaders({token, tenantId: this.tenantId}),
body: JSON.stringify(body),
});

const responseJson = await response.json();

handleTokenExpired({response: responseJson, onTokenExpired: this.onTokenExpired});

return responseJson;
}
}
2 changes: 1 addition & 1 deletion src/authsignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Authsignal {
this.sms = new Sms({tenantId, baseUrl, onTokenExpired});
this.securityKey = new SecurityKey({tenantId, baseUrl, onTokenExpired});
this.qrCode = new QrCode({tenantId, baseUrl});
this.push = new Push({tenantId, baseUrl});
this.push = new Push({tenantId, baseUrl, onTokenExpired});
this.whatsapp = new Whatsapp({tenantId, baseUrl, onTokenExpired});
}

Expand Down
19 changes: 15 additions & 4 deletions src/push.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {PushApiClient} from "./api/push-api-client";
import {PushChallengeResponse, PushVerifyResponse} from "./api/types/push";
import {handleApiResponse} from "./helpers";
import {TokenCache} from "./token-cache";
import {AuthsignalResponse} from "./types";

type PushOptions = {
baseUrl: string;
tenantId: string;
onTokenExpired?: () => void;
};

type ChallengeParams = {
Expand All @@ -18,19 +20,28 @@ type VerifyParams = {

export class Push {
private api: PushApiClient;
private cache = TokenCache.shared;

constructor({baseUrl, tenantId}: PushOptions) {
this.api = new PushApiClient({baseUrl, tenantId});
constructor({baseUrl, tenantId, onTokenExpired}: PushOptions) {
this.api = new PushApiClient({baseUrl, tenantId, onTokenExpired});
}

async challenge({action}: ChallengeParams): Promise<AuthsignalResponse<PushChallengeResponse>> {
const response = await this.api.challenge({action});
if (!this.cache.token) {
return this.cache.handleTokenNotSetError();
}

const response = await this.api.challenge({action, token: this.cache.token});

return handleApiResponse(response);
}

async verify({challengeId}: VerifyParams): Promise<AuthsignalResponse<PushVerifyResponse>> {
const response = await this.api.verify({challengeId});
if (!this.cache.token) {
return this.cache.handleTokenNotSetError();
}

const response = await this.api.verify({challengeId, token: this.cache.token});

return handleApiResponse(response);
}
Expand Down
Loading