node.js - Nodejs + Passport + MySQL -



node.js - Nodejs + Passport + MySQL -

i'm trying figure out how utilize nodejs + passport + mysql. seems though every tutorial out there using mongodb , don't want that. in fact quick searches of type yield web pages (http://nodejsrocks.blogspot.com/2012/04/nodejs-expressjs-mysql.html) , youtube video guy (https://www.youtube.com/watch?v=jgbbmvjx3h0) doing nil loging in, , knows using, page has had 3k + views. i'd hope of developers @ , maybe there utilize comprehensive non mvc type of thing mysql. reason trying ios , android capabilities , have no need big scaffolding overhead. db , server side scripting handling queries , returning json objects phones.

so, beingness said, can has had real experience please help me out(and rest of world trying similar things without in-depth tutorials, because aren't using mongodb , total blown scaffolding).

the tables have set 'twitterstrategy' users(id (pk), username, email, salt, password), , twitterusers(id (pk), name, screenname, location, description, url, img, token, tokensecret).

here code trying going single main.js file. know not best practices, , plan clean later, now, understand missing , things working. extremely appreciated if can help, , i'm sure others find useful well. thanks.

var http = require('http'), mysql = require('mysql'), url = require('url'), crypto = require('crypto'), express = require('express'), flash = require('connect-flash'), passport = require('passport'), twitterstrategy = require('passport-twitter').strategy; var db = mysql.createconnection({ host : "****", user : "****", password : "****", port : '****', database : '****' }); // connect connection db , throw error if exists db.connect(function(err) { if (err) { console.error('error connecting db'); console.error(err); return; } console.log('database connected'); }); var twitter_consumer_key = "****"; var twitter_consumer_secret = "****"; passport.use(new twitterstrategy({ consumerkey: twitter_consumer_key, consumersecret: twitter_consumer_secret, callbackurl: 'http://127.0.0.1:3000/auth/twitter/callback'}, function(accesstoken, refreshtoken, profile, done) { //db.query(select ........ ...... ........, function (err, user){ if (err) { console.log(err); } if (!err && user != null){ done(null, result); } else { console.log(result); } }) }); } )); passport.serializeuser(function(user, done) { console.log('serializeuser: ' + user.id); done(null, user.id); }); passport.deserializeuser(function(id, done) { db.query('select * users id = ' + id, function(err, result) { if (err){ console.log(err); } else { console.log(result); } if (!err) { done(null, result); } else { done(err, null); } }); }); var app = express(); app.set(function(){ // app.set('views', __dirname + '/views'); // views aren't beingness used here // app.set('view engine', 'jade'); // using jade views, not used // app.use(express.favicon()); // not sure important, should web app.use(express.logger('dev')); // again, not sure of import app.use(express.bodyparser()); // have no thought used app.use(express.methodoverride()); // same no fn clue app.use(express.cookieparser('what f')); app.use(express.session()); app.use(passport.initialize()); app.use(passport.session()); app.use(flash()); // app.use(app.router); // here 1 time again defining our routes in main, shouldn't need this. // app.use(express.static(__dirname + '/public')); }); var server = http.createserver(function(req, res) { console.log('url: ' + req.url); var params = url.parse(req.url, true) var path = params.pathname; if (path == '/signup') { console.log("user signing up"); onsignup(params, res); } else if (path == '/signin') { console.log("user signing in"); onsignin(params, res); } else if (path == '/auth/twitter'){ passport.authenticate('twitter'), function(req, res){ console.log('twitter user created or signed in'); } } }); //keep server live , listening requests db connected server.listen(3000);

am missing auth table? need set in mysql statement dots can find user, , parameters beingness passed user request query going, i.e. oauth id have seen in tutorials getting passed seems user twitter authorization? also, should expecting callback twitter? anyway, i'll glad post of somewhere else @ 1 time have solution made of using mysql , node don't left out , have search google find seems though should readily available, instead of copies of same exact nodejs + mongodb + express tutorial (with many out of date except scotch io, looks if wanna utilize mongo...might add together instances on @ amazon run $279 estimated per month on low end) floating around , beingness redistributed "tutorial" out there. again.

try wrapping strategy function under process.nexttick, e.g.,

passport.use(new twitterstrategy({ consumerkey: twitter_consumer_key, consumersecret: twitter_consumer_secret, callbackurl: 'http://127.0.0.1:3000/auth/twitter/callback'}, function(accesstoken, refreshtoken, profile, done) { process.nexttick(function(){ // set logic check profile sent twitter in db or not, // totally whether maintain separate auth table or not // nb: there unique value in profile can used next references db.query(select ........ ...... ........, function (err, user){ if (err) { console.log(err); } if (!err && user != null){ done(null, result); } else { console.log(result); } }) }); }); } ));

you have have route accepting callback, e.g.,

app.get('/auth/twitter/callback', function(req, res, next) { passport.authenticate('twitter', { }, function(err, user) { // result send strategy function here // user send } )(req, res, next); });

hope makes things clearer.

mysql node.js passport.js passport-twitter

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -