-
-
Save Paulo-Martins/82c589dc914d285c3e000bc1325d1219 to your computer and use it in GitHub Desktop.
Revisions
-
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,8 +6,8 @@ "scripts": { "test": "" }, "author": "Saophalkun Ponlu", "license": "The MIT License (MIT)", "dependencies": { "body-parser": "^1.12.0", "connect-flash": "^0.1.1", -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ { "name": "password-local", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "" }, "author": "", "license": "ISC", "dependencies": { "body-parser": "^1.12.0", "connect-flash": "^0.1.1", "express": "^4.12.2", "express-session": "^1.10.3", "passport": "^0.2.1", "passport-local": "^1.0.0" } } -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ ## Install required packages ```shell npm install --save express npm install --save connect-flash npm install --save express-session -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ ## Install required packages ```bash npm install --save express npm install --save connect-flash npm install --save express-session -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ ## Setup the project folder ```shell mkdir passport-local npm init # Use default values for npm init prompts -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,14 @@ ## Setup the project folder ``` mkdir passport-local npm init # Use default values for npm init prompts ``` ## Install required packages ``` npm install --save express npm install --save connect-flash npm install --save express-session @@ -17,13 +17,13 @@ npm install --save passport npm install --save passport-local ``` ## Create authentication module In the root folder create a file called "auth.js" with the following content. ```javascript var passport = require('passport'), LocalStrategy = require('passport-local').Strategy, user = { // This a hard-coded user @@ -59,13 +59,13 @@ In the root folder create a file called "auth.js" with the following content. }); module.exports = passport; ``` ## Create app module In the root folder create a file called "app.js" with the following content. ```javascript var express = require('express'), app = express(), session = require('express-session'), @@ -136,4 +136,4 @@ In the root folder create a file called "app.js" with the following content. console.log('Server running on http://127.0.0.1:%s', port); }); ``` -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,7 +23,7 @@ In the root folder create a file called "auth.js" with the following content. `` var passport = require('passport'), LocalStrategy = require('passport-local').Strategy, user = { // This a hard-coded user @@ -65,7 +65,7 @@ In the root folder create a file called "auth.js" with the following content. In the root folder create a file called "app.js" with the following content. `` var express = require('express'), app = express(), session = require('express-session'), -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ ## Install required packages `` npm install --save express npm install --save connect-flash npm install --save express-session -
phalkunz revised this gist
Mar 15, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ ## Setup the project folder `` mkdir passport-local npm init # Use default values for npm init prompts -
phalkunz created this gist
Mar 15, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,139 @@ ## Setup the project folder ``bash mkdir passport-local npm init # Use default values for npm init prompts `` ## Install required packages ``sh npm install --save express npm install --save connect-flash npm install --save express-session npm install --save body-parser npm install --save cookie-parser npm install --save passport npm install --save passport-local `` ## Create authentication module In the root folder create a file called "auth.js" with the following content. ``js var passport = require('passport'), LocalStrategy = require('passport-local').Strategy, user = { // This a hard-coded user _id: 1, username: 'john', email: '[email protected]', password: 'password' }; // Register a login strategy passport.use('login', new LocalStrategy( function(username, password, done) { // This should check again db if(username === user.username && password === user.password) { return done(null, user); } else { done(null, false, { message: 'Invalid username and password.' }); } } )); // Required for storing user info into session passport.serializeUser(function(user, done) { done(null, user._id); }); // Required for retrieving user from session passport.deserializeUser(function(id, done) { // The user should be queried against db // using the id done(null, user); }); module.exports = passport; `` ## Create app module In the root folder create a file called "app.js" with the following content. ``js var express = require('express'), app = express(), session = require('express-session'), flash = require('connect-flash'), session = require('express-session'), auth = require('./auth.js'), bodyParser = require('body-parser'); app.use('/', express.static(__dirname + '/public')); app.use(session({ secret: 'some-secret', saveUninitialized: false, resave: true })); // For parsing post request's data/body app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); // Tells app to use password session app.use(auth.initialize()); app.use(auth.session()); app.use(flash()); // Set up routes app.get('/', function(req, res) { if(req.user) { res.send( '<p>You\'re logged in as <strong>' + req.user.username + '</strong>.</p>' + '<p><a href="/logout">Log out</a></p>' ); } else { res.send('<p><a href="/login">Login</a></p>'); } }); app.get('/login', function(req, res) { res.send( '<form action="/login" method="POST">' + '<h2>Login</h2>' + '<p><input name="username"></p>' + '<p><input name="password"></p>' + '<p><input type="submit" value="Login"></p>' + '<p style="color: red;">' + req.flash('error') + '</p>' + '</form>' ); }); app.get('/logout', function(req, res) { req.logout(); res.redirect('/'); }); app.post('/login', auth.authenticate('login', { successRedirect: '/', failureRedirect: '/login', failureFlash: true }) ); var server = app.listen(3000, function() { var port = server.address().port; console.log('Server running on http://127.0.0.1:%s', port); }); ``