@crossmint/client-sdk-auth 1.1.26 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -36,6 +36,72 @@ const user = await crossmintAuth.getUser();
36
36
  crossmintAuth.logout();
37
37
  ```
38
38
 
39
+ ## Custom Storage Providers
40
+
41
+ By default, the SDK uses browser cookies for storing authentication materials. For environments where cookies are not available, such as React Native, you can provide a custom storage provider.
42
+
43
+ ### React Native Storage Options
44
+
45
+ The SDK provides several secure storage implementations for React Native:
46
+
47
+ #### 1. Expo SecureStore (Recommended for Expo apps)
48
+
49
+ **First install dependencies:**
50
+
51
+ ```bash
52
+ npx expo install expo-secure-store
53
+ ```
54
+
55
+ For Expo applications, use SecureStore which provides a secure encrypted storage solution:
56
+
57
+ ```typescript
58
+ import * as SecureStore from 'expo-secure-store';
59
+ import { ExpoSecureStorage, CrossmintAuth } from "@crossmint/client-sdk-auth";
60
+
61
+ // Create a custom storage provider using Expo's SecureStore
62
+ const storageProvider = new ExpoSecureStorage(SecureStore);
63
+
64
+ // Initialize auth client with secure storage
65
+ const crossmintAuth = CrossmintAuth.from(crossmint, {
66
+ storageProvider
67
+ });
68
+ ```
69
+
70
+ #### 2. React Native Encrypted Storage (Recommended for non-Expo apps)
71
+
72
+ **First install dependencies:**
73
+
74
+ ```bash
75
+ npm install react-native-encrypted-storage
76
+ ```
77
+
78
+ For vanilla React Native apps, use EncryptedStorage:
79
+
80
+ ```typescript
81
+ import EncryptedStorage from 'react-native-encrypted-storage';
82
+ import { RNEncryptedStorage, CrossmintAuth } from "@crossmint/client-sdk-auth";
83
+
84
+ // Create a custom storage provider using react-native-encrypted-storage
85
+ const storageProvider = new RNEncryptedStorage(EncryptedStorage);
86
+
87
+ // Initialize auth client with secure storage
88
+ const crossmintAuth = CrossmintAuth.from(crossmint, {
89
+ storageProvider
90
+ });
91
+ ```
92
+
93
+ ### Implementing Custom Storage
94
+
95
+ You can implement any storage solution by implementing the `StorageProvider` interface:
96
+
97
+ ```typescript
98
+ export interface StorageProvider {
99
+ get(key: string): string | undefined | Promise<string | undefined>;
100
+ set(key: string, value: string, expiresAt?: string): void | Promise<void>;
101
+ remove(key: string): void | Promise<void>;
102
+ }
103
+ ```
104
+
39
105
  ## Secure setup using HttpOnly cookies
40
106
 
41
107
  To secure the authentication material, you can set up a custom endpoint in your backend that will handle refreshing the authentication material and storing it in HttpOnly cookies. This way, the authentication material is not accessible to JavaScript running in the browser.
@@ -96,4 +162,4 @@ const crossmintAuth = CrossmintAuth.from(crossmint, {
96
162
  }
97
163
  ```
98
164
 
99
- These callbacks allow you to perform custom actions when tokens are refreshed or when the user logs out.
165
+ These callbacks allow you to perform custom actions when tokens are refreshed or when the user logs out.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkJHM7U2U5cjs = require('./chunk-JHM7U2U5.cjs');require('./chunk-XHYLTO6W.cjs');require('./chunk-NZ2DAY24.cjs');require('./chunk-NYYORERK.cjs');require('./chunk-VDJHVTKI.cjs');require('./chunk-CK4JCQY6.cjs');exports.CrossmintAuthClient = _chunkJHM7U2U5cjs.a;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkE6MPVBTFcjs = require('./chunk-E6MPVBTF.cjs');require('./chunk-3JC3QYTV.cjs');require('./chunk-NZ2DAY24.cjs');require('./chunk-VDJHVTKI.cjs');require('./chunk-TEI6NKFC.cjs');require('./chunk-NYYORERK.cjs');require('./chunk-CK4JCQY6.cjs');exports.CrossmintAuthClient = _chunkE6MPVBTFcjs.a;
@@ -1,20 +1,23 @@
1
1
  import { UseSignInData } from '@farcaster/auth-kit';
2
2
  import { CrossmintAuthOptions, CrossmintAuth, AuthMaterialWithUser, OAuthProvider } from '@crossmint/common-sdk-auth';
3
3
  import { Crossmint, CrossmintApiClient } from '@crossmint/common-sdk-base';
4
+ import { StorageProvider } from './utils/storage.cjs';
4
5
 
5
6
  type CrossmintAuthClientConfig = CrossmintAuthOptions & {
6
7
  callbacks?: CrossmintAuthClientCallbacks;
7
8
  logoutRoute?: string;
9
+ storageProvider?: StorageProvider;
8
10
  };
9
11
  declare class CrossmintAuthClient extends CrossmintAuth {
10
12
  private callbacks;
11
13
  private refreshTask;
12
14
  private refreshPromise;
13
15
  private logoutRoute;
16
+ private storageProvider;
14
17
  protected constructor(crossmint: Crossmint, apiClient: CrossmintApiClient, config?: CrossmintAuthClientConfig);
15
18
  static from(crossmint: Crossmint, config?: CrossmintAuthClientConfig): CrossmintAuthClient;
16
19
  getUser(): Promise<any>;
17
- storeAuthMaterial(authMaterial: AuthMaterialWithUser): void;
20
+ storeAuthMaterial(authMaterial: AuthMaterialWithUser): Promise<void>;
18
21
  logout(): Promise<void>;
19
22
  handleRefreshAuthMaterial(refreshTokenSecret?: string): Promise<void>;
20
23
  getOAuthUrl(provider: OAuthProvider): Promise<any>;
@@ -1,20 +1,23 @@
1
1
  import { UseSignInData } from '@farcaster/auth-kit';
2
2
  import { CrossmintAuthOptions, CrossmintAuth, AuthMaterialWithUser, OAuthProvider } from '@crossmint/common-sdk-auth';
3
3
  import { Crossmint, CrossmintApiClient } from '@crossmint/common-sdk-base';
4
+ import { StorageProvider } from './utils/storage.js';
4
5
 
5
6
  type CrossmintAuthClientConfig = CrossmintAuthOptions & {
6
7
  callbacks?: CrossmintAuthClientCallbacks;
7
8
  logoutRoute?: string;
9
+ storageProvider?: StorageProvider;
8
10
  };
9
11
  declare class CrossmintAuthClient extends CrossmintAuth {
10
12
  private callbacks;
11
13
  private refreshTask;
12
14
  private refreshPromise;
13
15
  private logoutRoute;
16
+ private storageProvider;
14
17
  protected constructor(crossmint: Crossmint, apiClient: CrossmintApiClient, config?: CrossmintAuthClientConfig);
15
18
  static from(crossmint: Crossmint, config?: CrossmintAuthClientConfig): CrossmintAuthClient;
16
19
  getUser(): Promise<any>;
17
- storeAuthMaterial(authMaterial: AuthMaterialWithUser): void;
20
+ storeAuthMaterial(authMaterial: AuthMaterialWithUser): Promise<void>;
18
21
  logout(): Promise<void>;
19
22
  handleRefreshAuthMaterial(refreshTokenSecret?: string): Promise<void>;
20
23
  getOAuthUrl(provider: OAuthProvider): Promise<any>;
@@ -1 +1 @@
1
- import{a}from"./chunk-LU7DVOA3.js";import"./chunk-U76ID4TS.js";import"./chunk-REIMEXVF.js";import"./chunk-HU56N5SW.js";import"./chunk-NIH25R3L.js";import"./chunk-22GIA4MK.js";export{a as CrossmintAuthClient};
1
+ import{a}from"./chunk-XZG4AO5B.js";import"./chunk-BGZBK7AT.js";import"./chunk-REIMEXVF.js";import"./chunk-NIH25R3L.js";import"./chunk-M64WCXMK.js";import"./chunk-HU56N5SW.js";import"./chunk-22GIA4MK.js";export{a as CrossmintAuthClient};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkNZ2DAY24cjs = require('./chunk-NZ2DAY24.cjs');var _chunkVDJHVTKIcjs = require('./chunk-VDJHVTKI.cjs');var _chunkTEI6NKFCcjs = require('./chunk-TEI6NKFC.cjs');var _chunkCK4JCQY6cjs = require('./chunk-CK4JCQY6.cjs');var _commonsdkauth = require('@crossmint/common-sdk-auth');var _clientsdkbase = require('@crossmint/client-sdk-base');var f=class C extends _commonsdkauth.CrossmintAuth{constructor(e,r,t={}){var a,s,i;super(e,r,t);this.refreshTask=null;this.refreshPromise=null;this.callbacks=(a=t.callbacks)!=null?a:{},this.logoutRoute=(s=t.logoutRoute)!=null?s:null,this.storageProvider=(i=t.storageProvider)!=null?i:_chunkTEI6NKFCcjs.b.call(void 0, )}static from(e,r={}){let t=new C(e,_commonsdkauth.CrossmintAuth.defaultApiClient(e),r);return typeof window!="undefined"&&t.handleRefreshAuthMaterial(),t}getUser(){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var e;try{let r=yield this.apiClient.get(`api/${_commonsdkauth.CROSSMINT_API_VERSION}/sdk/auth/user`,{headers:{"Content-Type":"application/json"}});if(!r.ok)throw new Error((e=JSON.parse(yield r.text()))==null?void 0:e.message);return yield r.json()}catch(r){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to fetch user: ${r instanceof Error?r.message:"Unknown error"}`)}})}storeAuthMaterial(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){yield Promise.all([this.storageProvider.set(_commonsdkauth.SESSION_PREFIX,e.jwt),this.storageProvider.set(_commonsdkauth.REFRESH_TOKEN_PREFIX,e.refreshToken.secret,e.refreshToken.expiresAt)])})}logout(){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r,t;let e=yield this.storageProvider.get(_commonsdkauth.REFRESH_TOKEN_PREFIX);yield Promise.all([this.storageProvider.remove(_commonsdkauth.REFRESH_TOKEN_PREFIX),this.storageProvider.remove(_commonsdkauth.SESSION_PREFIX)]),(t=(r=this.callbacks).onLogout)==null||t.call(r);try{this.logoutRoute!=null?yield this.logoutFromCustomRoute():e!=null&&(yield this.logoutFromDefaultRoute(e))}catch(a){console.error(a)}})}handleRefreshAuthMaterial(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r,t;try{let a=e!=null?e:yield this.storageProvider.get(_commonsdkauth.REFRESH_TOKEN_PREFIX);if(a==null&&this.refreshRoute==null)return;this.refreshPromise==null&&(this.refreshPromise=this.refreshAuthMaterial(a));let s=yield this.refreshPromise;this.refreshRoute==null&&(yield this.storeAuthMaterial(s)),(t=(r=this.callbacks).onTokenRefresh)==null||t.call(r,s),this.scheduleNextRefresh(s.jwt)}catch(a){console.error(a),yield this.logout()}finally{this.refreshPromise=null}})}getOAuthUrl(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r;try{let t=yield this.apiClient.get(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/social/${e}/start`,{headers:{"Content-Type":"application/json"}});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return(yield t.json()).oauthUrl}catch(t){if(console.error(`Failed to get OAuth URL for provider ${e}: ${t instanceof Error?t.message:"Unknown error"}`),t instanceof Error&&t.message.includes("Request from origin")){let a=t.message.match(/origin "([^"]+)"/),s=a==null?void 0:a[1];if(s)throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Unauthorized origin: ${s}. Please add this origin to your API key's authorized origins in the Crossmint Console.`)}throw new (0, _commonsdkauth.CrossmintAuthenticationError)("Unable to load oauth providers. Please try again later.")}})}sendEmailOtp(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r;try{let t=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/otps/send`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return yield t.json()}catch(t){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to send email OTP: ${t instanceof Error?t.message:"Unknown error"}`)}})}confirmEmailOtp(e,r,t){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var a;try{let s=new URLSearchParams({email:e,signinAuthenticationMethod:"email",token:t,locale:"en",state:r,callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}),i=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/authenticate?${s}`,{headers:{"Content-Type":"application/json"}});if(!i.ok)throw new Error((a=JSON.parse(yield i.text()))==null?void 0:a.message);return(yield i.json()).oneTimeSecret}catch(s){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to confirm email OTP: ${s instanceof Error?s.message:"Unknown error"}`)}})}signInWithFarcaster(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r;try{let t=new URLSearchParams({signinAuthenticationMethod:"farcaster",callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}),a=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/authenticate?${t}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify(_chunkCK4JCQY6cjs.b.call(void 0, _chunkCK4JCQY6cjs.a.call(void 0, {},e),{domain:e.signatureParams.domain,redirect:!0,callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}))});if(!a.ok)throw new Error((r=JSON.parse(yield a.text()))==null?void 0:r.message);return(yield a.json()).oneTimeSecret}catch(t){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to sign in with Farcaster: ${t instanceof Error?t.message:"Unknown error"}`)}})}signInWithSmartWallet(e,r){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var t;try{let a=r==="evm"?"ethereum":"solana",s=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/crypto_wallets/authenticate/start`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,walletType:a})});if(!s.ok)throw new Error((t=JSON.parse(yield s.text()))==null?void 0:t.message);return yield s.json()}catch(a){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to initiate smart wallet sign in: ${a instanceof Error?a.message:"Unknown error"}`)}})}authenticateSmartWallet(e,r,t){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var a;try{let s=new URLSearchParams({signinAuthenticationMethod:r,callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}),i=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/crypto_wallets/authenticate?${s}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,signature:t})});if(!i.ok)throw new Error((a=JSON.parse(yield i.text()))==null?void 0:a.message);return yield i.json()}catch(s){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to authenticate smart wallet: ${s instanceof Error?s.message:"Unknown error"}`)}})}logoutFromCustomRoute(){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){if(!this.logoutRoute)throw new Error("Custom logout route is not set");return yield fetch(this.logoutRoute,{method:"POST"})})}scheduleNextRefresh(e){let r=_chunkVDJHVTKIcjs.a.call(void 0, e);if(!r)throw new Error("Invalid JWT");let t=Date.now()/1e3,a=r-t-120;if(a>0){let s=Date.now()+a*1e3;this.cancelScheduledRefresh(),this.refreshTask=_clientsdkbase.queueTask.call(void 0, ()=>this.handleRefreshAuthMaterial(),s)}}cancelScheduledRefresh(){this.refreshTask&&(this.refreshTask.cancel(),this.refreshTask=null)}};exports.a = f;
@@ -0,0 +1 @@
1
+ import{a as i,b as n,c as o}from"./chunk-HU56N5SW.js";import{c as r}from"./chunk-22GIA4MK.js";var t=class{get(e){return r(this,null,function*(){if(typeof document!="undefined")return yield i(e)})}set(e,s,d){return r(this,null,function*(){if(typeof document!="undefined")return yield n(e,s,d)})}remove(e){return r(this,null,function*(){if(typeof document!="undefined")return yield o(e)})}};function m(){return new t}export{t as a,m as b};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkNYYORERKcjs = require('./chunk-NYYORERK.cjs');var _chunkCK4JCQY6cjs = require('./chunk-CK4JCQY6.cjs');var t=class{get(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){if(typeof document!="undefined")return yield _chunkNYYORERKcjs.a.call(void 0, e)})}set(e,s,d){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){if(typeof document!="undefined")return yield _chunkNYYORERKcjs.b.call(void 0, e,s,d)})}remove(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){if(typeof document!="undefined")return yield _chunkNYYORERKcjs.c.call(void 0, e)})}};function f(){return new t}exports.a = t; exports.b = f;
@@ -0,0 +1 @@
1
+ import{a as m}from"./chunk-REIMEXVF.js";import{a as p}from"./chunk-NIH25R3L.js";import{b as w}from"./chunk-M64WCXMK.js";import{a as c,b as u,c as o}from"./chunk-22GIA4MK.js";import{AUTH_SDK_ROOT_ENDPOINT as n,CROSSMINT_API_VERSION as y,CrossmintAuth as g,CrossmintAuthenticationError as l,REFRESH_TOKEN_PREFIX as h,SESSION_PREFIX as d}from"@crossmint/common-sdk-auth";import{queueTask as k}from"@crossmint/client-sdk-base";var f=class C extends g{constructor(e,r,t={}){var a,s,i;super(e,r,t);this.refreshTask=null;this.refreshPromise=null;this.callbacks=(a=t.callbacks)!=null?a:{},this.logoutRoute=(s=t.logoutRoute)!=null?s:null,this.storageProvider=(i=t.storageProvider)!=null?i:w()}static from(e,r={}){let t=new C(e,g.defaultApiClient(e),r);return typeof window!="undefined"&&t.handleRefreshAuthMaterial(),t}getUser(){return o(this,null,function*(){var e;try{let r=yield this.apiClient.get(`api/${y}/sdk/auth/user`,{headers:{"Content-Type":"application/json"}});if(!r.ok)throw new Error((e=JSON.parse(yield r.text()))==null?void 0:e.message);return yield r.json()}catch(r){throw new l(`Failed to fetch user: ${r instanceof Error?r.message:"Unknown error"}`)}})}storeAuthMaterial(e){return o(this,null,function*(){yield Promise.all([this.storageProvider.set(d,e.jwt),this.storageProvider.set(h,e.refreshToken.secret,e.refreshToken.expiresAt)])})}logout(){return o(this,null,function*(){var r,t;let e=yield this.storageProvider.get(h);yield Promise.all([this.storageProvider.remove(h),this.storageProvider.remove(d)]),(t=(r=this.callbacks).onLogout)==null||t.call(r);try{this.logoutRoute!=null?yield this.logoutFromCustomRoute():e!=null&&(yield this.logoutFromDefaultRoute(e))}catch(a){console.error(a)}})}handleRefreshAuthMaterial(e){return o(this,null,function*(){var r,t;try{let a=e!=null?e:yield this.storageProvider.get(h);if(a==null&&this.refreshRoute==null)return;this.refreshPromise==null&&(this.refreshPromise=this.refreshAuthMaterial(a));let s=yield this.refreshPromise;this.refreshRoute==null&&(yield this.storeAuthMaterial(s)),(t=(r=this.callbacks).onTokenRefresh)==null||t.call(r,s),this.scheduleNextRefresh(s.jwt)}catch(a){console.error(a),yield this.logout()}finally{this.refreshPromise=null}})}getOAuthUrl(e){return o(this,null,function*(){var r;try{let t=yield this.apiClient.get(`${n}/social/${e}/start`,{headers:{"Content-Type":"application/json"}});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return(yield t.json()).oauthUrl}catch(t){if(console.error(`Failed to get OAuth URL for provider ${e}: ${t instanceof Error?t.message:"Unknown error"}`),t instanceof Error&&t.message.includes("Request from origin")){let a=t.message.match(/origin "([^"]+)"/),s=a==null?void 0:a[1];if(s)throw new l(`Unauthorized origin: ${s}. Please add this origin to your API key's authorized origins in the Crossmint Console.`)}throw new l("Unable to load oauth providers. Please try again later.")}})}sendEmailOtp(e){return o(this,null,function*(){var r;try{let t=yield this.apiClient.post(`${n}/otps/send`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return yield t.json()}catch(t){throw new l(`Failed to send email OTP: ${t instanceof Error?t.message:"Unknown error"}`)}})}confirmEmailOtp(e,r,t){return o(this,null,function*(){var a;try{let s=new URLSearchParams({email:e,signinAuthenticationMethod:"email",token:t,locale:"en",state:r,callbackUrl:`${this.apiClient.baseUrl}/${n}/callback`}),i=yield this.apiClient.post(`${n}/authenticate?${s}`,{headers:{"Content-Type":"application/json"}});if(!i.ok)throw new Error((a=JSON.parse(yield i.text()))==null?void 0:a.message);return(yield i.json()).oneTimeSecret}catch(s){throw new l(`Failed to confirm email OTP: ${s instanceof Error?s.message:"Unknown error"}`)}})}signInWithFarcaster(e){return o(this,null,function*(){var r;try{let t=new URLSearchParams({signinAuthenticationMethod:"farcaster",callbackUrl:`${this.apiClient.baseUrl}/${n}/callback`}),a=yield this.apiClient.post(`${n}/authenticate?${t}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify(u(c({},e),{domain:e.signatureParams.domain,redirect:!0,callbackUrl:`${this.apiClient.baseUrl}/${n}/callback`}))});if(!a.ok)throw new Error((r=JSON.parse(yield a.text()))==null?void 0:r.message);return(yield a.json()).oneTimeSecret}catch(t){throw new l(`Failed to sign in with Farcaster: ${t instanceof Error?t.message:"Unknown error"}`)}})}signInWithSmartWallet(e,r){return o(this,null,function*(){var t;try{let a=r==="evm"?"ethereum":"solana",s=yield this.apiClient.post(`${n}/crypto_wallets/authenticate/start`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,walletType:a})});if(!s.ok)throw new Error((t=JSON.parse(yield s.text()))==null?void 0:t.message);return yield s.json()}catch(a){throw new l(`Failed to initiate smart wallet sign in: ${a instanceof Error?a.message:"Unknown error"}`)}})}authenticateSmartWallet(e,r,t){return o(this,null,function*(){var a;try{let s=new URLSearchParams({signinAuthenticationMethod:r,callbackUrl:`${this.apiClient.baseUrl}/${n}/callback`}),i=yield this.apiClient.post(`${n}/crypto_wallets/authenticate?${s}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,signature:t})});if(!i.ok)throw new Error((a=JSON.parse(yield i.text()))==null?void 0:a.message);return yield i.json()}catch(s){throw new l(`Failed to authenticate smart wallet: ${s instanceof Error?s.message:"Unknown error"}`)}})}logoutFromCustomRoute(){return o(this,null,function*(){if(!this.logoutRoute)throw new Error("Custom logout route is not set");return yield fetch(this.logoutRoute,{method:"POST"})})}scheduleNextRefresh(e){let r=p(e);if(!r)throw new Error("Invalid JWT");let t=Date.now()/1e3,a=r-t-120;if(a>0){let s=Date.now()+a*1e3;this.cancelScheduledRefresh(),this.refreshTask=k(()=>this.handleRefreshAuthMaterial(),s)}}cancelScheduledRefresh(){this.refreshTask&&(this.refreshTask.cancel(),this.refreshTask=null)}};export{f as a};
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkJHM7U2U5cjs = require('./chunk-JHM7U2U5.cjs');require('./chunk-XHYLTO6W.cjs');var _chunkNZ2DAY24cjs = require('./chunk-NZ2DAY24.cjs');var _chunkNYYORERKcjs = require('./chunk-NYYORERK.cjs');var _chunkVDJHVTKIcjs = require('./chunk-VDJHVTKI.cjs');require('./chunk-CK4JCQY6.cjs');var _commonsdkbase = require('@crossmint/common-sdk-base');exports.CrossmintAuth = _chunkJHM7U2U5cjs.a; exports.TIME_BEFORE_EXPIRING_JWT_IN_SECONDS = _chunkNZ2DAY24cjs.a; exports.createCrossmint = _commonsdkbase.createCrossmint; exports.deleteCookie = _chunkNYYORERKcjs.c; exports.getCookie = _chunkNYYORERKcjs.a; exports.getJWTExpiration = _chunkVDJHVTKIcjs.a; exports.setCookie = _chunkNYYORERKcjs.b;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkE6MPVBTFcjs = require('./chunk-E6MPVBTF.cjs');require('./chunk-3JC3QYTV.cjs');var _chunkNZ2DAY24cjs = require('./chunk-NZ2DAY24.cjs');var _chunkVDJHVTKIcjs = require('./chunk-VDJHVTKI.cjs');var _chunkTEI6NKFCcjs = require('./chunk-TEI6NKFC.cjs');var _chunkNYYORERKcjs = require('./chunk-NYYORERK.cjs');require('./chunk-CK4JCQY6.cjs');var _commonsdkbase = require('@crossmint/common-sdk-base');exports.CookieStorage = _chunkTEI6NKFCcjs.a; exports.CrossmintAuth = _chunkE6MPVBTFcjs.a; exports.TIME_BEFORE_EXPIRING_JWT_IN_SECONDS = _chunkNZ2DAY24cjs.a; exports.createCrossmint = _commonsdkbase.createCrossmint; exports.deleteCookie = _chunkNYYORERKcjs.c; exports.getCookie = _chunkNYYORERKcjs.a; exports.getDefaultStorageProvider = _chunkTEI6NKFCcjs.b; exports.getJWTExpiration = _chunkVDJHVTKIcjs.a; exports.setCookie = _chunkNYYORERKcjs.b;
package/dist/index.d.cts CHANGED
@@ -3,5 +3,6 @@ export { CrossmintAuthClient as CrossmintAuth } from './CrossmintAuthClient.cjs'
3
3
  export { getJWTExpiration } from './utils/jwt.cjs';
4
4
  export { deleteCookie, getCookie, setCookie } from './utils/cookies.cjs';
5
5
  export { TIME_BEFORE_EXPIRING_JWT_IN_SECONDS } from './utils/constants.cjs';
6
+ export { CookieStorage, StorageProvider, getDefaultStorageProvider } from './utils/storage.cjs';
6
7
  import '@farcaster/auth-kit';
7
8
  import '@crossmint/common-sdk-auth';
package/dist/index.d.ts CHANGED
@@ -3,5 +3,6 @@ export { CrossmintAuthClient as CrossmintAuth } from './CrossmintAuthClient.js';
3
3
  export { getJWTExpiration } from './utils/jwt.js';
4
4
  export { deleteCookie, getCookie, setCookie } from './utils/cookies.js';
5
5
  export { TIME_BEFORE_EXPIRING_JWT_IN_SECONDS } from './utils/constants.js';
6
+ export { CookieStorage, StorageProvider, getDefaultStorageProvider } from './utils/storage.js';
6
7
  import '@farcaster/auth-kit';
7
8
  import '@crossmint/common-sdk-auth';
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{a as r}from"./chunk-LU7DVOA3.js";import"./chunk-U76ID4TS.js";import{a as m}from"./chunk-REIMEXVF.js";import{a as o,b as s,c as e}from"./chunk-HU56N5SW.js";import{a as t}from"./chunk-NIH25R3L.js";import"./chunk-22GIA4MK.js";import{createCrossmint as x}from"@crossmint/common-sdk-base";export{r as CrossmintAuth,m as TIME_BEFORE_EXPIRING_JWT_IN_SECONDS,x as createCrossmint,e as deleteCookie,o as getCookie,t as getJWTExpiration,s as setCookie};
1
+ import{a as r}from"./chunk-XZG4AO5B.js";import"./chunk-BGZBK7AT.js";import{a as m}from"./chunk-REIMEXVF.js";import{a as t}from"./chunk-NIH25R3L.js";import{a as i,b as n}from"./chunk-M64WCXMK.js";import{a as o,b as s,c as e}from"./chunk-HU56N5SW.js";import"./chunk-22GIA4MK.js";import{createCrossmint as h}from"@crossmint/common-sdk-base";export{i as CookieStorage,r as CrossmintAuth,m as TIME_BEFORE_EXPIRING_JWT_IN_SECONDS,h as createCrossmint,e as deleteCookie,o as getCookie,n as getDefaultStorageProvider,t as getJWTExpiration,s as setCookie};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('../chunk-XHYLTO6W.cjs');var _chunkNZ2DAY24cjs = require('../chunk-NZ2DAY24.cjs');var _chunkNYYORERKcjs = require('../chunk-NYYORERK.cjs');var _chunkVDJHVTKIcjs = require('../chunk-VDJHVTKI.cjs');require('../chunk-CK4JCQY6.cjs');exports.TIME_BEFORE_EXPIRING_JWT_IN_SECONDS = _chunkNZ2DAY24cjs.a; exports.deleteCookie = _chunkNYYORERKcjs.c; exports.getCookie = _chunkNYYORERKcjs.a; exports.getJWTExpiration = _chunkVDJHVTKIcjs.a; exports.setCookie = _chunkNYYORERKcjs.b;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('../chunk-3JC3QYTV.cjs');var _chunkNZ2DAY24cjs = require('../chunk-NZ2DAY24.cjs');var _chunkVDJHVTKIcjs = require('../chunk-VDJHVTKI.cjs');var _chunkTEI6NKFCcjs = require('../chunk-TEI6NKFC.cjs');var _chunkNYYORERKcjs = require('../chunk-NYYORERK.cjs');require('../chunk-CK4JCQY6.cjs');exports.CookieStorage = _chunkTEI6NKFCcjs.a; exports.TIME_BEFORE_EXPIRING_JWT_IN_SECONDS = _chunkNZ2DAY24cjs.a; exports.deleteCookie = _chunkNYYORERKcjs.c; exports.getCookie = _chunkNYYORERKcjs.a; exports.getDefaultStorageProvider = _chunkTEI6NKFCcjs.b; exports.getJWTExpiration = _chunkVDJHVTKIcjs.a; exports.setCookie = _chunkNYYORERKcjs.b;
@@ -1,3 +1,4 @@
1
1
  export { getJWTExpiration } from './jwt.cjs';
2
2
  export { deleteCookie, getCookie, setCookie } from './cookies.cjs';
3
3
  export { TIME_BEFORE_EXPIRING_JWT_IN_SECONDS } from './constants.cjs';
4
+ export { CookieStorage, StorageProvider, getDefaultStorageProvider } from './storage.cjs';
@@ -1,3 +1,4 @@
1
1
  export { getJWTExpiration } from './jwt.js';
2
2
  export { deleteCookie, getCookie, setCookie } from './cookies.js';
3
3
  export { TIME_BEFORE_EXPIRING_JWT_IN_SECONDS } from './constants.js';
4
+ export { CookieStorage, StorageProvider, getDefaultStorageProvider } from './storage.js';
@@ -1 +1 @@
1
- import"../chunk-U76ID4TS.js";import{a as e}from"../chunk-REIMEXVF.js";import{a as b,b as c,c as d}from"../chunk-HU56N5SW.js";import{a}from"../chunk-NIH25R3L.js";import"../chunk-22GIA4MK.js";export{e as TIME_BEFORE_EXPIRING_JWT_IN_SECONDS,d as deleteCookie,b as getCookie,a as getJWTExpiration,c as setCookie};
1
+ import"../chunk-BGZBK7AT.js";import{a as e}from"../chunk-REIMEXVF.js";import{a}from"../chunk-NIH25R3L.js";import{a as f,b as g}from"../chunk-M64WCXMK.js";import{a as b,b as c,c as d}from"../chunk-HU56N5SW.js";import"../chunk-22GIA4MK.js";export{f as CookieStorage,e as TIME_BEFORE_EXPIRING_JWT_IN_SECONDS,d as deleteCookie,b as getCookie,g as getDefaultStorageProvider,a as getJWTExpiration,c as setCookie};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkTEI6NKFCcjs = require('../chunk-TEI6NKFC.cjs');require('../chunk-NYYORERK.cjs');require('../chunk-CK4JCQY6.cjs');exports.CookieStorage = _chunkTEI6NKFCcjs.a; exports.getDefaultStorageProvider = _chunkTEI6NKFCcjs.b;
@@ -0,0 +1,13 @@
1
+ interface StorageProvider {
2
+ get(key: string): Promise<string | undefined>;
3
+ set(key: string, value: string, expiresAt?: string): Promise<void>;
4
+ remove(key: string): Promise<void>;
5
+ }
6
+ declare class CookieStorage implements StorageProvider {
7
+ get(key: string): Promise<string | undefined>;
8
+ set(key: string, value: string, expiresAt?: string): Promise<void>;
9
+ remove(key: string): Promise<void>;
10
+ }
11
+ declare function getDefaultStorageProvider(): StorageProvider;
12
+
13
+ export { CookieStorage, type StorageProvider, getDefaultStorageProvider };
@@ -0,0 +1,13 @@
1
+ interface StorageProvider {
2
+ get(key: string): Promise<string | undefined>;
3
+ set(key: string, value: string, expiresAt?: string): Promise<void>;
4
+ remove(key: string): Promise<void>;
5
+ }
6
+ declare class CookieStorage implements StorageProvider {
7
+ get(key: string): Promise<string | undefined>;
8
+ set(key: string, value: string, expiresAt?: string): Promise<void>;
9
+ remove(key: string): Promise<void>;
10
+ }
11
+ declare function getDefaultStorageProvider(): StorageProvider;
12
+
13
+ export { CookieStorage, type StorageProvider, getDefaultStorageProvider };
@@ -0,0 +1 @@
1
+ import{a,b}from"../chunk-M64WCXMK.js";import"../chunk-HU56N5SW.js";import"../chunk-22GIA4MK.js";export{a as CookieStorage,b as getDefaultStorageProvider};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossmint/client-sdk-auth",
3
- "version": "1.1.26",
3
+ "version": "1.2.0",
4
4
  "repository": "https://github.com/Crossmint/crossmint-sdk",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Paella Labs Inc",
@@ -20,9 +20,9 @@
20
20
  "dependencies": {
21
21
  "jwt-decode": "4.0.0",
22
22
  "@farcaster/auth-kit": "0.6.0",
23
- "@crossmint/client-sdk-base": "1.4.16",
24
- "@crossmint/common-sdk-auth": "1.0.23",
25
- "@crossmint/common-sdk-base": "0.3.4"
23
+ "@crossmint/client-sdk-base": "1.4.17",
24
+ "@crossmint/common-sdk-auth": "1.0.24",
25
+ "@crossmint/common-sdk-base": "0.4.0"
26
26
  },
27
27
  "scripts": {
28
28
  "build": "tsup",
@@ -1 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});var _chunkNZ2DAY24cjs = require('./chunk-NZ2DAY24.cjs');var _chunkNYYORERKcjs = require('./chunk-NYYORERK.cjs');var _chunkVDJHVTKIcjs = require('./chunk-VDJHVTKI.cjs');var _chunkCK4JCQY6cjs = require('./chunk-CK4JCQY6.cjs');var _commonsdkauth = require('@crossmint/common-sdk-auth');var _clientsdkbase = require('@crossmint/client-sdk-base');var y=class k extends _commonsdkauth.CrossmintAuth{constructor(e,r,t={}){var s,a;super(e,r,t);this.refreshTask=null;this.refreshPromise=null;this.callbacks=(s=t.callbacks)!=null?s:{},this.logoutRoute=(a=t.logoutRoute)!=null?a:null}static from(e,r={}){let t=new k(e,_commonsdkauth.CrossmintAuth.defaultApiClient(e),r);return typeof window!="undefined"&&t.handleRefreshAuthMaterial(),t}getUser(){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var e;try{let r=yield this.apiClient.get(`api/${_commonsdkauth.CROSSMINT_API_VERSION}/sdk/auth/user`,{headers:{"Content-Type":"application/json"}});if(!r.ok)throw new Error((e=JSON.parse(yield r.text()))==null?void 0:e.message);return yield r.json()}catch(r){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to fetch user: ${r instanceof Error?r.message:"Unknown error"}`)}})}storeAuthMaterial(e){_chunkNYYORERKcjs.b.call(void 0, _commonsdkauth.SESSION_PREFIX,e.jwt),_chunkNYYORERKcjs.b.call(void 0, _commonsdkauth.REFRESH_TOKEN_PREFIX,e.refreshToken.secret,e.refreshToken.expiresAt)}logout(){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r,t;let e=_chunkNYYORERKcjs.a.call(void 0, _commonsdkauth.REFRESH_TOKEN_PREFIX);_chunkNYYORERKcjs.c.call(void 0, _commonsdkauth.REFRESH_TOKEN_PREFIX),_chunkNYYORERKcjs.c.call(void 0, _commonsdkauth.SESSION_PREFIX),(t=(r=this.callbacks).onLogout)==null||t.call(r);try{this.logoutRoute!=null?yield this.logoutFromCustomRoute():e!=null&&(yield this.logoutFromDefaultRoute(e))}catch(s){console.error(s)}})}handleRefreshAuthMaterial(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var t,s;let r=e!=null?e:_chunkNYYORERKcjs.a.call(void 0, _commonsdkauth.REFRESH_TOKEN_PREFIX);if(!(r==null&&this.refreshRoute==null))try{this.refreshPromise==null&&(this.refreshPromise=this.refreshAuthMaterial(r));let a=yield this.refreshPromise;this.refreshRoute==null&&this.storeAuthMaterial(a),(s=(t=this.callbacks).onTokenRefresh)==null||s.call(t,a),this.scheduleNextRefresh(a.jwt)}catch(a){console.error(a),this.logout()}finally{this.refreshPromise=null}})}getOAuthUrl(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r;try{let t=yield this.apiClient.get(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/social/${e}/start`,{headers:{"Content-Type":"application/json"}});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return(yield t.json()).oauthUrl}catch(t){if(console.error(`Failed to get OAuth URL for provider ${e}: ${t instanceof Error?t.message:"Unknown error"}`),t instanceof Error&&t.message.includes("Request from origin")){let s=t.message.match(/origin "([^"]+)"/),a=s==null?void 0:s[1];if(a)throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Unauthorized origin: ${a}. Please add this origin to your API key's authorized origins in the Crossmint Console.`)}throw new (0, _commonsdkauth.CrossmintAuthenticationError)("Unable to load oauth providers. Please try again later.")}})}sendEmailOtp(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r;try{let t=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/otps/send`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return yield t.json()}catch(t){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to send email OTP: ${t instanceof Error?t.message:"Unknown error"}`)}})}confirmEmailOtp(e,r,t){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var s;try{let a=new URLSearchParams({email:e,signinAuthenticationMethod:"email",token:t,locale:"en",state:r,callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}),l=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/authenticate?${a}`,{headers:{"Content-Type":"application/json"}});if(!l.ok)throw new Error((s=JSON.parse(yield l.text()))==null?void 0:s.message);return(yield l.json()).oneTimeSecret}catch(a){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to confirm email OTP: ${a instanceof Error?a.message:"Unknown error"}`)}})}signInWithFarcaster(e){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var r;try{let t=new URLSearchParams({signinAuthenticationMethod:"farcaster",callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}),s=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/authenticate?${t}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify(_chunkCK4JCQY6cjs.b.call(void 0, _chunkCK4JCQY6cjs.a.call(void 0, {},e),{domain:e.signatureParams.domain,redirect:!0,callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}))});if(!s.ok)throw new Error((r=JSON.parse(yield s.text()))==null?void 0:r.message);return(yield s.json()).oneTimeSecret}catch(t){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to sign in with Farcaster: ${t instanceof Error?t.message:"Unknown error"}`)}})}signInWithSmartWallet(e,r){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var t;try{let s=r==="evm"?"ethereum":"solana",a=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/crypto_wallets/authenticate/start`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,walletType:s})});if(!a.ok)throw new Error((t=JSON.parse(yield a.text()))==null?void 0:t.message);return yield a.json()}catch(s){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to initiate smart wallet sign in: ${s instanceof Error?s.message:"Unknown error"}`)}})}authenticateSmartWallet(e,r,t){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){var s;try{let a=new URLSearchParams({signinAuthenticationMethod:r,callbackUrl:`${this.apiClient.baseUrl}/${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/callback`}),l=yield this.apiClient.post(`${_commonsdkauth.AUTH_SDK_ROOT_ENDPOINT}/crypto_wallets/authenticate?${a}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,signature:t})});if(!l.ok)throw new Error((s=JSON.parse(yield l.text()))==null?void 0:s.message);return yield l.json()}catch(a){throw new (0, _commonsdkauth.CrossmintAuthenticationError)(`Failed to authenticate smart wallet: ${a instanceof Error?a.message:"Unknown error"}`)}})}logoutFromCustomRoute(){return _chunkCK4JCQY6cjs.c.call(void 0, this,null,function*(){if(!this.logoutRoute)throw new Error("Custom logout route is not set");return yield fetch(this.logoutRoute,{method:"POST"})})}scheduleNextRefresh(e){let r=_chunkVDJHVTKIcjs.a.call(void 0, e);if(!r)throw new Error("Invalid JWT");let t=Date.now()/1e3,s=r-t-120;if(s>0){let a=Date.now()+s*1e3;this.cancelScheduledRefresh(),this.refreshTask=_clientsdkbase.queueTask.call(void 0, ()=>this.handleRefreshAuthMaterial(),a)}}cancelScheduledRefresh(){this.refreshTask&&(this.refreshTask.cancel(),this.refreshTask=null)}};exports.a = y;
@@ -1 +0,0 @@
1
- import{a as g}from"./chunk-REIMEXVF.js";import{a as c,b as u,c as p}from"./chunk-HU56N5SW.js";import{a as f}from"./chunk-NIH25R3L.js";import{a as m,b as w,c as o}from"./chunk-22GIA4MK.js";import{AUTH_SDK_ROOT_ENDPOINT as i,CROSSMINT_API_VERSION as b,CrossmintAuth as d,CrossmintAuthenticationError as n,REFRESH_TOKEN_PREFIX as h,SESSION_PREFIX as C}from"@crossmint/common-sdk-auth";import{queueTask as R}from"@crossmint/client-sdk-base";var y=class k extends d{constructor(e,r,t={}){var s,a;super(e,r,t);this.refreshTask=null;this.refreshPromise=null;this.callbacks=(s=t.callbacks)!=null?s:{},this.logoutRoute=(a=t.logoutRoute)!=null?a:null}static from(e,r={}){let t=new k(e,d.defaultApiClient(e),r);return typeof window!="undefined"&&t.handleRefreshAuthMaterial(),t}getUser(){return o(this,null,function*(){var e;try{let r=yield this.apiClient.get(`api/${b}/sdk/auth/user`,{headers:{"Content-Type":"application/json"}});if(!r.ok)throw new Error((e=JSON.parse(yield r.text()))==null?void 0:e.message);return yield r.json()}catch(r){throw new n(`Failed to fetch user: ${r instanceof Error?r.message:"Unknown error"}`)}})}storeAuthMaterial(e){u(C,e.jwt),u(h,e.refreshToken.secret,e.refreshToken.expiresAt)}logout(){return o(this,null,function*(){var r,t;let e=c(h);p(h),p(C),(t=(r=this.callbacks).onLogout)==null||t.call(r);try{this.logoutRoute!=null?yield this.logoutFromCustomRoute():e!=null&&(yield this.logoutFromDefaultRoute(e))}catch(s){console.error(s)}})}handleRefreshAuthMaterial(e){return o(this,null,function*(){var t,s;let r=e!=null?e:c(h);if(!(r==null&&this.refreshRoute==null))try{this.refreshPromise==null&&(this.refreshPromise=this.refreshAuthMaterial(r));let a=yield this.refreshPromise;this.refreshRoute==null&&this.storeAuthMaterial(a),(s=(t=this.callbacks).onTokenRefresh)==null||s.call(t,a),this.scheduleNextRefresh(a.jwt)}catch(a){console.error(a),this.logout()}finally{this.refreshPromise=null}})}getOAuthUrl(e){return o(this,null,function*(){var r;try{let t=yield this.apiClient.get(`${i}/social/${e}/start`,{headers:{"Content-Type":"application/json"}});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return(yield t.json()).oauthUrl}catch(t){if(console.error(`Failed to get OAuth URL for provider ${e}: ${t instanceof Error?t.message:"Unknown error"}`),t instanceof Error&&t.message.includes("Request from origin")){let s=t.message.match(/origin "([^"]+)"/),a=s==null?void 0:s[1];if(a)throw new n(`Unauthorized origin: ${a}. Please add this origin to your API key's authorized origins in the Crossmint Console.`)}throw new n("Unable to load oauth providers. Please try again later.")}})}sendEmailOtp(e){return o(this,null,function*(){var r;try{let t=yield this.apiClient.post(`${i}/otps/send`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({email:e})});if(!t.ok)throw new Error((r=JSON.parse(yield t.text()))==null?void 0:r.message);return yield t.json()}catch(t){throw new n(`Failed to send email OTP: ${t instanceof Error?t.message:"Unknown error"}`)}})}confirmEmailOtp(e,r,t){return o(this,null,function*(){var s;try{let a=new URLSearchParams({email:e,signinAuthenticationMethod:"email",token:t,locale:"en",state:r,callbackUrl:`${this.apiClient.baseUrl}/${i}/callback`}),l=yield this.apiClient.post(`${i}/authenticate?${a}`,{headers:{"Content-Type":"application/json"}});if(!l.ok)throw new Error((s=JSON.parse(yield l.text()))==null?void 0:s.message);return(yield l.json()).oneTimeSecret}catch(a){throw new n(`Failed to confirm email OTP: ${a instanceof Error?a.message:"Unknown error"}`)}})}signInWithFarcaster(e){return o(this,null,function*(){var r;try{let t=new URLSearchParams({signinAuthenticationMethod:"farcaster",callbackUrl:`${this.apiClient.baseUrl}/${i}/callback`}),s=yield this.apiClient.post(`${i}/authenticate?${t}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify(w(m({},e),{domain:e.signatureParams.domain,redirect:!0,callbackUrl:`${this.apiClient.baseUrl}/${i}/callback`}))});if(!s.ok)throw new Error((r=JSON.parse(yield s.text()))==null?void 0:r.message);return(yield s.json()).oneTimeSecret}catch(t){throw new n(`Failed to sign in with Farcaster: ${t instanceof Error?t.message:"Unknown error"}`)}})}signInWithSmartWallet(e,r){return o(this,null,function*(){var t;try{let s=r==="evm"?"ethereum":"solana",a=yield this.apiClient.post(`${i}/crypto_wallets/authenticate/start`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,walletType:s})});if(!a.ok)throw new Error((t=JSON.parse(yield a.text()))==null?void 0:t.message);return yield a.json()}catch(s){throw new n(`Failed to initiate smart wallet sign in: ${s instanceof Error?s.message:"Unknown error"}`)}})}authenticateSmartWallet(e,r,t){return o(this,null,function*(){var s;try{let a=new URLSearchParams({signinAuthenticationMethod:r,callbackUrl:`${this.apiClient.baseUrl}/${i}/callback`}),l=yield this.apiClient.post(`${i}/crypto_wallets/authenticate?${a}`,{headers:{"Content-Type":"application/json"},body:JSON.stringify({walletAddress:e,signature:t})});if(!l.ok)throw new Error((s=JSON.parse(yield l.text()))==null?void 0:s.message);return yield l.json()}catch(a){throw new n(`Failed to authenticate smart wallet: ${a instanceof Error?a.message:"Unknown error"}`)}})}logoutFromCustomRoute(){return o(this,null,function*(){if(!this.logoutRoute)throw new Error("Custom logout route is not set");return yield fetch(this.logoutRoute,{method:"POST"})})}scheduleNextRefresh(e){let r=f(e);if(!r)throw new Error("Invalid JWT");let t=Date.now()/1e3,s=r-t-120;if(s>0){let a=Date.now()+s*1e3;this.cancelScheduledRefresh(),this.refreshTask=R(()=>this.handleRefreshAuthMaterial(),a)}}cancelScheduledRefresh(){this.refreshTask&&(this.refreshTask.cancel(),this.refreshTask=null)}};export{y as a};
File without changes
File without changes