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
| 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: { |
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/twitter-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
| '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', |
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
| { | |
| "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" | |
| } |
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 swaggerUi = require('swagger-ui-express'), | |
| swaggerDocument = require('./swagger.json'); | |
| app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); | |
| app.use('/api/v1', router); |
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('/users') | |
| .post(createUser) | |
| .get(getAllUsers); | |
| router.route('/users/:userId') | |
| .get(getOneUser) | |
| .put(updateUser) | |
| .delete(deleteUser); | |
| router.param('userId', getByIdUser); |
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 createUser = function (req, res, next) { | |
| var user = new User(req.body); | |
| user.save(function (err) { | |
| if (err) { | |
| next(err); | |
| } else { | |
| res.json(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
| //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} | |
| }); |