Commit 765e2914 authored by “Yevgeniy's avatar “Yevgeniy

#32 fixed login and register

parent 0e93993f
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
"@types/cors": "^2.8.12", "@types/cors": "^2.8.12",
"@types/express": "^4.17.14", "@types/express": "^4.17.14",
"bcrypt": "^5.1.0", "bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2", "class-validator": "^0.13.2",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
...@@ -2491,6 +2492,11 @@ ...@@ -2491,6 +2492,11 @@
"node": ">=10" "node": ">=10"
} }
}, },
"node_modules/class-transformer": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw=="
},
"node_modules/class-validator": { "node_modules/class-validator": {
"version": "0.13.2", "version": "0.13.2",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz", "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz",
...@@ -7724,6 +7730,11 @@ ...@@ -7724,6 +7730,11 @@
"resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz", "resolved": "https://registry.npmjs.org/chownr/-/chownr-2.0.0.tgz",
"integrity": "sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ==" "integrity": "sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ=="
}, },
"class-transformer": {
"version": "0.5.1",
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw=="
},
"class-validator": { "class-validator": {
"version": "0.13.2", "version": "0.13.2",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz", "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.13.2.tgz",
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
"@types/cors": "^2.8.12", "@types/cors": "^2.8.12",
"@types/express": "^4.17.14", "@types/express": "^4.17.14",
"bcrypt": "^5.1.0", "bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2", "class-validator": "^0.13.2",
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.18.2", "express": "^4.18.2",
......
...@@ -7,11 +7,11 @@ import { ...@@ -7,11 +7,11 @@ import {
BaseEntity, BaseEntity,
ManyToMany, ManyToMany,
OneToMany, OneToMany,
JoinTable JoinTable,
} from 'typeorm'; } from 'typeorm';
import {IsEmail import {IsEmail
} from "class-validator"; } from "class-validator";
import { Exclude, instanceToPlain } from "class-transformer";
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
import {nanoid} from 'nanoid'; import {nanoid} from 'nanoid';
import {Task} from './Task'; import {Task} from './Task';
...@@ -72,7 +72,8 @@ export class User extends BaseEntity implements IUser { ...@@ -72,7 +72,8 @@ export class User extends BaseEntity implements IUser {
}) })
role!: userRoleType role!: userRoleType
@Column({ type: 'varchar', nullable: false, select:false }) @Column({ type: 'varchar', nullable: false, select:true })
@Exclude({ toPlainOnly: true })
password!: string password!: string
...@@ -106,20 +107,16 @@ export class User extends BaseEntity implements IUser { ...@@ -106,20 +107,16 @@ export class User extends BaseEntity implements IUser {
public async checkPassword( public async checkPassword(
candidatePassword: string, candidatePassword: string,
):Promise<boolean> { ):Promise<boolean> {
console.log("Checking password", candidatePassword,'this.password', this.password)
return await bcrypt.compare(candidatePassword, this.password); return await bcrypt.compare(candidatePassword, this.password);
} }
toJSON() {
return instanceToPlain(this);
} }
}
......
...@@ -41,9 +41,9 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object> ...@@ -41,9 +41,9 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
.select("user") .select("user")
.from(User, "user") .from(User, "user")
.where("user.email = :email", { email: email }) .where("user.email = :email", { email: email })
.addSelect('password')
.getOne() .getOne()
if(!user) return res.status(404).send({Message:'user not found'}) if(!user) return res.status(404).send({Message:'user not found'})
const isMatch:boolean = await user.checkPassword(password); const isMatch:boolean = await user.checkPassword(password);
if (!isMatch) return res.status(400).send({ if (!isMatch) return res.status(400).send({
error: "Wrong Password" error: "Wrong Password"
...@@ -67,7 +67,6 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj ...@@ -67,7 +67,6 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj
token: token token: token
}) })
if(!user) return res.send({successMsg}); if(!user) return res.send({successMsg});
console.log('token: ' + token)
user.token = nanoid(); user.token = nanoid();
await user.save(); await user.save();
......
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