Skip to content

Commit 4dbc8cf

Browse files
committed
fix login issue
1 parent 829efc7 commit 4dbc8cf

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

src/middleware/auth.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const auth = async (req, res, next)=>{
55
// console.log("auth middleware")
66
// next()
77
try{
8-
console.log('inside auth')
8+
//console.log('inside auth')
99
const token = req.header('Authorization').replace('Bearer ', '')
10-
console.log(token)
11-
console.log(process.env.JWT_SECRET)
10+
//console.log(token)
11+
//console.log(process.env.JWT_SECRET)
1212
const decoded = jwt.verify(token, process.env.JWT_SECRET)
13-
// console.log(decoded)
13+
//console.log(decoded.User)
1414
// console.log('test')
1515
const user = await User.findById({_id: decoded._id, 'tokens.token': token})
16-
16+
//console.log(user)
1717
// const tokenExp = decoded.exp
1818
// const date=Math.round(new Date().getTime()/1000 )
1919
// const currentSystemDate = new Date().toLocaleString()

src/models/users.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ userSchema.statics.findByCredentials= async(email,password)=>{
7171
//console.log(email)
7272
//console.log(password)
7373
const isMatch = await bcrypt.compare(password,user.password)
74+
//console.log(isMatch)
7475
if(!isMatch){
75-
//throw new Error('Unable to login')
76+
throw new Error('Unable to login')
7677
}
7778
return user
7879
}

src/routers/users.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ router.post('/users', async (req,res)=>{
2121
})
2222

2323
router.get('/users',auth ,async (req,res)=>{
24-
console.log("inside get")
24+
//console.log("inside get")
2525
// try {
2626
// const users= await User.find({})
2727
// if (users.length === 0){
@@ -49,10 +49,7 @@ router.post('/users/login',async(req,res)=>{
4949
res.status(400).send()
5050
}
5151
})
52-
// mail.sendWelcomeEmail
53-
// mail.userDeletionEmail
54-
// mail.userUpdateEmail
55-
// mail.sendDeleteTokenEmail
52+
5653
router.post('/users/logout',auth ,async (req,res)=>{
5754
// console.log(req.user.tokens)
5855
// req.user.tokens.filter((token)=>{

tests/user.test.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
11
const request = require('supertest')
22
const app = require('../src/app')
3+
const User = require('../src/models/users')
34

4-
test ('Should signup a new User',async ()=>{
5-
6-
})
5+
6+
const userOne = {
7+
name:"Ken",
8+
9+
password:"123ssddff"
10+
}
11+
12+
beforeEach(async()=>{
13+
await User.deleteMany()
14+
await new User(userOne).save()
15+
})
16+
17+
18+
19+
20+
test('Should Login the new User', async () => {
21+
await request(app).post('/users/login').send({
22+
email:userOne.email,
23+
password:userOne.password
24+
}).expect(200)
25+
})
26+
27+
28+
29+
test('Should Not Login', async () => {
30+
await request(app).post('/users/login').send({
31+
email:userOne.email,
32+
password:'thisisnotmypassword'
33+
}).expect(400)
34+
})
35+
36+
test ('Should signup a new User', async () => {
37+
await request(app).post('/users').send({
38+
name:'Faizan',
39+
40+
password:'Mypass777!'
41+
}).expect(201)
42+
})
43+
44+
45+
// afterEach(()=>{
46+
// console.log('after each')
47+
// })

0 commit comments

Comments
 (0)