0

I am using a nodejs-express-passport solution for authentication with facebook. My goal is to automatically upload an image from a static directory the moment that a user is successfully logged in. If my image is simply animal.png located at /images how would I modify the following node-express server code to accomplish this?

var express = require('express');
var passport = require('passport');
var Strategy = require('passport-facebook').Strategy;



passport.use(new Strategy({
    clientID: process.env.CLIENT_ID,
    clientSecret: process.env.CLIENT_SECRET,
    callbackURL: 'http://localhost:3000/login/facebook/return'
  },
  function(accessToken, refreshToken, profile, cb) {

    return cb(null, profile);
  }));

passport.serializeUser(function(user, cb) {
  cb(null, user);
});

passport.deserializeUser(function(obj, cb) {
  cb(null, obj);
});


var app = express();

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');


app.use(require('cookie-parser')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('express-session')({ secret: 'keyboard cat', resave: true, saveUninitialized: true }));


app.use(passport.initialize());
app.use(passport.session());


app.get('/',
  function(req, res) {
    res.render('home', { user: req.user });
  });

app.get('/login',
  function(req, res){
    res.render('login');
  });

app.get('/login/facebook',
  passport.authenticate('facebook','user_photos'));

app.get('/login/facebook/return', 
  passport.authenticate('facebook', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });

app.get('/profile',
  require('connect-ensure-login').ensureLoggedIn(),
  function(req, res){
    res.render('profile', { user: req.user });
  });

app.listen(3000);
Bachalo
  • 6,965
  • 27
  • 95
  • 189
  • You are not allowed to post/upload photos _automatically_ – the user has to actively trigger that. – CBroe Mar 04 '16 at 08:14

1 Answers1

0

For res.render('profile', {user: req.user, image: 'my image string location'}) then with your html just pass in {{image}} to wherever you are trying to src the image from.

I am not super familiar with ejs templating but it'd look something like this:

<img src={{image}} alt="MyAnimal.png" style="width:304px;height:228px;">

Or did you want it to just flash on the screen? If that's the case I think you can do something like that with the flash module.

Edit:

I'll move this up here as this was a question for posting desktop data to facebook and not just popping up a picture from the server.

Passport does authentication handshakes to allow people access to your website via login, it does not however open up all the api's to Twitter/Facebook/Google, you'll need to either make them yourself, or use ones that are pre built like this Facebook API.

https://www.npmjs.com/package/fb

Morgan G
  • 3,089
  • 4
  • 18
  • 26
  • forgot to add, I am NOT uploading images to the node server, but rather uploading local desktop images to facebook (after user logs in) – Bachalo Mar 06 '16 at 23:47
  • You'll have to use Facebook's API to load an image to facebook. Passport only does a handshake as far as I'm aware for authentication. Have you looked at Facebook's SDK? Using something like this https://github.com/Thuzi/facebook-node-sdk – Morgan G Mar 07 '16 at 04:22
  • Thanks. Will try that but that module is 2 years old. These are the code modules I am referencing https://github.com/jaredhanson/passport-facebook https://github.com/passport/express-4.x-local-example – Bachalo Mar 08 '16 at 10:22
  • I have managed to upload an image automatically, but only to the application FB account, NOT to a user's FB account. I am getting an error saying that my auth code has been used. See http://stackoverflow.com/questions/35858226/errormessagethis-authorization-code-has-been-used-typeoauthexceptio – Bachalo Mar 08 '16 at 16:43
  • I will take a look at these and try and answer this question tomorrow, I am not super familiar with passport, I've always preferred making my own authentication layer but I'll look at it! Sorry about my slow replies – Morgan G Mar 08 '16 at 23:37
  • Adding a large bounty tomorrow March 10 to http://stackoverflow.com/questions/35858226/facebooktokenerror-this-authorization-code-has-been-used – Bachalo Mar 09 '16 at 13:23
  • Ok I'm sorry I haven't been more helpful! I'll see if I can answer it before you have to give away your points! – Morgan G Mar 09 '16 at 20:35
  • Hi Morgan, very much appreciate four feedback but would rather get my existing code module(s) working since I am very close to a solution! http://stackoverflow.com/questions/35858226/facebooktokenerror-this-authorization-code-has-been-used – Bachalo Mar 10 '16 at 03:37