-
Notifications
You must be signed in to change notification settings - Fork 2
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
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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,66 @@ | ||
import { AdminService } from '@src/service/AdminService'; | ||
import { RequestHandler } from 'express'; | ||
import { LoginResponse, ResponseDTO } from './definitions/response'; | ||
import StatusCode from '@src/utils/statusCode'; | ||
import { MSG_LOGIN_SUCCESS } from '@src/utils/strings'; | ||
|
||
let Admin: AdminService = new AdminService(); | ||
|
||
/** | ||
* @swagger | ||
* /admin/login: | ||
* post: | ||
* tags: | ||
* - admin | ||
* description: 로그인 <br/> 반환 되는 정보 [유저 정보 + Token + refresh Token] <br/> 발행된 로그인 토큰은 헤더[x-access-token="Bearer " + Token]에 넣어주세요. | ||
* operationId: loginAdminUser | ||
* produces: | ||
* - application/json | ||
* parameters: | ||
* - name: body | ||
* in: body | ||
* schema: | ||
* type: object | ||
* properties: | ||
* email: | ||
* type: string | ||
* example: hee.youn@samsung.com | ||
* password: | ||
* type: string | ||
* example: test | ||
* responses: | ||
* 200: | ||
* description: success | ||
* schema: | ||
* type: object | ||
* properties: | ||
* message: | ||
* type: string | ||
* example: 로그인 성공 | ||
* data: | ||
* allOf: | ||
* - $ref: '#/definitions/LoginResponse' | ||
* 400: | ||
* description: Invalid username/password supplied | ||
* 401: | ||
* description: 비밀번호가 잘못된 경우 / 아이디가 존재하지 않는 경우 | ||
* schema: | ||
* type: object | ||
* example: | ||
* message: 비밀번호가 잘못되었습니다 / 해당 조건에 일치하는 데이터가 없습니다. | ||
* x-swagger-router-controller: Admin | ||
* */ | ||
export const loginAdminUser: RequestHandler = async (req, res, next) => { | ||
const email: string = req.body.email; | ||
const password: string = req.body.password; | ||
|
||
try { | ||
const result = await Admin.loginAdminUser(email, password); | ||
const response = LoginResponse.createByJson(result); | ||
res.status(StatusCode.OK).json( | ||
new ResponseDTO<LoginResponse>(MSG_LOGIN_SUCCESS, response) | ||
); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; |
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,51 @@ | ||
import JwtController from '@libs/JwtController'; | ||
import { decrypt as _decrypt, encrypt as _encrypt } from '@libs/crypto'; | ||
import { LoginInfoDTO, TokenPayloadDTO } from '@src/data/dto'; | ||
import { | ||
UnAuthorizedError, | ||
WrongPasswordError, | ||
} from '@src/utils/errors/errors'; | ||
|
||
// TODO: 어드민 계정용 별도 테이블 만들기 | ||
const defaultAdmin = { | ||
email: process.env.ADMIN_EMAIL || 'admin@email.com', | ||
password: _encrypt(process.env.ADMIN_PASSWORD || 'password'), | ||
}; | ||
|
||
export class AdminService { | ||
crypto: any; | ||
jwt: any; | ||
constructor(crypto?: any, jwt?: any) { | ||
this.crypto = crypto || { encrypt: _encrypt, decrypt: _decrypt }; | ||
this.jwt = jwt || { | ||
create: JwtController.create, | ||
publish: JwtController.publish, | ||
verify: JwtController.verify, | ||
}; | ||
} | ||
|
||
async loginAdminUser( | ||
email: string, | ||
password: string | ||
): Promise<LoginInfoDTO> { | ||
// TODO: email에 해당하는 어드민계정 테이블에서 레코드 조회 | ||
if (email !== defaultAdmin.email) { | ||
throw new UnAuthorizedError(); | ||
} | ||
if ( | ||
this.crypto.decrypt(defaultAdmin.password) !== | ||
this.crypto.decrypt(password) | ||
) { | ||
throw new WrongPasswordError(); | ||
} | ||
const payload: any = TokenPayloadDTO.createByJson(defaultAdmin); | ||
|
||
const { token, refreshToken } = this.jwt.publish(payload); | ||
return LoginInfoDTO.createByJson( | ||
Object.assign({}, defaultAdmin, { | ||
token, | ||
refreshToken, | ||
}) | ||
); | ||
} | ||
} |