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
| var UserSchema = new Schema({ | |
| email: { | |
| type: String, required: true, | |
| trim: true, unique: true, | |
| match: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ | |
| }, | |
| facebookProvider: { | |
| type: { | |
| id: String, | |
| token: String |
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
| UserSchema.statics.upsertFbUser = function(accessToken, refreshToken, profile, cb) { | |
| var that = this; | |
| return this.findOne({ | |
| 'facebookProvider.id': profile.id | |
| }, function(err, user) { | |
| // no user was found, lets create a new one | |
| if (!user) { | |
| var newUser = new that({ | |
| email: profile.emails[0].value, | |
| facebookProvider: { |
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
| passport.use(new FacebookTokenStrategy({ | |
| clientID: ‘YOUR-CLIENT-ID-HERE’, | |
| clientSecret: ‘YOUR-CLIENT-SECRET-HERE’ | |
| }, | |
| function (accessToken, refreshToken, profile, done) { | |
| User.upsertFbUser(accessToken, refreshToken, profile, function(err, user) { | |
| return done(err, user); | |
| }); | |
| })); |
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
| var createToken = function(auth) { | |
| return jwt.sign({ | |
| id: auth.id | |
| }, 'my-secret', | |
| { | |
| expiresIn: 60 * 120 | |
| }); | |
| }; | |
| var generateToken = function (req, res, next) { |
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
| var authenticate = expressJwt({ | |
| secret: 'my-secret', | |
| requestProperty: 'auth', | |
| getToken: function(req) { | |
| if (req.headers['x-auth-token']) { | |
| return req.headers['x-auth-token']; | |
| } | |
| return null; | |
| } | |
| }); |
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
| router.route('/auth/facebook') | |
| .post(passport.authenticate('facebook-token', {session: false}), function(req, res, next) { | |
| if (!req.user) { | |
| return res.send(401, 'User Not Authenticated'); | |
| } | |
| // prepare token for API | |
| req.auth = { | |
| id: req.user.id | |
| }; |
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
| var getCurrentUser = function(req, res, next) { | |
| User.findById(req.auth.id, function(err, user) { | |
| if (err) { | |
| next(err); | |
| } else { | |
| req.user = user; | |
| next(); | |
| } | |
| }); | |
| }; |
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
| // enable cors | |
| var corsOption = { | |
| origin: true, | |
| methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', | |
| credentials: true, | |
| exposedHeaders: ['x-auth-token'] | |
| }; | |
| app.use(cors(corsOption)); |
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
| var data = "data that we want to hash"; | |
| var crypto = require('crypto'); | |
| crypto.createHash('sha1').update(data).digest("hex"); |
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
| authenticate(key) { | |
| let that = this; | |
| this.client.post('/auth/bitbucket', { | |
| access_token: key | |
| }) | |
| .then(response => { | |
| this.client = axios.create({ | |
| baseURL: 'http://localhost:4000/api/v1/', | |
| timeout: 3000, | |
| headers: {'x-auth-token': response.headers['x-auth-token']} |