18.05.2016 Views

Mittwoch, 18. Mai, 2016

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

MongoDB CRUD the MVC way with Passport Authentication 85<br />

1 var port = 1337;<br />

2<br />

3 module.exports = {<br />

4 port: port,<br />

5 db: 'mongodb://localhost/todos',<br />

6 facebook: {<br />

7 clientID: 'App ID',<br />

8 clientSecret: 'App Secret',<br />

9 callbackURL: 'http://localhost:'+ port +'/oauth/facebook/callback'<br />

10 }<br />

11 };<br />

You need to enter your own App ID as cliendID, and App secret as clientSecret (values which you<br />

got when you made your Facebook applicaiton).<br />

Now, go to your config/strategies folder, and create a new file named facebook.js and place the<br />

following code:<br />

1 var passport = require('passport'),<br />

2 url = require('url'),<br />

3 FacebookStrategy = require('passport-facebook').Strategy,<br />

4 config = require('../config'),<br />

5 users = require('../../app/controllers/users.server.controller');<br />

6<br />

7 module.exports = function() {<br />

8 passport.use(new FacebookStrategy({<br />

9 clientID: config.facebook.clientID,<br />

10 clientSecret: config.facebook.clientSecret,<br />

11 callbackURL: config.facebook.callbackURL,<br />

12 passReqToCallback: true<br />

13 },<br />

14 function(req, accessToken, refreshToken, profile, done) {<br />

15 var providerData = profile._json;<br />

16 providerData.accessToken = accessToken;<br />

17 providerData.refreshToken = refreshToken;<br />

18<br />

19 var providerUserProfile = {<br />

20 name: profile.name.givenName,<br />

21 email: profile.emails[0].value,<br />

22 username: profile.username,<br />

23 provider: 'facebook',<br />

24 providerId: profile.id,<br />

25 providerData: providerData

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!