Closed sailorhero closed 11 years ago
nice!
I was able to get Facebook Authentication working but am getting issues with Twitter, if I log in with Twitter, it will direct me to Twitter page to allow my twitter app to authorize it, one I click sign in, it gives me an error saying : failed to find request token in session at Strategy.OAuthStrategy.authenticate.
Need install passport-local package for this project. `sudo npm install passport-local`
And change the strategy.js/ index.js / config.json and create new local login.jade files. > strategy.js ``` /* * Module dependencies */ var passport = require('passport') , TwitterStrategy = require('passport-twitter').Strategy , FacebookStrategy = require('passport-facebook').Strategy , LocalStrategy = require('passport-local').Strategy , config = require('./config.json'); /* * local Authentication */ var users = [ { id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' } , { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' } ]; function findById(id, fn) { var idx = id - 1; if (users[idx]) { fn(null, users[idx]); } else { fn(new Error('User ' + id + ' does not exist')); } } function findByUsername(username, fn) { for (var i = 0, len = users.length; i < len; i++) { var user = users[i]; if (user.username === username) { return fn(null, user); } } return fn(null, null); } if (config.auth.local.value == true) { passport.use(new LocalStrategy( function(username, password, done) { // asynchronous verification, for effect... process.nextTick(function () { // Find the user by username. If there is no user with the given // username, or the password is not correct, set the user to `false` to // indicate failure and set a flash message. Otherwise, return the // authenticated `user`. findByUsername(username, function(err, user) { if (err) { return done(err); } if (!user) { return done(null, false, { message: 'Unknown user ' + username }); } if (user.password != password) { return done(null, false, { message: 'Invalid password' }); } return done(null, user); }) }); } )); } ``` > index.js ``` /* * Local Login routes */ app.get('/login', function(req, res){ res.render('login', { user: req.user}); }); app.post('/login', passport.authenticate('local', { failureRedirect: '/login' }), function(req, res) { res.redirect('/rooms'); }); ```
Create a new view page, name as login.jade > login.jade ``` extends layout block content body.home-page .homepage-container .slideshow #slideshow .slide h2 Realtime & Free .slide-image // #slideshow .login-box .inner-padding div#chat form(action='/login', method='POST') input#chat(name='username', type='text') input#chat(name='password', type='password') input(type='submit', value='Send') // #login-box // .homepage-container footer | Balloons.IO is an a(href='https://github.com/gravityonmars/Balloons.IO', target="_blank") Open Source project | developed by a(href='http://twitter.com/gravityonmars', target="_blank") Gravity On Mars. // JavaScript at the bottom for fast page loading ``` > config.json ``` { "auth": { "twitter": { "consumerkey" : "yourConsumerKey", "consumersecret" : "yourSecretKey", "callback" : "http://127.0.0.1:6789/auth/twitter/callback" }, "facebook": { "clientid" : "yourClientID", "clientsecret" : "yourClientSecret", "callback" : "http://127.0.0.1:6789/auth/facebook/callback" }, "local":{ "value" : true } }, "theme": { "name" : "default" }, "session" : { "secret" : "yourbestsecret" }, "app": { "port": 8000 } } ```
passport-local > https://github.com/jaredhanson/passport-local