-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
741 additions
and
36 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { AuthService } from './auth.service'; | ||
import { RolesGuard } from './passport/role.guard'; | ||
import { Roles } from '../users/entities/authorities'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService) { | ||
} | ||
|
||
@UseGuards(AuthGuard('jwt'), RolesGuard) | ||
@Roles(['ROLE_USER', 'ROLE_CLEANER', 'ROLE_ADMIN']) | ||
@Get('profile') | ||
getProfile(@Req() req) { | ||
console.log(req); | ||
return req.user; | ||
} | ||
|
||
@Post('signIn') | ||
async login(@Body() req) { | ||
return this.authService.login(req); // 1 | ||
// return req.user; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JwtStrategy } from './passport/jwt.strategy'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { UsersModule } from '../users/users.module'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { AuthRepository } from './auth.repository'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { User, UserSchema } from '../users/entities/user.entity'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), | ||
UsersModule, | ||
PassportModule, | ||
JwtModule.registerAsync({ | ||
imports: [ConfigModule], | ||
inject: [ConfigService], | ||
useFactory: async (configService: ConfigService) => ({ | ||
secret: configService.get('JWT_SECRET_KEY'), | ||
}), | ||
}), | ||
], | ||
providers: [JwtStrategy, AuthService, AuthRepository], | ||
controllers: [AuthController], | ||
}) | ||
export class AuthModule {} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { User, UserDocument } from '../users/entities/user.entity'; | ||
|
||
@Injectable() | ||
export class AuthRepository { | ||
constructor( | ||
@InjectModel(User.name) private usersModel: Model<UserDocument>, | ||
) {} | ||
|
||
async findByLoginIdAndPassword(user: { | ||
loginId: string; | ||
password: string; | ||
}): Promise<UserDocument[]> { | ||
try { | ||
const { loginId, password } = user; | ||
return await this.usersModel.find({ | ||
loginId, | ||
password, | ||
}); | ||
} catch (err) { | ||
console.log('error...'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { AuthRepository } from './auth.repository'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private readonly jwtService: JwtService, | ||
private readonly authRepository: AuthRepository, | ||
) {} | ||
|
||
async login(user: { loginId: string; password: string }) { | ||
console.log(user); | ||
const userAccounts = this.authRepository.findByLoginIdAndPassword(user); | ||
return userAccounts | ||
.then((res) => { | ||
if (res.length === 0) { | ||
throw `계정 정보가 없습니다. 입력하신 loginId: ${user.loginId}, password: ${user.password}`; | ||
} | ||
const userByFindFirst = res[0]; | ||
const payload = { | ||
username: userByFindFirst.username, | ||
email: userByFindFirst.email, | ||
role: userByFindFirst.role, | ||
}; | ||
return { | ||
userByFindFirst, | ||
access_token: this.jwtService.sign(payload), | ||
}; | ||
}) | ||
.catch((err) => { | ||
throw `Error 로그인 in service:, ${err}`; | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(private readonly configService: ConfigService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: configService.get('JWT_SECRET_KEY'), | ||
}); | ||
} | ||
|
||
async validate(payload: any) { | ||
return { | ||
email: payload.email, | ||
username: payload.username, | ||
role: payload.role, | ||
}; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Observable } from 'rxjs'; | ||
import { User } from '../../users/entities/user.entity'; | ||
|
||
@Injectable() | ||
export class RolesGuard implements CanActivate { | ||
constructor(private readonly reflector: Reflector) {} | ||
|
||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
const roles = this.reflector.get<string[]>('roles', context.getHandler()); | ||
|
||
if (!roles) { | ||
// roles가 아니면 true를 리턴하고 진행한다. | ||
return true; | ||
} | ||
|
||
const request = context.switchToHttp().getRequest(); | ||
// console.log('request + ', request); | ||
const user = request.user as User; | ||
|
||
return user && user.role && roles.includes(user.role); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { IsEmail, IsEnum, IsString } from 'class-validator'; | ||
import { Authorities, Role } from '../entities/authorities'; | ||
|
||
export class CreateUserDto { | ||
@IsEmail() | ||
readonly email: string; | ||
|
||
@IsString() | ||
readonly loginId: string; | ||
|
||
@IsString() | ||
readonly password: string; | ||
|
||
@IsString() | ||
readonly username: string; | ||
|
||
@IsEnum(Authorities) | ||
readonly role: Role; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export type Role = 'ROLE_USER' | 'ROLE_ADMIN' | 'ROLE_CLEANER'; | ||
|
||
export const Authorities: Role[] = ['ROLE_USER', 'ROLE_ADMIN', 'ROLE_CLEANER']; | ||
|
||
export const Roles = (roles: Role[]): any => SetMetadata('roles', roles); |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import mongoose from 'mongoose'; | ||
import { Role } from './authorities'; | ||
|
||
export type UserDocument = User & Document; | ||
|
||
@Schema({ timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' } }) | ||
export class User { | ||
@Prop() | ||
email: string; | ||
|
||
@Prop() | ||
loginId: string; | ||
|
||
@Prop() | ||
password: string; | ||
|
||
@Prop() | ||
username: string; | ||
|
||
@Prop() | ||
role: Role; | ||
|
||
@Prop({ default: new Date(), type: mongoose.Schema.Types.Date }) | ||
createdAt: Date; | ||
|
||
@Prop({ default: new Date(), type: mongoose.Schema.Types.Date }) | ||
updatedAt: Date; | ||
} | ||
|
||
export const UserSchema = SchemaFactory.createForClass(User); |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { UsersController } from './users.controller'; | ||
|
||
describe('UsersController', () => { | ||
let controller: UsersController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [UsersController], | ||
}).compile(); | ||
|
||
controller = module.get<UsersController>(UsersController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { CreateUserDto } from './dtos/create-user.dto'; | ||
import { User } from './entities/user.entity'; | ||
import { UsersService } from './users.service'; | ||
|
||
@Controller('users') | ||
export class UsersController { | ||
constructor(private readonly userService: UsersService) {} | ||
|
||
@Post() | ||
async create(@Body() userData: CreateUserDto): Promise<User> { | ||
return await this.userService.create(userData); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UsersController } from './users.controller'; | ||
import { UsersService } from './users.service'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { UsersRepository } from './users.repository'; | ||
import { User, UserSchema } from './entities/user.entity'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), | ||
], | ||
controllers: [UsersController], | ||
providers: [UsersService, UsersRepository], | ||
}) | ||
export class UsersModule {} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { User, UserDocument } from './entities/user.entity'; | ||
|
||
@Injectable() | ||
export class UsersRepository { | ||
constructor( | ||
@InjectModel(User.name) private usersModel: Model<UserDocument>, | ||
) {} | ||
|
||
save(user: { | ||
email: string; | ||
loginId: string; | ||
password: string; | ||
username: string; | ||
}): Promise<UserDocument> { | ||
try { | ||
const result = new this.usersModel(user); | ||
return result.save(); | ||
} catch (err) { | ||
console.log('error...'); | ||
} | ||
} | ||
} |
Oops, something went wrong.