File tree Expand file tree Collapse file tree 7 files changed +58
-17
lines changed
app/connect/in-app-wallet
packages/thirdweb/src/wallets/in-app Expand file tree Collapse file tree 7 files changed +58
-17
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " thirdweb " : patch
3+ ---
4+
5+ Prevent popup opening when logging in with auth mode: "redirect"
Original file line number Diff line number Diff line change @@ -80,15 +80,13 @@ function AnyAuth() {
8080 limits on customizations and auth methods.
8181 </ p >
8282 < CodeExample
83- preview = {
84- < div className = "w-1/2" >
85- < CustomLoginForm />
86- </ div >
87- }
83+ preview = { < CustomLoginForm /> }
8884 code = { `import { useState } from "react";
8985import { useConnect } from "thirdweb/react";
9086import { inAppWallet, preAuthenticate } from "thirdweb/wallets/in-app";
9187
88+ const wallet = inAppWallet();
89+
9290export function CustomLoginUi() {
9391 const { connect, isConnecting, error } = useConnect();
9492
@@ -101,10 +99,9 @@ export function CustomLoginUi() {
10199 });
102100 };
103101
104- const handleLogin = async (email: string, verificationCode: string) => {
102+ const loginWithEmail = async (email: string, verificationCode: string) => {
105103 // verify email with verificationCode and connect
106104 connect(async () => {
107- const wallet = inAppWallet();
108105 await wallet.connect({
109106 client,
110107 strategy: "email",
@@ -114,6 +111,17 @@ export function CustomLoginUi() {
114111 return wallet;
115112 });
116113 };
114+
115+ const loginWithGoogle = async () => {
116+ // connect with google
117+ connect(async () => {
118+ await wallet.connect({
119+ client,
120+ strategy: "google",
121+ });
122+ return wallet;
123+ });
124+ };
117125}
118126` }
119127 lang = "tsx"
Original file line number Diff line number Diff line change @@ -27,9 +27,26 @@ export function CustomLoginForm() {
2727 const wallet = inAppWallet ( ) ;
2828 await wallet . connect ( {
2929 strategy : "email" ,
30- client : THIRDWEB_CLIENT ,
3130 email,
3231 verificationCode,
32+ client : THIRDWEB_CLIENT ,
33+ } ) ;
34+ return wallet ;
35+ } ) ;
36+ } ;
37+
38+ const loginWithGoogle = async ( ) => {
39+ connect ( async ( ) => {
40+ const wallet = inAppWallet ( {
41+ auth : {
42+ options : [ "google" ] ,
43+ mode : "redirect" ,
44+ redirectUrl : `${ window . location . origin } /connect/in-app-wallet` ,
45+ } ,
46+ } ) ;
47+ await wallet . connect ( {
48+ strategy : "google" ,
49+ client : THIRDWEB_CLIENT ,
3350 } ) ;
3451 return wallet ;
3552 } ) ;
@@ -41,7 +58,7 @@ export function CustomLoginForm() {
4158
4259 if ( screen === "login" ) {
4360 return (
44- < div className = "flex flex-col space-y-2 " >
61+ < div className = "flex w-full flex-col space-y-4 " >
4562 < label htmlFor = "email" className = "font-medium text-sm" >
4663 Email Address
4764 </ label >
@@ -62,6 +79,14 @@ export function CustomLoginForm() {
6279 >
6380 { isConnecting ? "Submitting..." : "Submit" }
6481 </ button >
82+ < p className = "text-center text-sm text-white" > Or</ p >
83+ < button
84+ type = "button"
85+ onClick = { loginWithGoogle }
86+ className = "rounded-lg bg-blue-500 px-4 py-2 text-white transition-colors enabled:hover:bg-blue-600"
87+ >
88+ Login with Google
89+ </ button >
6590 { error && < p className = "max-w-[300px] text-red-500" > { error . message } </ p > }
6691 </ div >
6792 ) ;
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ export interface InAppConnector {
2121 strategy : SocialAuthOption ,
2222 mode ?: "redirect" | "popup" | "window" ,
2323 redirectUrl ?: string ,
24- ) : void ;
24+ ) : Promise < void > ;
2525 // Login takes an auth token and connects a user with it
2626 loginWithAuthToken ?(
2727 authResult : AuthStoredTokenWithCookieReturnType ,
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ export async function connectInAppWallet(
4646 ) {
4747 const strategy = options . strategy ;
4848 if ( socialAuthOptions . includes ( strategy as SocialAuthOption ) ) {
49- connector . authenticateWithRedirect (
49+ await connector . authenticateWithRedirect (
5050 strategy as SocialAuthOption ,
5151 createOptions ?. auth ?. mode ,
5252 createOptions ?. auth ?. redirectUrl ,
Original file line number Diff line number Diff line change @@ -26,13 +26,13 @@ const closeWindow = ({
2626 }
2727} ;
2828
29- export const loginWithOauthRedirect = ( options : {
29+ export async function loginWithOauthRedirect ( options : {
3030 authOption : OAuthOption ;
3131 client : ThirdwebClient ;
3232 ecosystem ?: Ecosystem ;
3333 redirectUrl ?: string ;
3434 mode ?: "redirect" | "popup" | "window" ;
35- } ) : void = > {
35+ } ) : Promise < void > {
3636 const loginUrl = getLoginUrl ( {
3737 ...options ,
3838 mode : options . mode || "redirect" ,
@@ -42,7 +42,10 @@ export const loginWithOauthRedirect = (options: {
4242 } else {
4343 window . open ( loginUrl ) ;
4444 }
45- } ;
45+ // wait for 5 secs for the redirect to happen
46+ // that way it interrupts the rest of the execution that would normally keep connecting
47+ await new Promise ( ( resolve ) => setTimeout ( resolve , 5000 ) ) ;
48+ }
4649
4750export const loginWithOauth = async ( options : {
4851 authOption : OAuthOption ;
Original file line number Diff line number Diff line change @@ -260,12 +260,12 @@ export class InAppWebConnector implements InAppConnector {
260260 } ) ;
261261 }
262262
263- authenticateWithRedirect (
263+ async authenticateWithRedirect (
264264 strategy : SocialAuthOption ,
265265 mode ?: "redirect" | "popup" | "window" ,
266266 redirectUrl ?: string ,
267- ) : void {
268- loginWithOauthRedirect ( {
267+ ) : Promise < void > {
268+ return loginWithOauthRedirect ( {
269269 authOption : strategy ,
270270 client : this . client ,
271271 ecosystem : this . ecosystem ,
You can’t perform that action at this time.
0 commit comments