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 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
| router.route('/auth/bitbucket') | |
| .post(passport.authenticate('bitbucket-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
| //token handling middleware | |
| 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
| 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
| 'use strict'; | |
| var passport = require('passport'), | |
| BitbucketTokenStrategy = require('passport-bitbucket-token'), | |
| User = require('mongoose').model('User'); | |
| module.exports = function () { | |
| passport.use(new BitbucketTokenStrategy({ | |
| clientID: 'app-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
| UserSchema.statics.upsertBitbuketUser = function(accessToken, refreshToken, profile, cb) { | |
| var that = this; | |
| return this.findOne({ | |
| 'bitbucketProvider.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, | |
| bitbucketProvider: { |
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
| 'use strict'; | |
| var mongoose = require('mongoose'), | |
| Schema = mongoose.Schema; | |
| module.exports = function () { | |
| var db = mongoose.connect('mongodb://localhost:27017/bitbucket-demo'); | |
| var UserSchema = new Schema({ |
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
| render() { | |
| let content = !!this.state.isAuthenticated ? | |
| ( | |
| <div> | |
| <p>Authenticated</p> | |
| <div> | |
| {this.state.user.email} | |
| </div> | |
| <div> | |
| <button onClick={this.logout} className="button" > |
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
| logout() { | |
| this.setState({isAuthenticated: false, token: '', user: null}) | |
| } |