Skip to content

Commit 286652c

Browse files
authored
Add proof of phone number verification method in web latte #346
ref DEV-2553
2 parents 59766b8 + 7e1924c commit 286652c

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

packages/authgear-web/src/latte/latte.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import { LatteAuthenticateOptions } from "./types";
1111
class Latte {
1212
public authgear: WebContainer;
1313
public customUIEndpoint: string;
14-
public tokenizeEndpoint: string;
14+
public middlewareEndpoint: string;
15+
16+
private get tokenizeEndpoint(): string {
17+
return `${this.middlewareEndpoint}/token`;
18+
}
19+
private get proofOfPhoneNumberVerificationEndpoint(): string {
20+
return `${this.middlewareEndpoint}/proof_of_phone_number_verification
21+
`;
22+
}
1523

1624
private flows: LatteFlows;
1725

@@ -20,11 +28,11 @@ class Latte {
2028
constructor(
2129
authgear: WebContainer,
2230
customUIEndpoint: string,
23-
tokenizeEndpoint: string
31+
middlewareEndpoint: string
2432
) {
2533
this.authgear = authgear;
2634
this.customUIEndpoint = customUIEndpoint;
27-
this.tokenizeEndpoint = tokenizeEndpoint;
35+
this.middlewareEndpoint = middlewareEndpoint;
2836

2937
this.flows = new LatteFlows(this);
3038
}
@@ -62,6 +70,25 @@ class Latte {
6270
}
6371
return resp.text();
6472
}
73+
74+
public async getProofOfPhoneNumberVerification(): Promise<string> {
75+
await this.authgear.refreshAccessTokenIfNeeded();
76+
const accessToken = this.authgear.accessToken;
77+
if (accessToken == null) {
78+
throw new LatteError("getProofOfPhoneNumberVerification requires authenticated user");
79+
}
80+
81+
const resp = await this.fetch(this.proofOfPhoneNumberVerificationEndpoint, {
82+
method: "POST",
83+
body: JSON.stringify({ access_token: accessToken }),
84+
credentials: "omit",
85+
});
86+
if (!resp.ok) {
87+
throw new LatteError("unexpected response from proofOfPhoneNumberVerification endpoint", resp);
88+
}
89+
const json = await resp.json();
90+
return json["proof_of_phone_number_verification"];
91+
}
6592
}
6693

6794
export { Latte };

0 commit comments

Comments
 (0)