Commit 2e137c79 authored by Pavel Mishakov's avatar Pavel Mishakov

lesson 91 done BACK

parent ed053dd4
......@@ -19,8 +19,7 @@ export class UserController {
this.router.post('/', this.createUser)
this.router.post('/login', this.login)
this.router.get('/token', auth, this.checkToken)
this.router.put('/', auth, this.editUser)
}
public getRouter = (): Router => {
return this.router
......@@ -30,6 +29,13 @@ export class UserController {
res.status(200).send(response)
}
private editUser = async (expressReq: Request, res: Response): Promise<void> => {
const req = expressReq as IRequestWithTokenData
const user = req.dataFromToken as IUserGetDto
const response: IResponse<IUserGetDto | undefined> = await this.service.editUser(req.body, user._id)
res.status(200).send(response)
}
public login = async (req: Request, res: Response): Promise<void> => {
const response: IResponse<IUserGetDto | undefined> = await this.service.login(req.body)
res.status(200).send(response)
......
import jwt from 'jsonwebtoken'
export const generateJWT = (payload: {[key: string]: string | number | boolean}) => {
return jwt.sign(payload, process.env.SECRET_KEY || '', {expiresIn: '2m'})
return jwt.sign(payload, process.env.SECRET_KEY || '', {expiresIn: '2h'})
}
\ No newline at end of file
......@@ -5,4 +5,5 @@ export default interface IUser extends Document {
username: string
password: string
active: boolean
checkPassword: (pass: string) => boolean
}
\ No newline at end of file
......@@ -281,8 +281,7 @@ export class Mongo implements IDataBase {
if (!user) {
throw new Error('Not found')
}
//@ts-ignore
const isMatch: boolean = await user.checkPassword(userDto.password)
const isMatch: boolean = user.checkPassword(userDto.password)
if (!isMatch) {
throw new Error('Wrong password')
......@@ -323,6 +322,32 @@ export class Mongo implements IDataBase {
}
}
}
public editUser = async (userDto: IUserCreateDto, id: string): Promise<IResponse<IUserGetDto | undefined>> => {
try {
const update: {username: string, password?: string} = {
username: userDto.username
}
if (userDto.password) {
update.password = userDto.password
}
const user: IUserGetDto | null = await User.findOneAndUpdate({_id: id}, update, {
new: true
})
return {
status: EStatuses.OK,
result: user || undefined,
message: 'User was updated'
}
} catch(err: unknown) {
return {
status: EStatuses.NOT_OK,
result: undefined,
message: '[ERROR] Cannot update user'
}
}
}
}
export const mongo = new Mongo()
\ No newline at end of file
......@@ -15,6 +15,10 @@ export class UserService {
return await this.repository.createUser(userDto)
}
public editUser = async (userDto: IUserCreateDto, id: string): Promise<IResponse<IUserGetDto | undefined>> => {
return await this.repository.editUser(userDto, id)
}
public login = async (userDto: IUserCreateDto): Promise<IResponse<IUserGetDto | undefined>> => {
return await this.repository.login(userDto)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment