Skip to content

Commit

Permalink
feat : 회원가입 시 홈화면 진입을 위한 토큰값들 반환 #172
Browse files Browse the repository at this point in the history
  • Loading branch information
GeunH committed Dec 6, 2023
1 parent 5a84f42 commit 0b86390
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
20 changes: 12 additions & 8 deletions be/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,24 @@ export class AuthService {
}
}

async createTokens(id: number) {
const payload = { id: id };
const accessToken = this.jwtService.sign(payload);
const refreshToken = this.jwtService.sign(payload, {
secret: "nibobnebob",
expiresIn: "7d",
});
await this.authRepository.upsert({ id: id, accessToken: accessToken, refreshToken: refreshToken }, ["id"]);
return { accessToken, refreshToken };
}

async signin(loginRequestUser: any) {
const user = await this.userRepository.findOneBy({
email: loginRequestUser.email,
});

if (user) {
const payload = { id: user.id };
const accessToken = this.jwtService.sign(payload);
const refreshToken = this.jwtService.sign(payload, {
secret: "nibobnebob",
expiresIn: "7d",
});
await this.authRepository.upsert({ id: user.id, accessToken: accessToken, refreshToken: refreshToken }, ["id"]);
return { accessToken, refreshToken };
return await this.createTokens(user.id);
} else {
throw new NotFoundException(
"사용자가 등록되어 있지 않습니다. 회원가입을 진행해주세요"
Expand Down
4 changes: 2 additions & 2 deletions be/src/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class UserRepository extends Repository<User> {
super(User, dataSource.createEntityManager());
}
async createUser(userentity: User) {
await this.save(userentity);
return await this.save(userentity);
}
async getNickNameAvailability(nickName: UserInfoDto["nickName"]) {
const user = await this.findOne({
Expand Down Expand Up @@ -93,7 +93,7 @@ export class UserRepository extends Repository<User> {
.where("user.id NOT IN (:...idList)", { idList })
.andWhere("user.region = :yourRegion", { yourRegion: curUser.region })
.groupBy("user.id")
.orderBy("\"commonRestaurant\"","DESC")
.orderBy("\"commonRestaurant\"", "DESC")
.limit(10)
.getRawMany();

Expand Down
19 changes: 11 additions & 8 deletions be/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { User } from "./entities/user.entity";
import { RestaurantInfoEntity } from "src/restaurant/entities/restaurant.entity";
import { AuthService } from "src/auth/auth.service";
import { SortInfoDto } from "src/utils/sortInfo.dto";
import { JwtService } from "@nestjs/jwt";

@Injectable()
export class UserService {
Expand All @@ -29,7 +30,7 @@ export class UserService {
private reviewRepository: ReviewRepository,
private userWishRestaurantListRepository: UserWishRestaurantListRepository,
private awsService: AwsService,
private authService: AuthService
private authService: AuthService,
) { }
async signup(@UploadedFile() file: Express.Multer.File, userInfoDto: UserInfoDto) {
if (userInfoDto.password) userInfoDto.password = await hashPassword(userInfoDto.password);
Expand All @@ -49,16 +50,18 @@ export class UserService {

try {
const newUser = this.usersRepository.create(user);
await this.usersRepository.createUser(newUser);
const result = await this.usersRepository.createUser(newUser);
if (file) {
await this.awsService.uploadToS3(profileImage, file.buffer);
}
return;
return this.authService.createTokens(result.id);
} catch (error) {
if (error.code === "23505") {
throw new ConflictException("Duplicated Value");
}
}


}
async getNickNameAvailability(nickName: UserInfoDto["nickName"]) {
return await this.usersRepository.getNickNameAvailability(nickName);
Expand Down Expand Up @@ -209,15 +212,15 @@ export class UserService {
);
const userIdValues = userIds.map((user) => user.followingUserId);
userIdValues.push(tokenInfo.id);
const result = await this.usersRepository.getRecommendUserListInfo( userIdValues ,tokenInfo.id);
const result = await this.usersRepository.getRecommendUserListInfo(userIdValues, tokenInfo.id);

function getRandomInts(min: number, max: number, count: number): number[] {
const ints = new Set<number>();
while (ints.size < count) {
const rand = Math.floor(Math.random() * (max - min + 1)) + min;
ints.add(rand);
const rand = Math.floor(Math.random() * (max - min + 1)) + min;
ints.add(rand);
}
return [...ints].sort((a,b) => a - b);
return [...ints].sort((a, b) => a - b);
}

const randomIndexes = getRandomInts(0, result.length - 1, 2);
Expand Down Expand Up @@ -387,7 +390,7 @@ export class UserService {
return await this.usersRepository.deleteUserAccount(tokenInfo.id);
}
async updateMypageUserInfo(file: Express.Multer.File, tokenInfo: TokenInfo, userInfoDto: UserInfoDto) {
userInfoDto.password = await hashPassword(userInfoDto.password);
if (userInfoDto.password) userInfoDto.password = await hashPassword(userInfoDto.password);
let profileImage;
if (file) {
const uuid = v4();
Expand Down

0 comments on commit 0b86390

Please sign in to comment.