11import { useEffect , useState } from 'react' ;
2- import { cn } from '../../lib/classname.ts' ;
2+ import Cookies from 'js-cookie' ;
3+ import {
4+ FIRST_LOGIN_PARAM ,
5+ TOKEN_COOKIE_NAME ,
6+ setAuthToken ,
7+ } from '../../lib/jwt' ;
38import { httpGet } from '../../lib/http' ;
4- import { COURSE_PURCHASE_PARAM , setAuthToken } from '../../lib/jwt' ;
9+ import { COURSE_PURCHASE_PARAM } from '../../lib/jwt' ;
510import { GoogleIcon } from '../ReactIcons/GoogleIcon.tsx' ;
611import { Spinner } from '../ReactIcons/Spinner.tsx' ;
712import { CHECKOUT_AFTER_LOGIN_KEY } from './CourseLoginPopup.tsx' ;
813import {
914 getStoredUtmParams ,
1015 triggerUtmRegistration ,
1116} from '../../lib/browser.ts' ;
17+ import { cn } from '../../lib/classname.ts' ;
1218
1319type GoogleButtonProps = {
1420 isDisabled ?: boolean ;
@@ -37,14 +43,12 @@ export function GoogleButton(props: GoogleButtonProps) {
3743
3844 setIsLoading ( true ) ;
3945 setIsDisabled ?.( true ) ;
40- httpGet < { token : string } > (
46+ httpGet < { token : string ; isNewUser : boolean } > (
4147 `${ import . meta. env . PUBLIC_API_URL } /v1-google-callback${
4248 window . location . search
4349 } `,
4450 )
4551 . then ( ( { response, error } ) => {
46- const utmParams = getStoredUtmParams ( ) ;
47-
4852 if ( ! response ?. token ) {
4953 setError ( error ?. message || 'Something went wrong.' ) ;
5054 setIsLoading ( false ) ;
@@ -55,7 +59,7 @@ export function GoogleButton(props: GoogleButtonProps) {
5559
5660 triggerUtmRegistration ( ) ;
5761
58- let redirectUrl = '/' ;
62+ let redirectUrl = new URL ( '/' , window . location . origin ) ;
5963 const googleRedirectAt = localStorage . getItem ( GOOGLE_REDIRECT_AT ) ;
6064 const lastPageBeforeGoogle = localStorage . getItem ( GOOGLE_LAST_PAGE ) ;
6165
@@ -67,30 +71,36 @@ export function GoogleButton(props: GoogleButtonProps) {
6771 const timeSinceRedirect = now - socialRedirectAtTime ;
6872
6973 if ( timeSinceRedirect < 30 * 1000 ) {
70- redirectUrl = lastPageBeforeGoogle ;
74+ redirectUrl = new URL ( lastPageBeforeGoogle , window . location . origin ) ;
7175 }
7276 }
7377
7478 const authRedirectUrl = localStorage . getItem ( 'authRedirect' ) ;
7579 if ( authRedirectUrl ) {
7680 localStorage . removeItem ( 'authRedirect' ) ;
77- redirectUrl = authRedirectUrl ;
81+ redirectUrl = new URL ( authRedirectUrl , window . location . origin ) ;
82+ }
83+
84+ if ( response ?. isNewUser ) {
85+ redirectUrl . searchParams . set ( FIRST_LOGIN_PARAM , '1' ) ;
7886 }
7987
8088 const shouldTriggerPurchase =
8189 localStorage . getItem ( CHECKOUT_AFTER_LOGIN_KEY ) !== '0' ;
82- if ( redirectUrl . includes ( '/courses/sql' ) && shouldTriggerPurchase ) {
83- const tempUrl = new URL ( redirectUrl , window . location . origin ) ;
84- tempUrl . searchParams . set ( COURSE_PURCHASE_PARAM , '1' ) ;
85- redirectUrl = tempUrl . toString ( ) ;
90+ if (
91+ redirectUrl . pathname . includes ( '/courses/sql' ) &&
92+ shouldTriggerPurchase
93+ ) {
94+ redirectUrl . searchParams . set ( COURSE_PURCHASE_PARAM , '1' ) ;
8695
8796 localStorage . removeItem ( CHECKOUT_AFTER_LOGIN_KEY ) ;
8897 }
8998
9099 localStorage . removeItem ( GOOGLE_REDIRECT_AT ) ;
91100 localStorage . removeItem ( GOOGLE_LAST_PAGE ) ;
92101 setAuthToken ( response . token ) ;
93- window . location . href = redirectUrl ;
102+
103+ window . location . href = redirectUrl . toString ( ) ;
94104 } )
95105 . catch ( ( err ) => {
96106 setError ( 'Something went wrong. Please try again later.' ) ;
0 commit comments