Skip to content

Commit 961d8af

Browse files
Paul OracPaul Orac
authored andcommitted
first commit
0 parents  commit 961d8af

File tree

1,939 files changed

+243124
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,939 files changed

+243124
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Local Authentication with Passport and Mongo
2+
3+
- clone the repo
4+
- git install
5+
- npm index.js
6+
- navigate to localhost:300 and login

auth.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<body>
3+
<form action="/" method="post">
4+
<div>
5+
<label>Username:</label>
6+
<input type="text" name="username" />
7+
<br/>
8+
</div>
9+
<div>
10+
<label>Password:</label>
11+
<input type="password" name="password" />
12+
</div>
13+
<div>
14+
<input type="submit" value="Submit" />
15+
</div>
16+
</form>
17+
</body>
18+
</html>

index.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* EXPRESS SETUP */
2+
3+
const express = require('express');
4+
const app = express();
5+
6+
const bodyParser = require('body-parser');
7+
app.use(bodyParser.urlencoded({ extended: true }));
8+
9+
app.get('/', (req, res) => res.sendFile('auth.html', { root : __dirname}));
10+
11+
const port = process.env.PORT || 3000;
12+
app.listen(port , () => console.log('App listening on port ' + port));
13+
14+
/* PASSPORT SETUP */
15+
16+
const passport = require('passport');
17+
app.use(passport.initialize());
18+
app.use(passport.session());
19+
20+
app.get('/success', (req, res) => res.send("Welcome "+ req.query.username + "!!"));
21+
app.get('/error', (req, res) => res.send("error logging in"));
22+
23+
passport.serializeUser(function(user, cb) {
24+
cb(null, user);
25+
});
26+
27+
passport.deserializeUser(function(id, cb) {
28+
User.findById(id, function(err, user) {
29+
cb(err, user);
30+
});
31+
});
32+
33+
/* MONGOOSE SETUP */
34+
35+
const mongoose = require('mongoose');
36+
mongoose.connect('mongodb://localhost/MyDatabase');
37+
38+
const Schema = mongoose.Schema;
39+
const UserDetail = new Schema({
40+
username: String,
41+
password: String
42+
});
43+
const UserDetails = mongoose.model('userInfo', UserDetail, 'userInfo');
44+
45+
/* PASSPORT LOCAL AUTHENTICATION */
46+
47+
const LocalStrategy = require('passport-local').Strategy;
48+
49+
passport.use(new LocalStrategy(
50+
function(username, password, done) {
51+
UserDetails.findOne({
52+
username: username
53+
}, function(err, user) {
54+
if (err) {
55+
return done(err);
56+
}
57+
58+
if (!user) {
59+
return done(null, false);
60+
}
61+
62+
if (user.password != password) {
63+
return done(null, false);
64+
}
65+
return done(null, user);
66+
});
67+
}
68+
));
69+
70+
app.post('/',
71+
passport.authenticate('local', { failureRedirect: '/error' }),
72+
function(req, res) {
73+
res.redirect('/success?username='+req.user.username);
74+
});

node_modules/.bin/mime

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/semver

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/accepts/HISTORY.md

Lines changed: 218 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/accepts/LICENSE

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)