Created
February 3, 2019 01:08
-
-
Save andregardi/8c69fc50ec7309178a549da33204ab92 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Request, Response } from "express"; | |
| import * as jwt from "jsonwebtoken"; | |
| import { getRepository } from "typeorm"; | |
| import { validate } from "class-validator"; | |
| import { User } from "../entity/User"; | |
| import config from "../config/config"; | |
| class AuthController { | |
| static login = async (req: Request, res: Response) => { | |
| //Check if username and password are set | |
| let { username, password } = req.body; | |
| if (!(username && password)) { | |
| res.status(400).send(); | |
| } | |
| //Get user from database | |
| const userRepository = getRepository(User); | |
| let user: User; | |
| try { | |
| user = await userRepository.findOneOrFail({ where: { username } }); | |
| } catch (error) { | |
| res.status(401).send(); | |
| } | |
| //Check if encrypted password match | |
| if (!user.checkIfUnencryptedPasswordIsValid(password)) { | |
| res.status(401).send(); | |
| return; | |
| } | |
| //Sing JWT, valid for 1 hour | |
| const token = jwt.sign( | |
| { userId: user.id, username: user.username }, | |
| config.jwtSecret, | |
| { expiresIn: "1h" } | |
| ); | |
| //Send the jwt in the response | |
| res.send(token); | |
| }; | |
| static changePassword = async (req: Request, res: Response) => { | |
| //Get ID from JWT | |
| const id = res.locals.jwtPayload.userId; | |
| //Get parameters from the body | |
| const { oldPassword, newPassword } = req.body; | |
| if (!(oldPassword && newPassword)) { | |
| res.status(400).send(); | |
| } | |
| //Get user from the database | |
| const userRepository = getRepository(User); | |
| let user: User; | |
| try { | |
| user = await userRepository.findOneOrFail(id); | |
| } catch (id) { | |
| res.status(401).send(); | |
| } | |
| //Check if old password matchs | |
| if (!user.checkIfUnencryptedPasswordIsValid(oldPassword)) { | |
| res.status(401).send(); | |
| return; | |
| } | |
| //Validate de model (password lenght) | |
| user.password = newPassword; | |
| const errors = await validate(user); | |
| if (errors.length > 0) { | |
| res.status(400).send(errors); | |
| return; | |
| } | |
| //Hash the new password and save | |
| user.hashPassword(); | |
| userRepository.save(user); | |
| res.status(204).send(); | |
| }; | |
| } | |
| export default AuthController; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment