Last active
August 29, 2015 14:17
-
-
Save sean-hill/e2315762fdfb0d311547 to your computer and use it in GitHub Desktop.
Auth Token Interceptor Service for AngularJS and Passport HTTP Bearer Strategy
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
angular.module('your-app') | |
.config(function($httpProvider){ | |
$httpProvider.interceptors.push('AuthTokenInterceptor'); | |
}) |
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
Auth Token Interceptor Service for AngularJS and Passport HTTP Bearer Strategy |
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
.factory('AuthTokenInterceptor', function ($q, Storage) { | |
return { | |
request: function (config) { | |
var authToken = Storage.get("authToken"); | |
config.headers = config.headers || {}; | |
if (authToken) { | |
config.headers.Authorization = 'Bearer ' + authToken; | |
} | |
return config || $q.when(config); | |
} | |
}; | |
}) |
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
// Used when an auth token is required for a route | |
var passport = require("passport"); | |
var path = require('path'); | |
var BearerStrategy = require("passport-http-bearer").Strategy; | |
var User = require('./models/user-model'); | |
// Use Bearer Strategy as authentication | |
passport.use(new BearerStrategy( | |
function(token, done) { | |
User.findOne({ authToken: token }, function (err, user) { | |
if (err) { return done(err); } | |
if (!user) { return done(null, false); } | |
return done(null, user, { scope: 'all' }); | |
}); | |
} | |
)); | |
module.exports = { | |
protect: passport.authenticate('bearer', { session: false }) | |
} |
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
// User Api | |
var express = require('express'); | |
var userApi = express.Router(); | |
var userRoute = require('./routes/user/base-user-route'); | |
var RouteAuth = require('./util/route-auth'); | |
userApi.post('/update-something', RouteAuth.protect, userRoute.updateSomething); | |
module.exports = userApi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Integration into an Ionic / NodeJS project can be found here. Happy coding 😄