Skip to content

Commit

Permalink
Merge pull request #13 from Roger13579/features/U003
Browse files Browse the repository at this point in the history
feat: add google oauth function, optimize route structure
  • Loading branch information
Roger13579 authored May 10, 2024
2 parents 250ea3f + 1dbe196 commit da7b573
Show file tree
Hide file tree
Showing 15 changed files with 381 additions and 63 deletions.
81 changes: 81 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"morgan": "^1.10.0",
"nodemailer": "^6.9.13",
"passport": "^0.7.0",
"passport-google-oauth20": "^2.0.0",
"passport-local": "^1.0.0",
"swagger-autogen": "^2.23.7",
"swagger-jsdoc": "^6.2.8",
Expand All @@ -44,6 +45,7 @@
"@types/morgan": "^1.9.9",
"@types/nodemailer": "^6.4.15",
"@types/passport": "^1.0.16",
"@types/passport-google-oauth20": "^2.0.16",
"@types/passport-local": "^1.0.38",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
Expand Down
46 changes: 43 additions & 3 deletions src/controller/indexController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Request } from 'express';
import { NextFunction, Request, Response } from 'express';
import { BaseController } from './baseController';
import { ResponseObject } from '../utils/responseObject';
import { CustomResponseType } from '../types/customResponseType';
import { validationResult } from 'express-validator';
import bcrypt from 'bcrypt';
import { SignUpVo } from '../vo/signUpVo';
import { LoginVo } from '../vo/loginVo';
import { UserService } from '../service/userService';
import { ResetPwdDto } from '../dto/resetPwdDto';

Expand All @@ -20,6 +20,21 @@ class IndexController extends BaseController {
CustomResponseType.OK,
);
};
public googleSignUp = async (req: Request): Promise<ResponseObject> => {
this.paramVerify(req);
const { account, pwd, confirmPwd } = req.body;
const thirdPartyId = (req.user as any).thirdPartyId;
await this.userService.updateUserFromGoogle(
account,
pwd,
confirmPwd,
thirdPartyId,
);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
);
};

public login = async (req: Request): Promise<ResponseObject> => {
this.paramVerify(req);
Expand All @@ -36,7 +51,7 @@ class IndexController extends BaseController {
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
new SignUpVo(user, jwt),
new LoginVo(user, jwt),
);
} else {
return this.formatResponse(
Expand Down Expand Up @@ -67,6 +82,31 @@ class IndexController extends BaseController {
});
};

public googleCallback = async (
req: Request,
res: Response,
next: NextFunction,
): Promise<ResponseObject> => {
const authUser = await this.userService.googleAuth(req, res, next);
if (authUser) {
const jwt = this.userService.generateJWT(
authUser.id,
authUser.accountType,
);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
new LoginVo(authUser, jwt),
);
} else {
console.log('authUser is empty');
return this.formatResponse(
CustomResponseType.GOOGLE_AUTH_ERROR_MESSAGE,
CustomResponseType.GOOGLE_AUTH_ERROR,
);
}
};

private paramVerify(req: Request) {
const result = validationResult(req);
if (!result.isEmpty()) {
Expand Down
31 changes: 31 additions & 0 deletions src/dto/googleProfileDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class GoogleProfileDto {
private readonly id: string;
private readonly name: string;
private readonly email: string;
private readonly picture: string;
private readonly emailVerified: boolean;

get getId(): string {
return this.id;
}
get getName(): string {
return this.name;
}
get getEmail(): string {
return this.email;
}
get getPicture(): string {
return this.picture;
}
get getEmailVerified(): boolean {
return this.emailVerified;
}

constructor(googleUser: any) {
this.id = googleUser._json.sub;
this.name = googleUser._json.name;
this.email = googleUser._json.email;
this.picture = googleUser._json.picture;
this.emailVerified = googleUser._json.email_verified;
}
}
46 changes: 23 additions & 23 deletions src/dto/resetPwdDto.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { Request } from 'express';

export class ResetPwdDto {
private readonly id: string;
private readonly oldPwd: string;
private readonly pwd: string;
private readonly confirmPwd: string;
private readonly id: string;
private readonly oldPwd: string;
private readonly pwd: string;
private readonly confirmPwd: string;

get getId(): string {
return this.id;
}
get getOldPwd(): string {
return this.oldPwd;
}
get getPwd(): string {
return this.pwd;
}
get getConfirmPwd(): string {
return this.confirmPwd;
}
get getId(): string {
return this.id;
}
get getOldPwd(): string {
return this.oldPwd;
}
get getPwd(): string {
return this.pwd;
}
get getConfirmPwd(): string {
return this.confirmPwd;
}

constructor(req: Request) {
this.id = (req.user as any).id;
const { oldPwd, pwd, confirmPwd } = req.body;
this.oldPwd = oldPwd;
this.pwd = pwd;
this.confirmPwd = confirmPwd;
}
constructor(req: Request) {
this.id = (req.user as any).id;
const { oldPwd, pwd, confirmPwd } = req.body;
this.oldPwd = oldPwd;
this.pwd = pwd;
this.confirmPwd = confirmPwd;
}
}
28 changes: 28 additions & 0 deletions src/middleware/passportInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import passport from 'passport';
import { NextFunction, Response, Request } from 'express';
import { Strategy as GoogleStrategy } from 'passport-google-oauth20';

export const PassportInit = async (
req: Request,
res: Response,
next: NextFunction,
) => {
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_OAUTH_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET as string,
callbackURL: process.env.GOOGLE_OAUTH_CALLBACK_URL as string,
},
(
accessToken: any,
refreshToken: any,
profile: any,
cb: (arg0: null, arg1: any) => any,
) => {
return cb(null, profile);
},
),
);
return next();
};
Loading

0 comments on commit da7b573

Please sign in to comment.