Skip to content

Instantly share code, notes, and snippets.

View ivanvs's full-sized avatar

Ivan Vasiljević ivanvs

View GitHub Profile
@ivanvs
ivanvs / 04-nodejs-twitter-token-handling.js
Created September 16, 2017 16:03
React example how to do authenitcation with Twitter
var createToken = function(auth) {
return jwt.sign({
id: auth.id
}, 'my-secret',
{
expiresIn: 60 * 120
});
};
var generateToken = function (req, res, next) {
@ivanvs
ivanvs / 02-nodejs-twitter-upsert.js
Created September 16, 2017 16:00
React example how to do authenitcation with Twitter
UserSchema.statics.upsertTwitterUser = function(token, tokenSecret, profile, cb) {
var that = this;
return this.findOne({
'twitterProvider.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,
twitterProvider: {
@ivanvs
ivanvs / 01-nodejs-twitter-model.js
Created September 16, 2017 15:58
React example how to do authenitcation with Twitter
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
module.exports = function () {
var db = mongoose.connect('mongodb://localhost:27017/twitter-demo');
var UserSchema = new Schema({
@ivanvs
ivanvs / 03-nodejs-twitter-passport-config.js
Created September 16, 2017 15:34
React example how to do authenitcation with Twitter
'use strict';
var passport = require('passport'),
TwitterTokenStrategy = require('passport-twitter-token'),
User = require('mongoose').model('User');
module.exports = function () {
passport.use(new TwitterTokenStrategy({
consumerKey: 'KEY',
@ivanvs
ivanvs / 08-nodejs-twitter-cors.js
Created September 16, 2017 15:22
React example how to do authenitcation with Twitter
// 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 / 05_swagger-to-existing-nodejs-project-specification.json
Created August 17, 2017 19:09
Demo application that shows how to add Swagger UI Spec to existing Node.js/Express.js project
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Yet Another Node.js Blogg Application API",
"description": "Yet Another Node.js Blogg Application API",
"license": {
"name": "MIT",
"url": "https://opensource.org/licenses/MIT"
}
@ivanvs
ivanvs / 04_swagger-to-existing-nodejs-project-swagger-ui.js
Created August 17, 2017 18:58
Demo application that shows how to add Swagger UI Spec to existing Node.js/Express.js project
var swaggerUi = require('swagger-ui-express'),
swaggerDocument = require('./swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/v1', router);
@ivanvs
ivanvs / 03_swagger-to-existing-nodejs-project-routes.js
Created August 16, 2017 21:58
Demo application that shows how to add Swagger UI Spec to existing Node.js/Express.js project
router.route('/users')
.post(createUser)
.get(getAllUsers);
router.route('/users/:userId')
.get(getOneUser)
.put(updateUser)
.delete(deleteUser);
router.param('userId', getByIdUser);
@ivanvs
ivanvs / 02_swagger-to-existing-nodejs-project-controllers.js
Last active August 16, 2017 21:56
Demo application that shows how to add Swagger UI Spec to existing Node.js/Express.js project
var createUser = function (req, res, next) {
var user = new User(req.body);
user.save(function (err) {
if (err) {
next(err);
} else {
res.json(user);
}
});
@ivanvs
ivanvs / 01_swagger-to-existing-nodejs-project-user-model.js
Last active August 20, 2017 18:07
Demo application that shows how to add Swagger UI Spec to existing Node.js/Express.js project
//defining user schema
var UserSchema = new Schema({
email: {
type: String, required: true,
trim: true, unique: true,
match: /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/
},
firstName: {type: String},
lastName: {type: String}
});