1
1
import Image from "next/image" ;
2
2
import styles from "./index.module.css" ;
3
- import { createActivityPoller , TurnkeyClient } from "@turnkey/http" ;
4
- import { IframeStamper } from "@turnkey/iframe-stamper" ;
3
+ import { useTurnkey } from "@turnkey/sdk-react" ;
5
4
import { useForm } from "react-hook-form" ;
6
5
import axios from "axios" ;
7
6
import * as React from "react" ;
8
7
import { useState } from "react" ;
9
- import { Auth } from "@/components/Auth" ;
10
8
11
9
/**
12
10
* Type definition for the server response coming back from `/api/auth`
@@ -32,9 +30,7 @@ type AuthFormData = {
32
30
33
31
export default function AuthPage ( ) {
34
32
const [ authResponse , setAuthResponse ] = useState < AuthResponse | null > ( null ) ;
35
- const [ iframeStamper , setIframeStamper ] = useState < IframeStamper | null > (
36
- null
37
- ) ;
33
+ const { authIframeClient } = useTurnkey ( ) ;
38
34
const { register : authFormRegister , handleSubmit : authFormSubmit } =
39
35
useForm < AuthFormData > ( ) ;
40
36
const {
@@ -43,75 +39,60 @@ export default function AuthPage() {
43
39
} = useForm < InjectCredentialsFormData > ( ) ;
44
40
45
41
const auth = async ( data : AuthFormData ) => {
46
- if ( iframeStamper === null ) {
42
+ if ( authIframeClient === null ) {
47
43
throw new Error ( "cannot initialize auth without an iframe" ) ;
48
44
}
49
45
50
46
const response = await axios . post ( "/api/auth" , {
51
47
suborgID : data . suborgID ,
52
48
email : data . email ,
53
- targetPublicKey : iframeStamper . publicKey ( ) ,
49
+ targetPublicKey : authIframeClient ! . iframePublicKey ,
54
50
invalidateExisting : data . invalidateExisting ,
55
51
} ) ;
56
52
57
53
setAuthResponse ( response . data ) ;
58
54
} ;
59
55
60
56
const injectCredentials = async ( data : InjectCredentialsFormData ) => {
61
- if ( iframeStamper === null ) {
62
- throw new Error ( "iframeStamper is null" ) ;
57
+ if ( authIframeClient === null ) {
58
+ throw new Error ( "iframe client is null" ) ;
63
59
}
64
60
if ( authResponse === null ) {
65
61
throw new Error ( "authResponse is null" ) ;
66
62
}
67
-
68
63
try {
69
- await iframeStamper . injectCredentialBundle ( data . authBundle ) ;
64
+ await authIframeClient ! . injectCredentialBundle (
65
+ data . authBundle
66
+ ) ;
70
67
} catch ( e ) {
71
68
const msg = `error while injecting bundle: ${ e } ` ;
72
69
console . error ( msg ) ;
73
70
alert ( msg ) ;
74
71
return ;
75
72
}
76
73
77
- const client = new TurnkeyClient (
78
- {
79
- baseUrl : process . env . NEXT_PUBLIC_BASE_URL ! ,
80
- } ,
81
- iframeStamper
82
- ) ;
83
-
84
74
// get whoami for suborg
85
- const whoamiResponse = await client . getWhoami ( {
75
+ const whoamiResponse = await authIframeClient ! . getWhoami ( {
86
76
organizationId : process . env . NEXT_PUBLIC_ORGANIZATION_ID ! ,
87
77
} ) ;
88
78
89
79
const orgID = whoamiResponse . organizationId ;
90
80
91
- const activityPoller = createActivityPoller ( {
92
- client,
93
- requestFn : client . createWallet ,
94
- } ) ;
95
-
96
- const completedActivity = await activityPoller ( {
97
- type : "ACTIVITY_TYPE_CREATE_WALLET" ,
98
- timestampMs : String ( Date . now ( ) ) ,
81
+ const createWalletResponse = await authIframeClient ! . createWallet ( {
99
82
organizationId : orgID ,
100
- parameters : {
101
- walletName : data . walletName ,
102
- accounts : [
103
- {
104
- curve : "CURVE_SECP256K1" ,
105
- pathFormat : "PATH_FORMAT_BIP32" ,
106
- path : "m/44'/60'/0'/0/0" ,
107
- addressFormat : "ADDRESS_FORMAT_ETHEREUM" ,
108
- } ,
109
- ] ,
110
- } ,
83
+ walletName : data . walletName ,
84
+ accounts : [
85
+ {
86
+ curve : "CURVE_SECP256K1" ,
87
+ pathFormat : "PATH_FORMAT_BIP32" ,
88
+ path : "m/44'/60'/0'/0/0" ,
89
+ addressFormat : "ADDRESS_FORMAT_ETHEREUM" ,
90
+ } ,
91
+ ] ,
111
92
} ) ;
112
93
113
- const wallet = refineNonNull ( completedActivity . result . createWalletResult ) ;
114
- const address = refineNonNull ( wallet . addresses [ 0 ] ) ;
94
+ const wallet = refineNonNull ( createWalletResponse . walletId ) ;
95
+ const address = refineNonNull ( createWalletResponse . addresses [ 0 ] ) ;
115
96
116
97
alert ( `SUCCESS! Wallet and new address created: ${ address } ` ) ;
117
98
} ;
@@ -133,15 +114,10 @@ export default function AuthPage() {
133
114
/>
134
115
</ a >
135
116
136
- < Auth
137
- setIframeStamper = { setIframeStamper }
138
- iframeUrl = { process . env . NEXT_PUBLIC_AUTH_IFRAME_URL ! }
139
- turnkeyBaseUrl = { process . env . NEXT_PUBLIC_BASE_URL ! }
140
- > </ Auth >
141
117
142
- { ! iframeStamper && < p > Loading...</ p > }
118
+ { ! authIframeClient && < p > Loading...</ p > }
143
119
144
- { iframeStamper && iframeStamper . publicKey ( ) && authResponse === null && (
120
+ { authIframeClient && authIframeClient . iframePublicKey && authResponse === null && (
145
121
< form className = { styles . form } onSubmit = { authFormSubmit ( auth ) } >
146
122
< label className = { styles . label } >
147
123
Email
@@ -171,16 +147,16 @@ export default function AuthPage() {
171
147
< label className = { styles . label } >
172
148
Encryption Target from iframe:
173
149
< br />
174
- < code title = { iframeStamper . publicKey ( ) ! } >
175
- { iframeStamper . publicKey ( ) ! . substring ( 0 , 30 ) } ...
150
+ < code title = { authIframeClient . iframePublicKey ! } >
151
+ { authIframeClient . iframePublicKey ! . substring ( 0 , 30 ) } ...
176
152
</ code >
177
153
</ label >
178
154
179
155
< input className = { styles . button } type = "submit" value = "Auth" />
180
156
</ form >
181
157
) }
182
158
183
- { iframeStamper && iframeStamper . publicKey ( ) && authResponse !== null && (
159
+ { authIframeClient && authIframeClient . iframePublicKey && authResponse !== null && (
184
160
< form
185
161
className = { styles . form }
186
162
onSubmit = { injectCredentialsFormSubmit ( injectCredentials ) }
0 commit comments