11import { Extension } from '@magic-sdk/commons' ;
2- import type { CreateChannelAPIResponse , AuthenticateAPIResponse , AuthClientError } from '@farcaster/auth-client' ;
32import { FarcasterPayloadMethod } from './types' ;
43import { isMobile } from './utils' ;
54
@@ -15,9 +14,58 @@ const FarcasterLoginEventOnReceived = {
1514 Failed : 'failed' ,
1615} as const ;
1716
17+ interface CreateChannelAPIResponse {
18+ channelToken : string ;
19+ url : string ;
20+ nonce : string ;
21+ }
22+
23+ type Hex = `0x${string } `;
24+
25+ interface StatusAPIResponse {
26+ state : 'pending' | 'completed' ;
27+ nonce : string ;
28+ url : string ;
29+ message ?: string ;
30+ signature ?: `0x${string } `;
31+ fid ?: number ;
32+ username ?: string ;
33+ bio ?: string ;
34+ displayName ?: string ;
35+ pfpUrl ?: string ;
36+ verifications ?: Hex [ ] ;
37+ custody ?: Hex ;
38+ }
39+
40+ type AuthClientErrorCode =
41+ | 'unauthenticated'
42+ | 'unauthorized'
43+ | 'bad_request'
44+ | 'bad_request.validation_failure'
45+ | 'not_found'
46+ | 'not_implemented'
47+ | 'unavailable'
48+ | 'unknown' ;
49+
50+ interface AuthClientErrorOpts {
51+ message : string ;
52+ cause : Error | AuthClientError ;
53+ presentable : boolean ;
54+ }
55+
56+ declare class AuthClientError extends Error {
57+ readonly errCode : AuthClientErrorCode ;
58+ readonly presentable : boolean ;
59+ /**
60+ * @param errCode - the AuthClientError code for this message
61+ * @param context - a message, another Error, or a AuthClientErrorOpts
62+ */
63+ constructor ( errCode : AuthClientErrorCode , context : Partial < AuthClientErrorOpts > | string | Error ) ;
64+ }
65+
1866type FarcasterLoginEventHandlers = {
1967 [ FarcasterLoginEventOnReceived . OpenChannel ] : ( channel : CreateChannelAPIResponse ) => void ;
20- [ FarcasterLoginEventOnReceived . Success ] : ( data : AuthenticateAPIResponse ) => void ;
68+ [ FarcasterLoginEventOnReceived . Success ] : ( data : StatusAPIResponse ) => void ;
2169 [ FarcasterLoginEventOnReceived . Failed ] : ( error : AuthClientError ) => void ;
2270} ;
2371
@@ -37,8 +85,8 @@ export class FarcasterExtension extends Extension.Internal<'farcaster'> {
3785 'Content-Type' : 'application/json' ,
3886 } ,
3987 body : JSON . stringify ( {
40- domain : location . host ,
41- siweUri : location . origin ,
88+ domain : window . location . host ,
89+ siweUri : window . location . origin ,
4290 } ) ,
4391 } ) . then < CreateChannelAPIResponse > ( ( r ) => r . json ( ) ) ;
4492
@@ -53,7 +101,7 @@ export class FarcasterExtension extends Extension.Internal<'farcaster'> {
53101
54102 const showUI = params ?. showUI ?? DEFAULT_SHOW_UI ;
55103
56- const domain = location . origin ;
104+ const domain = window . location . origin ;
57105
58106 const payload = this . utils . createJsonRpcRequestPayload ( FarcasterPayloadMethod . FarcasterShowQR , [
59107 {
@@ -69,7 +117,7 @@ export class FarcasterExtension extends Extension.Internal<'farcaster'> {
69117 const handle = this . request < string , FarcasterLoginEventHandlers > ( payload ) ;
70118
71119 if ( isMobile ( ) ) {
72- location . href = this . channel . url ;
120+ window . location . href = this . channel . url ;
73121 }
74122
75123 return handle ;
0 commit comments