-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from Khingz/FEAT-Google-auth
feat: reimplement google auth
- Loading branch information
Showing
10 changed files
with
233 additions
and
180 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { AuthService } from "../services/auth.services"; | ||
import { BadRequest } from "../middleware"; | ||
import { GoogleAuthService } from "../services/google.auth.service"; | ||
|
||
const authService = new AuthService(); | ||
|
||
|
@@ -333,11 +335,101 @@ const changePassword = async ( | |
} | ||
}; | ||
|
||
|
||
/** | ||
* @swagger | ||
* /api/v1/auth/google: | ||
* post: | ||
* summary: Handle Google authentication and register/login a user | ||
* description: This endpoint handles Google OAuth2.0 authentication. It accepts a Google user payload and either registers a new user or logs in an existing one. | ||
* tags: | ||
* - Auth | ||
* requestBody: | ||
* required: true | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* email: | ||
* type: string | ||
* format: email | ||
* description: The user's email address. | ||
* example: [email protected] | ||
* email_verified: | ||
* type: boolean | ||
* description: Whether the user's email is verified. | ||
* example: true | ||
* name: | ||
* type: string | ||
* description: The user's full name. | ||
* example: "John Doe" | ||
* picture: | ||
* type: string | ||
* format: url | ||
* description: URL to the user's profile picture. | ||
* example: "https://example.com/avatar.jpg" | ||
* sub: | ||
* type: string | ||
* description: Google user ID (subject claim). | ||
* example: "1234567890" | ||
* responses: | ||
* 200: | ||
* description: User authenticated successfully | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* message: | ||
* type: string | ||
* description: Verify if authentication is successful | ||
* example: Authentication successful | ||
* user: | ||
* type: object | ||
* description: The authenticated user object. | ||
* access_token: | ||
* type: string | ||
* description: JWT access token for authentication. | ||
* example: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." | ||
* 400: | ||
* description: Bad Request - Invalid or missing data in request body | ||
* 500: | ||
* description: Internal Server Error - An unexpected error occurred | ||
*/ | ||
const handleGoogleAuth = async ( | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
) => { | ||
const googleAuthService = new GoogleAuthService(); | ||
const userData = req.body; | ||
try { | ||
if (!userData) { | ||
throw new BadRequest("Bad request"); | ||
} | ||
const isDbUser = await googleAuthService.getUserByGoogleId(userData.sub); | ||
const dbUser = await googleAuthService.handleGoogleAuthUser( | ||
userData, | ||
isDbUser, | ||
); | ||
res.status(200).json({ | ||
status: "success", | ||
message: "User successfully authenticated", | ||
access_token: dbUser.access_token, | ||
user: dbUser.user | ||
}); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export { | ||
signUp, | ||
verifyOtp, | ||
login, | ||
forgotPassword, | ||
resetPassword, | ||
changePassword, | ||
handleGoogleAuth | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.