Last active
December 22, 2015 13:39
-
-
Save Synvox/6480416 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * AuthController | |
| * | |
| * @module :: Controller | |
| * @description :: Contains logic for handling requests. | |
| */ | |
| var passport = require('passport'); | |
| module.exports = { | |
| index: function(req, res) { | |
| res.json({}); | |
| }, | |
| logout: function(req, res) { | |
| req.logout(); | |
| res.redirect('/'); | |
| }, | |
| 'facebook': function(req, res) { | |
| passport.authenticate('facebook', { | |
| failureRedirect: '/auth', | |
| scope: 'email' | |
| }, function(err, user) { | |
| req.logIn(user, function(err) { | |
| if (err) { | |
| console.log(err); | |
| res.view('500'); | |
| return; | |
| } | |
| res.redirect('/'); | |
| return; | |
| }); | |
| })(req, res); | |
| }, | |
| 'facebookcb': function(req, res) { | |
| console.log('Fired'); // Is logged several times | |
| passport.authenticate('facebook', | |
| function(req, res) { | |
| res.redirect('/'); | |
| })(req, res); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Bootstrap | |
| * | |
| * An asynchronous boostrap function that runs before your Sails app gets lifted. | |
| * This gives you an opportunity to set up your data model, run jobs, or perform some special logic. | |
| * | |
| * For more information on bootstrapping your app, check out: | |
| * http://sailsjs.org/#documentation | |
| */ | |
| var passport = require('passport'), | |
| FacebookStrategy = require('passport-facebook').Strategy; | |
| var verifyHandler = function(token, tokenSecret, profile, done) { | |
| process.nextTick(function() { | |
| User.findOne({ | |
| uid: profile.id | |
| }).done(function(err, user) { | |
| if (user) { | |
| return done(null, user); | |
| } else { | |
| User.create({ | |
| provider: profile.provider, | |
| uid: profile.id, | |
| name: profile.displayName | |
| }).done(function(err, user) { | |
| return done(err, user); | |
| }); | |
| } | |
| }); | |
| }); | |
| }; | |
| passport.serializeUser(function(user, done) { | |
| done(null, user.uid); | |
| }); | |
| passport.deserializeUser(function(uid, done) { | |
| User.findOne({ | |
| uid: uid | |
| }).done(function(err, user) { | |
| done(err, user) | |
| }); | |
| }); | |
| module.exports.express = { | |
| customMiddleware: function(app) { | |
| passport.use(new FacebookStrategy({ | |
| clientID: '...', | |
| clientSecret: '...', | |
| callbackURL: 'http://localhost:1337/auth/facebookcb' | |
| }, | |
| verifyHandler | |
| )); | |
| app.use(passport.initialize()); | |
| app.use(passport.session()); | |
| } | |
| } | |
| module.exports.bootstrap = function(cb) { | |
| // It's very important to trigger this callack method when you are finished | |
| // with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap) | |
| cb(); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sails.js <| | |
| v0.9.3 |\ | |
| /|.\ | |
| / || \ | |
| ,' |' \ | |
| .-'.-==|/_--' | |
| `--'-------' | |
| __---___--___---___--___---___--___ | |
| ____---___--___---___--___---___--___-__ | |
| info: Server lifted in `...` | |
| info: To see your app, visit http://localhost:1337 | |
| info: To shut down Sails, press <CTRL> + C at any time. | |
| debug: -------------------------------------------------------- | |
| debug: :: Sat Sep 07 2013 17:36:03 GMT-0600 (MDT) | |
| debug: | |
| debug: Environment : development | |
| debug: Port : 1337 | |
| debug: -------------------------------------------------------- | |
| info: handshake authorized BnoXCoF52kD34XKIyEJn | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| Fired | |
| info: transport end (socket end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment