Skip to content

Commit 513be37

Browse files
committed
Changes
0 parents  commit 513be37

13 files changed

Lines changed: 305 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

app.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require('express');
2+
const mongoose = require('mongoose');
3+
const router = express.Router();
4+
const app = express();
5+
const expressEjsLayout = require('express-ejs-layouts')
6+
const flash = require('connect-flash');
7+
const session = require('express-session');
8+
//mongoose
9+
mongoose.connect('mongodb://localhost/test',{useNewUrlParser: true, useUnifiedTopology : true})
10+
.then(() => console.log('connected,,'))
11+
.catch((err)=> console.log(err));
12+
13+
//EJS
14+
app.set('view engine','ejs');
15+
app.use(expressEjsLayout);
16+
//BodyParser
17+
app.use(express.urlencoded({extended : false}));
18+
//express session
19+
app.use(session({
20+
secret : 'secret',
21+
resave : true,
22+
saveUninitialized : true
23+
}));
24+
25+
app.use(flash());
26+
app.use((req,res,next)=> {
27+
res.locals.success_msg = req.flash('success_msg');
28+
res.locals.error_msg = req.flash('error_msg');
29+
res.locals.error = req.flash('error');
30+
next();
31+
})
32+
33+
//Routes
34+
app.use('/',require('./routes/index'));
35+
app.use('/users',require('./routes/users'));
36+
37+
app.listen(3000);

config/auth.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const User = require('../models/user');
2+
const LocalStrategy = require('passport-local').Strategy;
3+
const bcrypt = require('bcrypt');
4+
5+
module.exports = function(passport){
6+
passport.use(
7+
new LocalStrategy({usernameField: 'email'},(email,password,done)=>{
8+
//match user
9+
User.findOne({email:email})
10+
.then((user)=>{
11+
if(!user){
12+
return done(null,false,{message:'email not registered'});
13+
}
14+
//math passwords
15+
bcrypt.compare(password,user.password,(err,isMatch)=>{
16+
if(err) throw err;
17+
if(isMatch){
18+
return done(null,user);
19+
} else{
20+
return done(null,false,{message: 'password incorrect'});
21+
}
22+
})
23+
})
24+
.catch((err)=>{console.log(err)})
25+
})
26+
)
27+
passport.serializeUser(function(user,done) {
28+
done(null,user.id);
29+
})
30+
passport.deserializeUser(function(id,done){
31+
User.findById(id,function(err,user){
32+
done(err,user);
33+
})
34+
})
35+
}

models/user.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const mongoose = require('mongoose');
2+
const UserSchema = new mongoose.Schema({
3+
name :{
4+
type : String,
5+
required : true
6+
} ,
7+
email :{
8+
type : String,
9+
required : true
10+
} ,
11+
password :{
12+
type : String,
13+
required : true
14+
} ,
15+
date :{
16+
type : Date,
17+
default : Date.now
18+
}
19+
});
20+
const User= mongoose.model('User',UserSchema);
21+
22+
module.exports = User;

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "passport-learning",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"nodemon": "^2.0.4"
14+
},
15+
"dependencies": {
16+
"bcrypt": "^5.0.0",
17+
"connect-flash": "^0.1.1",
18+
"ejs": "^3.1.3",
19+
"express": "^4.17.1",
20+
"express-ejs-layouts": "^2.5.0",
21+
"express-session": "^1.17.1",
22+
"mongoose": "^5.9.21"
23+
}
24+
}

routes/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
//login page
4+
router.get('/', (req,res)=>{
5+
res.render('welcome');
6+
})
7+
//register page
8+
router.get('/register', (req,res)=>{
9+
res.render('register');
10+
})
11+
12+
module.exports = router;

routes/users.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const User = require("../models/user");
4+
const bcrypt = require('bcrypt');
5+
//login handle
6+
router.get('/login',(req,res)=>{
7+
res.render('login');
8+
})
9+
router.get('/register',(req,res)=>{
10+
res.render('register')
11+
})
12+
//Register handle
13+
router.post('/login',(req,res)=>{
14+
})
15+
//register post handle
16+
router.post('/register',(req,res)=>{
17+
const {name,email, password, password2} = req.body;
18+
let errors = [];
19+
console.log(' Name ' + name+ ' email :' + email+ ' pass:' + password);
20+
if(!name || !email || !password || !password2) {
21+
errors.push({msg : "Please fill in all fields"})
22+
}
23+
//check if match
24+
if(password !== password2) {
25+
errors.push({msg : "passwords dont match"});
26+
}
27+
28+
//check if password is more than 6 characters
29+
if(password.length < 6 ) {
30+
errors.push({msg : 'password atleast 6 characters'})
31+
}
32+
if(errors.length > 0 ) {
33+
res.render('register', {
34+
errors : errors,
35+
name : name,
36+
email : email,
37+
password : password,
38+
password2 : password2})
39+
} else {
40+
//validation passed
41+
User.findOne({email : email}).exec((err,user)=>{
42+
console.log(user);
43+
if(user) {
44+
errors.push({msg: 'email already registered'});
45+
res.render('register',{errors,name,email,password,password2})
46+
} else {
47+
const newUser = new User({
48+
name : name,
49+
email : email,
50+
password : password
51+
});
52+
53+
//hash password
54+
bcrypt.genSalt(10,(err,salt)=>
55+
bcrypt.hash(newUser.password,salt,
56+
(err,hash)=> {
57+
if(err) throw err;
58+
//save pass to hash
59+
newUser.password = hash;
60+
//save user
61+
newUser.save()
62+
.then((value)=>{
63+
console.log(value)
64+
req.flash('success_msg','You have now registered!');
65+
res.redirect('/users/login');
66+
})
67+
.catch(value=> console.log(value));
68+
69+
}));
70+
}
71+
})
72+
}
73+
})
74+
//logout
75+
router.get('/logout',(req,res)=>{
76+
})
77+
module.exports = router;

views/dashboard.ejs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Dashboard</h1>
2+
<p> Welcome User</p>
3+
<a href="/users/logout">Logout</a>

views/layout.ejs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Node.Js and Passport App</title>
4+
</head>
5+
<body>
6+
<%- body %>
7+
</body>
8+
</html>

views/login.ejs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<h1> Login </h1>
2+
<%- include ('./partials/messages') %>
3+
<form action="/users/login" method="POST">
4+
<div>
5+
<label for="email">Email</label>
6+
<input
7+
type="email"
8+
name="email"
9+
placeholder="Enter Email"
10+
/>
11+
</div>
12+
<div>
13+
<label for="password">Password</label>
14+
<input
15+
type="password"
16+
name="password"
17+
placeholder="Enter Password"
18+
/>
19+
20+
</div>
21+
<button type="submit" class="btn btn-primary btn-block">Login</button>
22+
23+
</form>

0 commit comments

Comments
 (0)