@@ -3,6 +3,7 @@ import { useCallback, useEffect } from 'react'
33import { useNavigate , useSearchParams } from 'react-router-dom'
44import toast from 'react-simple-toasts'
55import { useAuth } from 'src/features/auth'
6+ import { OAUTH_ERRORS } from 'src/features/auth/constants'
67import { firebaseAuth } from 'src/lib/firebase'
78
89export const AuthProvider = ( { children } : { children : React . ReactNode } ) => {
@@ -53,19 +54,32 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
5354 const messageListener = ( message : {
5455 action : string
5556 type ?: string
57+ error : string
5658 access_token ?: string
5759 provider ?: string
5860 } ) => {
5961 if ( message . type === 'TOKEN_RECEIVED' ) {
6062 const { access_token : token , provider } = message
63+ const oppositeProvider = provider === 'google' ? 'Github' : 'Google'
64+
6165 connectTheUser ( token , provider ) . catch ( ( error ) => {
6266 if ( error && error . code === 'auth/account-exists-with-different-credential' ) {
6367 setAuthError ( {
64- message :
65- 'You have an account with a different provider. Please sign in with that provider to continue.' ,
68+ message : `You've previously signed up using ${ oppositeProvider } . To continue, please sign in with ${ oppositeProvider } .` ,
69+ } )
70+ } else if ( error ) {
71+ setAuthError ( {
72+ message : OAUTH_ERRORS [ error ] || OAUTH_ERRORS [ 'default' ] ,
6673 } )
6774 }
6875 } )
76+ } else if ( message . type === 'ERROR_RECEIVED' ) {
77+ const { error } = message
78+ setAuthError ( {
79+ message : OAUTH_ERRORS [ error ] || OAUTH_ERRORS [ 'default' ] ,
80+ } )
81+ openAuthModal ( )
82+ navigate ( window . location . pathname , { replace : true } )
6983 }
7084 }
7185
@@ -87,19 +101,29 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
87101 useEffect ( ( ) => {
88102 const token = searchParams . get ( 'access_token' )
89103 const provider = searchParams . get ( 'provider' )
104+ const error = searchParams . get ( 'error' )
105+
106+ if ( error ) {
107+ setAuthError ( {
108+ message : OAUTH_ERRORS [ error ] || OAUTH_ERRORS [ 'default' ] ,
109+ } )
110+ openAuthModal ( )
111+ navigate ( window . location . pathname , { replace : true } )
112+ return
113+ }
90114 const oppositeProvider = provider === 'google' ? 'Github' : 'Google'
91115 connectTheUser ( token , provider ) . catch ( ( error ) => {
92- openAuthModal ( )
93- console . log ( 'error' , error )
94116 if ( error && error . code === 'auth/account-exists-with-different-credential' ) {
95117 setAuthError ( {
96118 message : `You've previously signed up using ${ oppositeProvider } . To continue, please sign in with ${ oppositeProvider } .` ,
97119 } )
98- } else {
120+ } else if ( error ) {
99121 setAuthError ( {
100- message : 'Error signing in, Please try again' ,
122+ message : OAUTH_ERRORS [ error ] || OAUTH_ERRORS [ 'default' ] ,
101123 } )
102124 }
125+ openAuthModal ( )
126+ navigate ( window . location . pathname , { replace : true } )
103127 } )
104128 } , [ searchParams ] )
105129 return < > { children } </ >
0 commit comments