Skip to content

Instantly share code, notes, and snippets.

View ivanvs's full-sized avatar

Ivan Vasiljević ivanvs

View GitHub Profile
@ivanvs
ivanvs / 08-nodejs-bitbucket-cors.js
Created July 2, 2017 22:44
React example how to do authenitcation with Bitbucket
// enable cors
var corsOption = {
origin: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
exposedHeaders: ['x-auth-token']
};
app.use(cors(corsOption));
@ivanvs
ivanvs / 07-nodejs-bitbucket-all-other-routes.js
Created July 2, 2017 22:43
React example how to do authenitcation with Bitbucket
var getCurrentUser = function(req, res, next) {
User.findById(req.auth.id, function(err, user) {
if (err) {
next(err);
} else {
req.user = user;
next();
}
});
};
@ivanvs
ivanvs / 06-nodejs-bitbucket-bitbucket-route.js
Created July 2, 2017 22:42
React example how to do authenitcation with Bitbucket
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
};
@ivanvs
ivanvs / 05-nodejs-bitbucket-jwt-handling.js
Created July 2, 2017 22:40
React example how to do authenitcation with Bitbucket
//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;
}
@ivanvs
ivanvs / 04-nodejs-bitbucket-token-handling.js
Created July 2, 2017 22:39
React example how to do authenitcation with Bitbucket
var createToken = function(auth) {
return jwt.sign({
id: auth.id
}, 'my-secret',
{
expiresIn: 60 * 120
});
};
var generateToken = function (req, res, next) {
@ivanvs
ivanvs / 03-nodejs-bitbucket-passport-config.js
Created July 2, 2017 22:37
React example how to do authenitcation with Bitbucket
'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',
@ivanvs
ivanvs / 02-nodejs-bitbucket-upsert.js
Created July 2, 2017 22:35
React example how to do authenitcation with Bitbucket
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: {
@ivanvs
ivanvs / 01-nodejs-bitbucket-model.js
Created July 2, 2017 22:34
React example how to do authenitcation with Bitbucket
'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({
@ivanvs
ivanvs / 06-react-authentication-render.jsx
Created July 1, 2017 22:29
React example how to do authenitcation with Bitbucket
render() {
let content = !!this.state.isAuthenticated ?
(
<div>
<p>Authenticated</p>
<div>
{this.state.user.email}
</div>
<div>
<button onClick={this.logout} className="button" >
@ivanvs
ivanvs / 05-react-authentication-logout.js
Created July 1, 2017 22:28
React example how to do authenitcation with Bitbucket
logout() {
this.setState({isAuthenticated: false, token: '', user: null})
}