Skip to content

Instantly share code, notes, and snippets.

@tiagolpadua
Last active March 18, 2021 22:24
Show Gist options
  • Save tiagolpadua/ea6013dae6f8bc567ebe70a5feae399f to your computer and use it in GitHub Desktop.
Save tiagolpadua/ea6013dae6f8bc567ebe70a5feae399f to your computer and use it in GitHub Desktop.
cmail-back-master\src\controllers\LoginController.js
import HttpStatus from 'http-status'
import * as requestUtils from '../infra/utils/requestUtils'
import * as LoginInputDto from './dto/input/LoginInputDto'
import * as tokenManager from '../infra/tokenManager'
export class LoginController {
constructor(usersService) {
this.usersService = usersService
}
signIn = (req, res) => {
if (req.validations.hasErrors()) {
requestUtils.errorResponse(res, req.validations.errors)
return;
}
const userLoginInfo = LoginInputDto.extractLoginInfo(req.body)
this.usersService.login(userLoginInfo)
.then((loginResponse) => {
requestUtils.defaultResponse(res, loginResponse, HttpStatus.CREATED)
})
.catch((err) => {
requestUtils.errorResponse(res, err)
})
}
// Método novo
validate = (req, res) => {
const senderToken = req.headers.authorization;
tokenManager.verify(senderToken)
.then(() => requestUtils.defaultResponse(res))
.catch(() => requestUtils.defaultResponse(res, null, 401));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment