Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOTFIX] 소셜 회원가입 시 기존 회원의 가입 여부를 검증 로직 변경 #196

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
} from '../intefaces/exception';
import {
CreateUserCommand,
CreateUserReq
CreateUserReq,
toCommand
} from '../intefaces/createUserCommand';
import { PostBaseResponseDto } from '../intefaces/PostBaseResponseDto';
import { UserLoginDto } from '../intefaces/user/UserLoginDto';
Expand Down Expand Up @@ -37,6 +38,7 @@ import { SocialVendor } from '../models/socialVendor';
import IUser from '../models/interface/IUser';
import { AgeGroup } from '../models/user/ageGroup';
import { Gender } from '../models/user/gender';
import { createUser, validateNewLocalEmail } from '../services/userService';

/**
* @route GET /email
Expand Down Expand Up @@ -168,7 +170,7 @@ const verifyEmailTest = async (
};

/**
* @route /users
* @route POST /users
* @desc 회원가입 api
* @access Public
*/
Expand All @@ -183,18 +185,9 @@ const postUser = async (
throw new IllegalArgumentException('필요한 값이 없습니다.');
}

const createUserCommand: CreateUserCommand = {
email: req.body.email,
password: req.body.password,
nickname: req.body.nickname,
ageGroup: req.body.ageGroup
? AgeGroup[req.body.ageGroup]
: AgeGroup.UNDEFINED,
gender: req.body.gender ? Gender[req.body.gender] : Gender.ETC,
profileImgUrl: (req?.file as Express.MulterS3.File)?.location
};

const createdUser = await UserService.createUser(createUserCommand);
const createUserCommand = toCommand(req);
kyY00n marked this conversation as resolved.
Show resolved Hide resolved
await validateNewLocalEmail(createUserCommand.email);
const createdUser = await createUser(createUserCommand);

const jwt = getToken(createdUser._id);

Expand Down
17 changes: 16 additions & 1 deletion src/intefaces/createUserCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AgeGroup, AgeGroupKey } from '../models/user/ageGroup';
import { Gender, GenderKey } from '../models/user/gender';
import { TypedRequest } from '../types/TypedRequest';

interface CreateUserCommand {
email: string;
Expand All @@ -18,4 +19,18 @@ interface CreateUserReq {
gender?: GenderKey;
}

export { CreateUserCommand, CreateUserReq };
const toCommand = (req: TypedRequest<CreateUserReq>) => {
const createUserCommand: CreateUserCommand = {
email: req.body.email,
password: req.body.password,
nickname: req.body.nickname,
ageGroup: req.body.ageGroup
? AgeGroup[req.body.ageGroup]
: AgeGroup.UNDEFINED,
gender: req.body.gender ? Gender[req.body.gender] : Gender.ETC,
profileImgUrl: (req?.file as Express.MulterS3.File)?.location
};
return createUserCommand;
};

export { CreateUserCommand, CreateUserReq, toCommand };
5 changes: 2 additions & 3 deletions src/models/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ type UserDocument = IUser & IDocument;
const userSchema = new Schema<UserDocument>(
{
email: {
type: String,
unique: true
type: String
kyY00n marked this conversation as resolved.
Show resolved Hide resolved
},
socialId: {
type: String,
Expand Down Expand Up @@ -60,7 +59,7 @@ const userSchema = new Schema<UserDocument>(
}
);

userSchema.index({ socialVendor: 1, socialId: 1 }, { unique: true });
userSchema.index({ socialVendor: 1, email: 1 }, { unique: true });
DongLee99 marked this conversation as resolved.
Show resolved Hide resolved

const User = model<UserDocument>('User', userSchema);

Expand Down
42 changes: 15 additions & 27 deletions src/services/socialAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,22 @@ const findUserBySocial = async (
return user;
};

const join = async (socialMember: IUser): Promise<UserDocument> => {
if (socialMember.email) {
const alreadyEmail = await UserService.isEmailExisting(socialMember.email);
if (alreadyEmail) {
throw new IllegalArgumentException(
`같은 이메일로 가입된 유저가 있습니다. email: ${socialMember.email}`
);
}
const validateMemberState = async (preMember: IUser) => {
const alreadyUser = await User.findOne({
socialVendor: '$preMember.socialVendor',
socialId: '$preMember.socialId'
kyY00n marked this conversation as resolved.
Show resolved Hide resolved
});
if (alreadyUser) {
throw new IllegalArgumentException(
`해당 소셜에 같은 이메일로 가입된 유저가 있습니다. email: ${
preMember.email || '이메일 알 수 없음'
}`
);
}
};

const join = async (socialMember: IUser): Promise<UserDocument> => {
await validateMemberState(socialMember);
socialMember.nickname = await UserService.autoGenerateNicknameFrom(
socialMember.nickname
);
Expand All @@ -98,26 +105,7 @@ const findSocialUser = async (socialMember: IUser) => {
return alreadyMember;
};

const findOrCreateUserBySocialToken = async (vendor: string, token: string) => {
const response: SocialLoginResponse = await getUserFromSocialVendor(
token,
vendor
);
const socialVendor = SocialVendorExtension.getVendorByValue(vendor);
const socialMember: IUser = convertSocialToPiickle(socialVendor, response);
const isAlreadyMember = await isAlreadySocialMember(socialMember);
if (isAlreadyMember) {
const alreadyMember = await findUserBySocial(
socialVendor,
socialMember.socialId!
);
return alreadyMember;
}
return join(socialMember);
};

export {
findOrCreateUserBySocialToken,
findUserBySocial,
getPiickleUser,
isAlreadySocialMember,
Expand Down
9 changes: 5 additions & 4 deletions src/services/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ const isEmailExisting = async (email: string): Promise<boolean> => {
return false;
};

const validateEmail = async (email: string) => {
const validateNewLocalEmail = async (email: string) => {
const alreadyUser = await User.findOne({
email
email,
socialVendor: null
kyY00n marked this conversation as resolved.
Show resolved Hide resolved
});
if (alreadyUser) {
throw new IllegalArgumentException('이미 존재하는 이메일입니다.');
Expand All @@ -48,7 +49,6 @@ const createUser = async (command: CreateUserCommand) => {
gender,
profileImgUrl: profileImageUrl
} = command;
await validateEmail(email);

const user = new User({
email,
Expand Down Expand Up @@ -241,5 +241,6 @@ export {
createOrDeleteBookmark,
nicknameAlreadyExists,
autoGenerateNicknameFrom,
deleteUser
deleteUser,
validateNewLocalEmail
};
Loading