Skip to content

Commit 96ec628

Browse files
committed
fix: update google auth
1 parent 4462e71 commit 96ec628

13 files changed

Lines changed: 71 additions & 52 deletions

File tree

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { GoogleCallbackPage } from '@/pages/auth'
21
import { createFileRoute } from '@tanstack/react-router'
32

3+
import { GoogleAuthLoader } from '@/features/session/google'
4+
45
import { SessionContracts } from '@/entities/session'
56

67
export const Route = createFileRoute('/auth/google/callback')({
7-
component: GoogleCallbackPage,
8+
component: GoogleAuthLoader,
89
validateSearch: SessionContracts.GoogleCallbackSearchSchema
910
})

src/entities/session/api/contracts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export const SignupDtoSchema = v.object({
1212
...SigninDtoSchema.entries
1313
})
1414

15+
export const InitiateGoogleResponseDtoSchema = v.object({
16+
redirectUrl: v.pipe(v.string(), v.url())
17+
})
18+
1519
export const GoogleSigninDtoSchema = v.object({
1620
code: v.pipe(v.string(), v.minLength(1))
1721
})

src/entities/session/api/endpoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SessionApiEndpoints {
55
signin = `${this.baseUrl}/signin`
66
refresh = `${this.baseUrl}/refresh`
77
logout = `${this.baseUrl}/logout`
8-
googleRedirect = `${this.baseUrl}/google/redirect`
8+
googleInitiate = `${this.baseUrl}/google/initiate`
99
googleCallback = `${this.baseUrl}/google/callback`
1010
}
1111

src/entities/session/api/service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { axiosInstance } from '@/shared/api'
1111

1212
import {
1313
GoogleSigninDtoSchema,
14+
InitiateGoogleResponseDtoSchema,
1415
RefreshTokenDtoSchema,
1516
SessionResponseDtoSchema,
1617
SigninDtoSchema,
@@ -47,12 +48,20 @@ export const sessionService = {
4748
return parsedData
4849
},
4950

51+
async initiateGoogleSignin() {
52+
const response = await axiosInstance.get(sessionApiEndpoints.googleInitiate)
53+
54+
const parsedData = parse(InitiateGoogleResponseDtoSchema, response.data)
55+
56+
return parsedData
57+
},
58+
5059
async signinWithGoogle(data: GoogleSigninDto) {
5160
const googleSigninDto = parse(GoogleSigninDtoSchema, data)
5261

53-
const response = await axiosInstance.get(
62+
const response = await axiosInstance.post(
5463
sessionApiEndpoints.googleCallback,
55-
{ params: googleSigninDto }
64+
googleSigninDto
5665
)
5766

5867
const parsedData = parse(SessionResponseDtoSchema, response.data)

src/entities/session/api/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { InferOutput } from 'valibot'
22
import type {
33
GoogleSigninDtoSchema,
4+
InitiateGoogleResponseDtoSchema,
45
RefreshTokenDtoSchema,
56
SessionResponseDtoSchema,
67
SigninDtoSchema,
@@ -14,3 +15,6 @@ export type SigninDto = InferOutput<typeof SigninDtoSchema>
1415
export type SignupDto = InferOutput<typeof SignupDtoSchema>
1516
export type GoogleSigninDto = InferOutput<typeof GoogleSigninDtoSchema>
1617
export type RefreshTokenDto = InferOutput<typeof RefreshTokenDtoSchema>
18+
export type InitiateGoogleResponseDto = InferOutput<
19+
typeof InitiateGoogleResponseDtoSchema
20+
>

src/entities/session/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ export * as SessionDtoTypes from './api/types'
33
export * as SessionContracts from './model/contracts'
44
export * as SessionDtoContracts from './api/contracts'
55
export { sessionService } from './api/service'
6-
export { sessionApiEndpoints } from './api/endpoints'
76
export { useSessionStore, getSessionStore, sessionActions } from './model/store'
Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useEffect } from 'react'
2-
import { useQuery } from '@tanstack/react-query'
1+
import { useMutation } from '@tanstack/react-query'
32
import { useNavigate, useSearch } from '@tanstack/react-router'
43
import { toast } from 'sonner'
54

@@ -12,26 +11,17 @@ export const useGoogleSignin = () => {
1211

1312
const navigate = useNavigate()
1413

15-
const { data, isPending, isSuccess, isError } = useQuery({
16-
queryKey: ['auth', 'google', 'callback', code],
17-
queryFn: () => sessionService.signinWithGoogle({ code })
18-
})
19-
20-
useEffect(() => {
21-
if (isSuccess) {
14+
return useMutation({
15+
mutationFn: () => sessionService.signinWithGoogle({ code }),
16+
onSuccess(data) {
2217
navigate({ to: '/dashboard' })
2318
authenticate(data)
24-
}
25-
}, [authenticate, data, isSuccess, navigate])
26-
27-
useEffect(() => {
28-
if (isError) {
19+
},
20+
onError() {
2921
navigate({ to: '/' })
3022
toast.error(
3123
'An error occurred during sign-in with Google. Please try again shortly.'
3224
)
3325
}
34-
}, [isError, navigate])
35-
36-
return { isPending }
26+
})
3727
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useMutation } from '@tanstack/react-query'
2+
3+
import { sessionService } from '@/entities/session'
4+
5+
export const useInitiateGoogleSignin = () =>
6+
useMutation({
7+
mutationFn: sessionService.initiateGoogleSignin,
8+
onSuccess({ redirectUrl }) {
9+
window.location.href = redirectUrl
10+
}
11+
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { ContinueWithGoogleButton } from './ui/ContinueWithGoogleButton'
2-
export { useGoogleSignin } from './api/useGoogleSignin'
2+
export { GoogleAuthLoader } from './ui/GoogleAuthLoader'
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
import { sessionApiEndpoints } from '@/entities/session'
2-
3-
import { env } from '@/shared/config'
41
import { Icon } from '@/shared/ui'
52

3+
import { useInitiateGoogleSignin } from '../api/useInitiateGoogleSignin'
4+
65
export const ContinueWithGoogleButton = () => {
7-
const actionUrl = `${env.VITE_API_BASE_URL}${sessionApiEndpoints.googleRedirect}`
6+
const { mutate } = useInitiateGoogleSignin()
87

98
return (
10-
<form
11-
action={actionUrl}
12-
method='post'>
13-
<button
14-
type='submit'
15-
className='flex w-84 items-center justify-center gap-2 rounded-lg
16-
bg-black py-2.5 text-center text-white'>
17-
<Icon
18-
name='google'
19-
className='size-7'
20-
/>
21-
Continue with Google
22-
</button>
23-
</form>
9+
<button
10+
type='button'
11+
className='flex w-84 items-center justify-center gap-2 rounded-lg bg-black
12+
py-2.5 text-center text-white'
13+
onClick={() => mutate()}>
14+
<Icon
15+
name='google'
16+
className='size-7'
17+
/>
18+
Continue with Google
19+
</button>
2420
)
2521
}

0 commit comments

Comments
 (0)