Skip to content

Commit 96ea552

Browse files
authored
node
1 parent bab3595 commit 96ea552

File tree

16 files changed

+3588
-0
lines changed

16 files changed

+3588
-0
lines changed

Diff for: node_passport_login/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Node.js & Passport Login
2+
3+
This is a user login and registration app using Node.js, Express, Passport, Mongoose, EJS and some other packages.
4+
5+
### Version: 2.0.0
6+
7+
### Usage
8+
9+
```sh
10+
$ npm install
11+
```
12+
13+
```sh
14+
$ npm start
15+
# Or run with Nodemon
16+
$ npm run dev
17+
18+
# Visit http://localhost:5000
19+
```
20+
21+
### MongoDB
22+
23+
Open "config/keys.js" and add your MongoDB URI, local or Atlas

Diff for: node_passport_login/app.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const express = require('express');
2+
const expressLayouts = require('express-ejs-layouts');
3+
const mongoose = require('mongoose');
4+
const passport = require('passport');
5+
const flash = require('connect-flash');
6+
const session = require('express-session');
7+
8+
const app = express();
9+
10+
// Passport Config
11+
require('./config/passport')(passport);
12+
13+
// DB Config
14+
const db = require('./config/keys').mongoURI;
15+
16+
// Connect to MongoDB
17+
mongoose
18+
.connect(
19+
db,
20+
{ useNewUrlParser: true ,useUnifiedTopology: true}
21+
)
22+
.then(() => console.log('MongoDB Connected'))
23+
.catch(err => console.log(err));
24+
25+
// EJS
26+
app.use(expressLayouts);
27+
app.set('view engine', 'ejs');
28+
29+
// Express body parser
30+
app.use(express.urlencoded({ extended: true }));
31+
32+
// Express session
33+
app.use(
34+
session({
35+
secret: 'secret',
36+
resave: true,
37+
saveUninitialized: true
38+
})
39+
);
40+
41+
// Passport middleware
42+
app.use(passport.initialize());
43+
app.use(passport.session());
44+
45+
// Connect flash
46+
app.use(flash());
47+
48+
// Global variables
49+
app.use(function(req, res, next) {
50+
res.locals.success_msg = req.flash('success_msg');
51+
res.locals.error_msg = req.flash('error_msg');
52+
res.locals.error = req.flash('error');
53+
next();
54+
});
55+
56+
// Routes
57+
app.use('/', require('./routes/index.js'));
58+
app.use('/users', require('./routes/users.js'));
59+
60+
const PORT = process.env.PORT || 5000;
61+
62+
app.listen(PORT, console.log(`Server running on ${PORT}`));

Diff for: node_passport_login/config/auth.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
ensureAuthenticated: function(req, res, next) {
3+
if (req.isAuthenticated()) {
4+
return next();
5+
}
6+
req.flash('error_msg', 'Please log in to view that resource');
7+
res.redirect('/users/login');
8+
},
9+
forwardAuthenticated: function(req, res, next) {
10+
if (!req.isAuthenticated()) {
11+
return next();
12+
}
13+
res.redirect('/dashboard');
14+
}
15+
};

Diff for: node_passport_login/config/keys.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dbPassword = 'mongodb+srv://YOUR_USERNAME_HERE:'+ encodeURIComponent('YOUR_PASSWORD_HERE') + '@CLUSTER_NAME_HERE.mongodb.net/test?retryWrites=true';
2+
3+
module.exports = {
4+
mongoURI: dbPassword
5+
};

Diff for: node_passport_login/config/passport.js

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

Diff for: node_passport_login/models/User.js

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

0 commit comments

Comments
 (0)