Skip to content

Commit 6c9ade3

Browse files
committed
feat: Enhance AuthModal with OAuth link handling and error management
1 parent 074b1f9 commit 6c9ade3

1 file changed

Lines changed: 84 additions & 26 deletions

File tree

src/features/auth/components/AuthModal.tsx

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,74 @@
1-
import { AuthProvider, signInWithPopup } from 'firebase/auth'
1+
//import { AuthProvider, signInWithPopup } from 'firebase/auth'
2+
import { AuthProvider } from 'firebase/auth'
3+
import { useCallback, useState } from 'react'
24
import { FaGithub } from 'react-icons/fa'
35
import { FcGoogle } from 'react-icons/fc'
46
import { IoHeartCircle } from 'react-icons/io5'
5-
import toast from 'react-simple-toasts'
67
import { Button, Modal } from 'src/components/Elements'
8+
import { BUILD_TARGET } from 'src/config'
79
import { useAuth } from 'src/features/auth'
8-
import { trackUserConnect } from 'src/lib/analytics'
9-
import { firebaseAuth, githubAuthProvider, googleAuthProvider } from 'src/lib/firebase'
10+
import { githubAuthProvider, googleAuthProvider } from 'src/lib/firebase'
11+
import { getBrowserName } from 'src/utils/Environment'
12+
import { checkHostPermissions, requestHostPermissions } from 'src/utils/Permissions'
13+
import { useGetOauthLink } from '../api/getOauthLink'
1014
import './authModal.css'
1115

1216
type AuthModalProps = {
1317
showAuth: boolean
1418
}
1519

1620
export const AuthModal = ({ showAuth }: AuthModalProps) => {
17-
const { closeAuthModal, initState } = useAuth()
21+
const { closeAuthModal, authError, setAuthError } = useAuth()
22+
const [selectedProvider, setSelectedProvider] = useState<AuthProvider>(googleAuthProvider)
23+
const getOauthLink = useGetOauthLink()
1824

19-
const signIn = (provider: AuthProvider) => {
20-
signInWithPopup(firebaseAuth, provider)
21-
.then((result) => {
22-
const name = result.user.displayName
23-
const imageURL = result.user.photoURL
24-
if (name && imageURL) {
25-
trackUserConnect(provider.providerId)
26-
closeAuthModal()
27-
initState({
28-
user: {
29-
name: name,
30-
imageURL: imageURL,
31-
},
32-
providerId: provider.providerId,
33-
})
34-
}
25+
const requestOauthLink = useCallback(
26+
async (provider: AuthProvider) => {
27+
const getOauthLinkResponse = await getOauthLink.mutateAsync({
28+
data: {
29+
provider: provider.providerId as 'google.com' | 'github.com',
30+
state: BUILD_TARGET === 'web' ? window.location.origin : getBrowserName(),
31+
},
3532
})
36-
.catch((error) => {
37-
console.log(error)
38-
toast(`We couldn't login to your ${provider.providerId} account!!`, { theme: 'failure' })
33+
34+
if (getOauthLinkResponse.authLink) {
35+
window.open(getOauthLinkResponse.authLink, BUILD_TARGET === 'web' ? '_self' : '_blank')
36+
}
37+
},
38+
[getOauthLink]
39+
)
40+
41+
const signIn = useCallback(async (provider: AuthProvider) => {
42+
setSelectedProvider(provider)
43+
setAuthError(null)
44+
45+
if (BUILD_TARGET === 'web') {
46+
requestOauthLink(provider)
47+
} else {
48+
const permissionCheck = await checkHostPermissions()
49+
50+
if (!permissionCheck) {
51+
setAuthError({
52+
message: 'Hackertab needs permission to Sign in, Please request it and try again',
53+
})
54+
} else {
55+
requestOauthLink(provider)
56+
}
57+
}
58+
}, [])
59+
60+
const requestMissingPermission = useCallback(async () => {
61+
const permissionRequest = await requestHostPermissions()
62+
63+
if (!permissionRequest) {
64+
setAuthError({
65+
message: 'Permission not granted, Request it again',
3966
})
40-
}
67+
} else {
68+
setAuthError(null)
69+
requestOauthLink(selectedProvider)
70+
}
71+
}, [])
4172

4273
return (
4374
<Modal
@@ -54,17 +85,44 @@ export const AuthModal = ({ showAuth }: AuthModalProps) => {
5485
<div className="buttons">
5586
<Button
5687
startIcon={<FaGithub className="blockHeaderWhite" style={{ fontSize: '1.5em' }} />}
57-
onClick={() => signIn(githubAuthProvider)}
88+
isLoading={getOauthLink.isLoading && getOauthLink.data?.['provider'] === 'github.com'}
89+
onClick={() => {
90+
signIn(githubAuthProvider)
91+
}}
5892
size="medium">
5993
Connect with Github
6094
</Button>
6195
<Button
96+
isLoading={getOauthLink.isLoading && getOauthLink.data?.['provider'] === 'google.com'}
6297
startIcon={<FcGoogle style={{ fontSize: '1.5em' }} />}
6398
onClick={() => signIn(googleAuthProvider)}
6499
size="medium">
65100
Connect with Google
66101
</Button>
67102
</div>
103+
{authError && (
104+
<div className="errors">
105+
<p>{authError.message}</p>
106+
{authError.retry && (
107+
<Button onClick={() => requestMissingPermission()} size="small" className="cta">
108+
{authError.retry.label}
109+
</Button>
110+
)}
111+
</div>
112+
)}
113+
<div className="footer">
114+
<p>
115+
By signing in, you agree to our{' '}
116+
<a href="https://hackertab.dev/terms-and-conditions" target="_blank" rel="noreferrer">
117+
Terms of Service
118+
</a>{' '}
119+
and{' '}
120+
<a href="https://hackertab.dev/privacy-policy" target="_blank" rel="noreferrer">
121+
Privacy Policy
122+
</a>
123+
.
124+
</p>
125+
</div>
68126
</div>
69127
</Modal>
70128
)

0 commit comments

Comments
 (0)