-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
36 lines (33 loc) · 1.56 KB
/
auth.js
File metadata and controls
36 lines (33 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//contains all the code related to the authentication
//first check myNewServerfile and watch video at 38:20
const Person = require("./Modals/Person");
//learn about passport in 33_Authentication.txt
const passport=require("passport")
//using passport-local strategy, means Authenticating users via username and passport
const LocalStrategy=require("passport-local").Strategy;
//Authentication code:
//always written like this: username, password, and done: the names could be different
passport.use(new LocalStrategy(async(userName,password,done)=>{
//authentication logic
try{
// console.log("Received Credentials:",userName, password)
//finding do we have any user/person with this username in person table
const user=await Person.findOne({username:userName})
if(!user){
//done is a callback function provided by passport.js it gives the signal of the completion of an authentication attempt (either success or fail)
//done() takes three parameters: error , user, info
return done(null, false, {message:"Incorrect Username!"})
}
//const isPasswordMatch=user.password === password?true:false //used before hashing password
//used after hasing password : see like 34_Password_Protection and Person.js
const isPasswordMatch=await user.comparePassword(password) //this comparePassword function is in Person.js file
if(isPasswordMatch){
return done(null,user)
}else{
return done(null,false,{message:"Incorrect Password!"})
}
}catch(error){
return done(error)
}
}))
module.exports=passport; //used inside myNewServer