I am making an application that requires users to log in, either via Twitter or Facebook and upon successful logging in, it redirects them to the /share
url. I am not that much experienced with express, hence I believe that my approach here is wrong. I am using share-dialog npm package.
What I am trying to do is, once the user inputs the share links for twitter, I am sending them to the server via AJAX. Upon validation on the NODE's side, I am sending the object back to the client. Now I have no idea how to get to this object. Is my approach wrong?
Complete app.js:
var express = require('express')
, passport = require('passport')
, util = require('util')
, FacebookStrategy = require('passport-facebook').Strategy
, TwitterStrategy = require('passport-twitter').Strategy
, session = require('express-session')
, cookieParser = require('cookie-parser')
, bodyParser = require('body-parser')
, config = require('./configuration/config')
, mysql = require('mysql')
, Dialog = require('share-dialog')
, cors = require ('cors')
, app = express();
app.use(cors());
//If needed..
var connection = mysql.createConnection({
host: config.host,
user: config.username,
password: config.password,
database: config.database
});
//We are 'false'.
if (config.use_database === 'true') {
connection.connect();
}
// Passport session setup.
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
// Passport.js strategy with Facebook
passport.use(new FacebookStrategy({
clientID: config.facebook_api_key,
clientSecret: config.facebook_api_secret,
callbackURL: config.facebook_callback_url,
authURL: config.facebook_auth_url,
logoutURL: config.logout_url
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
//Check whether the user exists or not using profile.id
return done(null, profile);
});
}
));
passport.use(new TwitterStrategy({
consumerKey: config.twitter_api_key,
consumerSecret: config.twitter_api_secret,
callbackURL: config.twitter_callback_url,
authURL: config.twitter_auth_url,
logoutURL: config.logout_url
},
function (token, tokenSecret, profile, done) {
process.nextTick(function () {
//Check whether the user exists or not using profile.id
return done(null, profile)
});
}
));
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(session({secret: 'keyboard cat', key: 'sid'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public'));
//Router code
app.get('/', function (req, res) {
res.render('index', {user: req.user});
});
app.get('/share', ensureAuthenticated, function(req, res) {
res.render('share', {user: req.user});
});
app.post('/share', function(req, res) {
var status = Object.keys(req.body).join('');
var twitter = Dialog.twitter(status);
var url = twitter.get();
console.log(status);
res.json({'url': url});
res.end();
});
app.get('/share', ensureAuthenticated, function(req, res) {
res.render('share', {user: req.user});
});
app.get('/account', ensureAuthenticated, function (req, res) {
res.render('account', {user: req.user});
});
//Passport Router - Facebook OAuth
app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '/share',
failureRedirect: '/'
}),
function (req, res) {
res.redirect('/');
});
//Passport Router - Twitter OAuth
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect : '/share',
failureRedirect: '/'
}),
function(req, res) {
res.redirect('/');
});
//Redirect and logout
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/')
}
// Redirect all 404
app.get('*', function (req, res) {
res.render('404');
});
app.listen(3000);
Script.js on client's side:
function postLinks() {
var shareVal = document.querySelector('#share-input').value;
var xhr = new XMLHttpRequest();
var url = "http://127.0.0.1:3000/share";
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if(this.readyState === 4) {
console.log("Passed: " + this.status);
}
else {
console.log("Error: " + this.readyState);
}
};
xhr.send(shareVal);
}
What should I do here guys? Please help.
EDIT: Please note that I do get the objects, for example:
url: "https://twitter.com/intent/tweet?url=http%3A%2F%2Fgoogle.com"
Thanks.
via tholo
No comments:
Post a Comment