Skip to content

Instantly share code, notes, and snippets.

View ivanvs's full-sized avatar

Ivan Vasiljević ivanvs

View GitHub Profile
@ivanvs
ivanvs / 04-react-authentication-login.js
Last active July 2, 2017 22:17
React example how to do authenitcation with Bitbucket
bitbucketLogin() {
let key = 'secret_key';
window.location =
`https://bitbucket.org/site/oauth2/authorize?client_id=${key}&response_type=token`;
}
@ivanvs
ivanvs / 03-react-authentication-loading.js
Last active July 1, 2017 22:29
React example how to do authenitcation with Bitbucket
componentDidMount() {
let params = window.location.hash.split('&');
if (params.length > 0 && params[0].startsWith('#access_token=')) {
let key = decodeURIComponent(params[0].replace('#access_token=', ''));
this.authenticate(key);
}
}
@ivanvs
ivanvs / 02-react-authentication-constructor.js
Created July 1, 2017 22:26
React example how to do authenitcation with Bitbucket
constructor(props) {
super(props);
this.client = axios.create({
baseURL: 'http://localhost:4000/api/v1/',
timeout: 3000,
headers: {'Accept': 'application/json'},
});
this.state = {key: '', isAuthenticated: false, user: null, token: ''};
@ivanvs
ivanvs / 01-react-bitbucket-authentication.js
Last active July 2, 2017 22:17
React example how to do authenitcation with Bitbucket
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']}
@ivanvs
ivanvs / node_sha1_example.js
Created June 11, 2017 11:03
Node.js create SHA1 hash from string
var data = "data that we want to hash";
var crypto = require('crypto');
crypto.createHash('sha1').update(data).digest("hex");
// enable cors
var corsOption = {
origin: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
exposedHeaders: ['x-auth-token']
};
app.use(cors(corsOption));
var getCurrentUser = function(req, res, next) {
User.findById(req.auth.id, function(err, user) {
if (err) {
next(err);
} else {
req.user = user;
next();
}
});
};
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
};
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;
}
});
var createToken = function(auth) {
return jwt.sign({
id: auth.id
}, 'my-secret',
{
expiresIn: 60 * 120
});
};
var generateToken = function (req, res, next) {