8
I’m having trouble understanding why it is not possible to apply delete user.password in the code below
Terminal return:
The operand of a 'delete' Operator must be optional.
My code:
import { Request, Response } from 'express';
import { getRepository } from 'typeorm';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import User from '../models/User';
class AuthController {
async authenticate(req: Request, res: Response){
const repository = getRepository(User);
const { email, password } = req.body;
const user = await repository.findOne({
where: { email }
});
if(!user) {
return res.sendStatus(401);
}
const isValidPassword = await bcrypt.compare(password, user.password);
if(!isValidPassword) {
return res.sendStatus(401);
}
const token = jwt.sign({ id: user.id}, 'secret', { expiresIn: '1d'});
delete user.password;
return res.json({
user,
token,
});
}
}
export default new AuthController();
Also don’t forget to import your View from users in your authentication controller of course. import usersView from '.. /views/Usersviews';
– Wesley Oliveira