I am trying to create an android app which is similar to Tinder.
Until now, what I have is login with facebook and it workd like this: The user sends access token to my server (wrriten in node js), and then it authorized him using paspport-facebook-token library.
And now I need to create a database of users in using mongoose. How and where should I do it? How to save users if I have ONLY facebook login? I need to save the user base on what?
This is my server routes.js code:
module.exports = function(app) {
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.end("Node-Android-Chat-Project");
});
passport.use(new FacebookStrategy({
clientID: *****1246,
clientSecret: "******",
callbackURL: "http://localhost:8080/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
console.log("accesToken ", accessToken)
console.log ("refreshToken", refreshToken)
console.log ("profile", profile)
user = {} // find or create a user in your database
done(null, user)
}
));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback
app.post('/auth/facebook/token', function(req, res, next) {
passport.authenticate(['facebook-token'], function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
console.log(req.user);
return res.send({ success : false, message : 'authentication failed' });
}
console.log("Success!!");
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);
});
/*app.post('/auth/facebook/token',
passport.authenticate('facebook-token'),
function (req, res) {
console.log(req)
// do something with req.user
res.send(req.user? 200 : 401);
}
); */
//app.get('/auth/facebook', passport.authenticate('facebook'));
//app.get(, );
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
};