Skip to content

Commit 63288b2

Browse files
committed
fix: update google auth
1 parent da3b384 commit 63288b2

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

app/controllers/auth.controller.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import type {
66
} from '@/schemas'
77
import type { JwtPayload } from '@/types'
88
import type { NextFunction, Request, Response } from 'express'
9-
import type {
10-
TypedRequestBody,
11-
TypedRequestQuery
12-
} from 'zod-express-middleware'
9+
import type { TypedRequestBody } from 'zod-express-middleware'
1310

1411
import { prisma } from '@/prisma'
1512
import { hash, verify } from 'argon2'
@@ -95,21 +92,21 @@ class AuthController {
9592
res.json({ user: userWithoutPassword, ...tokens })
9693
}
9794

98-
googleRedirect = async (_: Request, res: Response) => {
95+
getGoogleRedirectUrl = async (_: Request, res: Response) => {
9996
const url = this.googleClient.generateAuthUrl({
10097
access_type: 'offline',
10198
scope: ['profile', 'email']
10299
})
103100

104-
res.redirect(url)
101+
res.json({ redirectUrl: url })
105102
}
106103

107104
googleCallback = async (
108-
req: TypedRequestQuery<typeof GoogleCodeSchema>,
105+
req: TypedRequestBody<typeof GoogleCodeSchema>,
109106
res: Response,
110107
next: NextFunction
111108
) => {
112-
const { tokens } = await this.googleClient.getToken(req.query.code)
109+
const { tokens } = await this.googleClient.getToken(req.body.code)
113110

114111
if (!tokens.id_token) return next(Forbidden())
115112

app/routes/api/auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ authRouter.post(
2626
authController.signin
2727
)
2828

29-
authRouter.post('/google/redirect', authController.googleRedirect)
29+
authRouter.get('/google/initiate', authController.getGoogleRedirectUrl)
3030

31-
authRouter.get(
31+
authRouter.post(
3232
'/google/callback',
33-
validateRequest({ query: GoogleCodeSchema }),
33+
validateRequest({ body: GoogleCodeSchema }),
3434
authController.googleCallback
3535
)
3636

swagger.json

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,36 @@
8787
}
8888
}
8989
},
90-
"/auth/google/redirect": {
91-
"post": {
90+
"/auth/google/initiate": {
91+
"get": {
9292
"tags": ["Auth"],
93-
"summary": "Redirect to Google for OAuth authentication",
93+
"summary": "Get redirect url for Google authentication",
9494
"responses": {
95-
"302": {
96-
"headers": {
97-
"Location": {
98-
"description": "The URL to which the client should be redirected for Google authentication.",
99-
"schema": {
100-
"type": "string",
101-
"format": "uri",
102-
"example": "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=profile%20email&response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI"
103-
}
104-
}
105-
}
106-
}
95+
"200": { "$ref": "#/components/responses/GoogleInitiateResponse" }
10796
}
10897
}
10998
},
11099
"/auth/google/callback": {
111-
"get": {
100+
"post": {
112101
"tags": ["Auth"],
113102
"summary": "Handle Google OAuth callback and sign-in/sign-up",
114-
"parameters": [
115-
{
116-
"name": "code",
117-
"in": "query",
118-
"required": true,
119-
"schema": {
120-
"type": "string",
121-
"example": "4/0AT5xKif_S_..."
103+
"requestBody": {
104+
"required": true,
105+
"content": {
106+
"application/json": {
107+
"schema": {
108+
"type": "object",
109+
"required": ["code"],
110+
"properties": {
111+
"code": {
112+
"type": "string",
113+
"example": "4/0AT5xKif_S_..."
114+
}
115+
}
116+
}
122117
}
123118
}
124-
],
119+
},
125120
"responses": {
126121
"200": { "$ref": "#/components/responses/GoogleResponse" },
127122
"400": { "$ref": "#/components/responses/BadRequestError" },
@@ -1044,6 +1039,22 @@
10441039
}
10451040
}
10461041
},
1042+
"GoogleInitiateResponse": {
1043+
"content": {
1044+
"application/json": {
1045+
"schema": {
1046+
"type": "object",
1047+
"properties": {
1048+
"redirectUrl": {
1049+
"type": "string",
1050+
"format": "url",
1051+
"example": "https://accounts.google.com/o/oauth2/v2/auth?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&response_type=code&scope=email%20profile&access_type=offline"
1052+
}
1053+
}
1054+
}
1055+
}
1056+
}
1057+
},
10471058
"GoogleResponse": {
10481059
"content": {
10491060
"application/json": {

0 commit comments

Comments
 (0)