Feature/ds 20: Auth API#16
Conversation
| login = async req => { | ||
| login = async (req, res) => { | ||
| const data = await this.service.login(LoginDto(req.body)); | ||
| res.cookie('access_token', data.access_token, { ...cookieOptions, maxAge: ACCESS_TOKEN_MAX_AGE }); |
There was a problem hiding this comment.
chổ res cookie access refresh lặp lại nhiều lần nè , em thêm hàm để xử lý phần res này
| }; | ||
|
|
||
| logout = async (req, res) => { | ||
| const data = await this.service.logout(LogoutDto(req.body)); |
There was a problem hiding this comment.
Logout thì ko gửi req.body á em . Logout dựa vào refresh token trong cookie hoặc user context
| sameSite: 'lax', | ||
| }; | ||
|
|
||
| const ACCESS_TOKEN_MAX_AGE = 24 * 60 * 60 * 1000; // 1 day |
There was a problem hiding this comment.
Mấy const này đưa vào file config nha , đừng để ở controller
|
|
||
| ApiDocument.addModel('LogoutDto', | ||
| { | ||
| refresh_token: SwaggerDocument.ApiProperty({ type: 'string' }), |
There was a problem hiding this comment.
hình như em dùng cookie-based rồi thì logout dto này ko cần nữa
|
|
||
| refresh = async req => { | ||
| refresh = async (req, res) => { | ||
| const data = await this.service.refresh(RefreshDto(req.body)); |
There was a problem hiding this comment.
em đang sài cookie thì lấy từ req.cookies.refresh_token nha
| } | ||
|
|
||
| return { | ||
| message: 'Dang xuat thanh cong.', |
| // 4. Tao cap token moi (access + refresh) | ||
| // 5. Tra ve { access_token, refresh_token } | ||
| async refresh(refreshDto) { | ||
| const tokenRecord = await this.refreshTokenRepository.findValidToken(refreshDto.refresh_token); |
There was a problem hiding this comment.
refreshDto.refresh_token nên check rỗng trước
| } | ||
|
|
||
| const accessToken = this.jwtService.sign(JwtPayload({ id: user.id, roles: [user.role] })); | ||
| const refreshToken = await this.#createRefreshToken(user.id); |
There was a problem hiding this comment.
trong #createRefreshToken nên lưu nha hash token tránh lưu raw
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
No description provided.