Skip to content

Commit 3df07be

Browse files
committed
feat: handle authentication errors and improve error messaging
1 parent 8286b4c commit 3df07be

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

public/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
window.addEventListener('message', (event) => {
2-
if (event.data.type === 'TOKEN_RECEIVED') {
2+
if (event.data.type === 'TOKEN_RECEIVED' || event.data.type === 'ERROR_RECEIVED') {
33
// Forward to content script
44
window.postMessage(event.data, '*')
55
}

public/content.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ document.documentElement.appendChild(script)
44

55
// Listen for messages from the injected script
66
window.addEventListener('message', (event) => {
7-
if (event.source !== window || !event.data || event.data.type !== 'TOKEN_RECEIVED') {
7+
if (
8+
event.source !== window ||
9+
!event.data ||
10+
event.data.type !== 'TOKEN_RECEIVED' ||
11+
event.data.type !== 'ERROR_RECEIVED'
12+
) {
813
return
914
}
1015

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const OAUTH_ERRORS: {
2+
[key: string]: string
3+
} = {
4+
access_denied: 'You have denied access to Hackertab, please try again.',
5+
invalid_grant: 'The authentication code is invalid or expired. Please try again.',
6+
default: 'An unexpected error occurred during sign in. Please try again.',
7+
}

src/providers/AuthProvider.tsx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useCallback, useEffect } from 'react'
33
import { useNavigate, useSearchParams } from 'react-router-dom'
44
import toast from 'react-simple-toasts'
55
import { useAuth } from 'src/features/auth'
6+
import { OAUTH_ERRORS } from 'src/features/auth/constants'
67
import { firebaseAuth } from 'src/lib/firebase'
78

89
export 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

Comments
 (0)