Skip to content

Commit 1310255

Browse files
authored
add email otp (#116)
* add email otp * 0.1.1
1 parent 7587e32 commit 1310255

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@utxos/sdk",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "UTXOS SDK - Web3 infrastructure platform for UTXO blockchains",
55
"main": "./dist/index.cjs",
66
"browser": "./dist/index.js",

src/non-custodial/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,59 @@ export class Web3NonCustodialProvider {
571571
};
572572
}
573573

574+
/**
575+
* Sends OTP to email address
576+
* @param email - The email address to send OTP to
577+
* @returns Promise that resolves when OTP is sent
578+
*/
579+
async sendEmailOtp(email: string): Promise<{ error: Error | null }> {
580+
const res = await fetch(this.appOrigin + "/api/auth/email/send-otp", {
581+
method: "POST",
582+
headers: { "Content-Type": "application/json" },
583+
body: JSON.stringify({ email, projectId: this.projectId }),
584+
});
585+
586+
if (!res.ok) {
587+
const data = await res.json();
588+
return { error: new Error(data.error) };
589+
}
590+
591+
return { error: null };
592+
}
593+
594+
/**
595+
* Verifies OTP and stores JWT (same pattern as OAuth callback)
596+
* @param email - The email address used to send OTP
597+
* @param code - The 6-digit OTP code
598+
* @returns User data on success, error on failure
599+
*/
600+
async verifyEmailOtp(
601+
email: string,
602+
code: string,
603+
): Promise<
604+
| { data: Web3NonCustodialProviderUser; error: null }
605+
| { data: null; error: Error }
606+
> {
607+
const res = await fetch(this.appOrigin + "/api/auth/email/verify-otp", {
608+
method: "POST",
609+
headers: { "Content-Type": "application/json" },
610+
body: JSON.stringify({ email, code, projectId: this.projectId }),
611+
});
612+
613+
if (!res.ok) {
614+
const data = await res.json();
615+
return { data: null, error: new Error(data.error) };
616+
}
617+
618+
const { token } = await res.json();
619+
620+
// Store JWT same way as OAuth flow
621+
await this.putInStorage<AuthJwtLocationObject>(AUTH_KEY, { jwt: token });
622+
623+
// Return user data same way as getUser()
624+
return this.getUser();
625+
}
626+
574627
private async putInStorage<ObjectType extends object>(
575628
key: string,
576629
data: ObjectType,

src/types/core/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ export type Web3ProjectBranding = {
2525
discordEnabled?: boolean;
2626
googleEnabled?: boolean;
2727
appleEnabled?: boolean;
28+
emailEnabled?: boolean;
29+
};
30+
31+
export type Web3ProjectWallet = {
32+
id: string;
33+
projectId: string;
34+
key: string;
35+
pubKeyHash: string;
36+
stakeCredentialHash: string;
37+
tags: string[];
2838
};
2939

3040
export type Web3JWTBody = {
@@ -48,7 +58,7 @@ export type Web3JWTBody = {
4858
username: string | null;
4959
};
5060

51-
export type Web3AuthProvider = "google" | "discord" | "twitter" | "apple";
61+
export type Web3AuthProvider = "google" | "discord" | "twitter" | "apple" | "email";
5262

5363
export type SponsorshipTxParserPostRequestBody = {
5464
txHex: string;

0 commit comments

Comments
 (0)