1
1
"use server" ;
2
2
3
3
import { TurnkeyServerSDK } from "./sdk-client" ;
4
- import { DEFAULT_ETHEREUM_ACCOUNTS , DEFAULT_SOLANA_ACCOUNTS , WalletAccount } from "./turnkey-helpers" ;
5
-
4
+ import {
5
+ DEFAULT_ETHEREUM_ACCOUNTS ,
6
+ DEFAULT_SOLANA_ACCOUNTS ,
7
+ WalletAccount ,
8
+ } from "./turnkey-helpers" ;
6
9
7
10
type Session = {
8
11
sessionType : string ;
9
12
userId : string ;
10
13
organizationId : string ;
11
14
expiry : number ;
12
- token : string | undefined
13
- }
15
+ token : string | undefined ;
16
+ } ;
14
17
15
18
type VerifyOtpRequest = {
16
19
suborgID : string ;
@@ -79,7 +82,7 @@ type Provider = {
79
82
80
83
type CreateSuborgResponse = {
81
84
subOrganizationId : string ;
82
- }
85
+ } ;
83
86
84
87
const turnkeyClient = new TurnkeyServerSDK ( {
85
88
apiBaseUrl : process . env . NEXT_PUBLIC_BASE_URL ! ,
@@ -88,7 +91,9 @@ const turnkeyClient = new TurnkeyServerSDK({
88
91
apiPublicKey : process . env . TURNKEY_API_PUBLIC_KEY ! ,
89
92
} ) ;
90
93
91
- export async function sendCredential ( request : InitEmailAuthRequest ) : Promise < void > {
94
+ export async function sendCredential (
95
+ request : InitEmailAuthRequest
96
+ ) : Promise < void > {
92
97
try {
93
98
const response = await turnkeyClient . apiClient ( ) . emailAuth ( {
94
99
email : request . email ,
@@ -98,8 +103,10 @@ export async function sendCredential(request: InitEmailAuthRequest): Promise<voi
98
103
...( request . sessionLengthSeconds !== undefined && {
99
104
expirationSeconds : request . sessionLengthSeconds . toString ( ) ,
100
105
} ) ,
101
- ...( request . invalidateExisting && { invalidateExisting : request . invalidateExisting } ) ,
102
- } )
106
+ ...( request . invalidateExisting && {
107
+ invalidateExisting : request . invalidateExisting ,
108
+ } ) ,
109
+ } ) ;
103
110
if ( ! response . userId ) {
104
111
throw new Error ( "Expected a non-null userId." ) ;
105
112
}
@@ -109,7 +116,9 @@ export async function sendCredential(request: InitEmailAuthRequest): Promise<voi
109
116
}
110
117
}
111
118
112
- export async function sendOtp ( request : SendOtpRequest ) : Promise < SendOtpResponse | undefined > {
119
+ export async function sendOtp (
120
+ request : SendOtpRequest
121
+ ) : Promise < SendOtpResponse | undefined > {
113
122
try {
114
123
const response = await turnkeyClient . apiClient ( ) . initOtpAuth ( {
115
124
contact : request . contact ,
@@ -130,7 +139,9 @@ export async function sendOtp(request: SendOtpRequest): Promise<SendOtpResponse
130
139
}
131
140
}
132
141
133
- export async function verifyOtp ( request : VerifyOtpRequest ) : Promise < Session | undefined > {
142
+ export async function verifyOtp (
143
+ request : VerifyOtpRequest
144
+ ) : Promise < Session | undefined > {
134
145
try {
135
146
const response = await turnkeyClient . apiClient ( ) . otpAuth ( {
136
147
otpId : request . otpId ,
@@ -144,23 +155,28 @@ export async function verifyOtp(request: VerifyOtpRequest): Promise<Session | un
144
155
145
156
const { credentialBundle, apiKeyId, userId } = response ;
146
157
if ( ! credentialBundle || ! apiKeyId || ! userId ) {
147
- throw new Error ( "Expected non-null values for credentialBundle, apiKeyId, and userId." ) ;
158
+ throw new Error (
159
+ "Expected non-null values for credentialBundle, apiKeyId, and userId."
160
+ ) ;
148
161
}
149
162
const session : Session = {
150
163
sessionType : "rw" ,
151
164
userId : userId ,
152
165
organizationId : request . suborgID ,
153
- expiry : Math . floor ( Date . now ( ) / 1000 ) + ( request . sessionLengthSeconds ?? 900 ) , //TODO change this to the actual expiry time from the response,
154
- token : credentialBundle
155
- }
166
+ expiry :
167
+ Math . floor ( Date . now ( ) / 1000 ) + ( request . sessionLengthSeconds ?? 900 ) , //TODO change this to the actual expiry time from the response,
168
+ token : credentialBundle ,
169
+ } ;
156
170
return session ;
157
171
} catch ( error ) {
158
172
console . error ( error ) ;
159
173
return undefined ;
160
174
}
161
175
}
162
176
163
- export async function oauth ( request : OauthRequest ) : Promise < Session | undefined > {
177
+ export async function oauth (
178
+ request : OauthRequest
179
+ ) : Promise < Session | undefined > {
164
180
try {
165
181
const response = await turnkeyClient . apiClient ( ) . oauth ( {
166
182
oidcToken : request . oidcToken ,
@@ -173,23 +189,28 @@ export async function oauth(request: OauthRequest): Promise<Session | undefined>
173
189
174
190
const { credentialBundle, apiKeyId, userId } = response ;
175
191
if ( ! credentialBundle || ! apiKeyId || ! userId ) {
176
- throw new Error ( "Expected non-null values for credentialBundle, apiKeyId, and userId." ) ;
192
+ throw new Error (
193
+ "Expected non-null values for credentialBundle, apiKeyId, and userId."
194
+ ) ;
177
195
}
178
196
const session : Session = {
179
197
sessionType : "rw" ,
180
198
userId : userId ,
181
199
organizationId : request . suborgID ,
182
- expiry : Math . floor ( Date . now ( ) / 1000 ) + ( request . sessionLengthSeconds ?? 900 ) , //TODO change this to the actual expiry time from the response,
183
- token : credentialBundle
184
- }
200
+ expiry :
201
+ Math . floor ( Date . now ( ) / 1000 ) + ( request . sessionLengthSeconds ?? 900 ) , //TODO change this to the actual expiry time from the response,
202
+ token : credentialBundle ,
203
+ } ;
185
204
return session ;
186
205
} catch ( error ) {
187
206
console . error ( error ) ;
188
207
return undefined ;
189
208
}
190
209
}
191
210
192
- export async function getSuborgs ( request : GetSuborgsRequest ) : Promise < GetSuborgsResponse | undefined > {
211
+ export async function getSuborgs (
212
+ request : GetSuborgsRequest
213
+ ) : Promise < GetSuborgsResponse | undefined > {
193
214
try {
194
215
const response = await turnkeyClient . apiClient ( ) . getSubOrgIds ( {
195
216
organizationId : turnkeyClient . config . defaultOrganizationId ,
@@ -207,7 +228,9 @@ export async function getSuborgs(request: GetSuborgsRequest): Promise<GetSuborgs
207
228
}
208
229
}
209
230
210
- export async function getVerifiedSuborgs ( request : GetSuborgsRequest ) : Promise < GetSuborgsResponse | undefined > {
231
+ export async function getVerifiedSuborgs (
232
+ request : GetSuborgsRequest
233
+ ) : Promise < GetSuborgsResponse | undefined > {
211
234
try {
212
235
const response = await turnkeyClient . apiClient ( ) . getVerifiedSubOrgIds ( {
213
236
organizationId : turnkeyClient . config . defaultOrganizationId ,
@@ -225,7 +248,9 @@ export async function getVerifiedSuborgs(request: GetSuborgsRequest): Promise<Ge
225
248
}
226
249
}
227
250
228
- export async function createSuborg ( request : CreateSuborgRequest ) : Promise < CreateSuborgResponse | undefined > {
251
+ export async function createSuborg (
252
+ request : CreateSuborgRequest
253
+ ) : Promise < CreateSuborgResponse | undefined > {
229
254
try {
230
255
const response = await turnkeyClient . apiClient ( ) . createSubOrganization ( {
231
256
subOrganizationName : `suborg-${ String ( Date . now ( ) ) } ` ,
@@ -234,7 +259,9 @@ export async function createSuborg(request: CreateSuborgRequest): Promise<Create
234
259
{
235
260
userName : request . email ?? "" ,
236
261
userEmail : request . email ?? "" ,
237
- ...( request . phoneNumber ? { userPhoneNumber : request . phoneNumber } : { } ) ,
262
+ ...( request . phoneNumber
263
+ ? { userPhoneNumber : request . phoneNumber }
264
+ : { } ) ,
238
265
apiKeys : [ ] ,
239
266
authenticators : request . passkey ? [ request . passkey ] : [ ] ,
240
267
oauthProviders : request . oauthProviders ?? [ ] ,
0 commit comments