Created
June 27, 2014 17:07
-
-
Save rdegges/b1f4970f24b054912a67 to your computer and use it in GitHub Desktop.
stormpath node api
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 app = require('express')(); | |
var stormpath = require('express-stormpath'); | |
// example 1 - auth by middleware | |
app.use(stormpath({ | |
'/', true, // enforce login | |
})); | |
app.get('/', function(req, res) { | |
res.send('im authenticated!'); | |
}); | |
// example 2 - auth by explicitness | |
app.use(stormpath()); | |
app.get('/', function(req, res) { | |
stormpath.login_required(); // enforce login | |
res.send('im authenticated!'); | |
}); | |
// example 3 - auth by kinda explicitness | |
app.use(stormpath()); | |
app.get('/', stormpath.login_required(function (req, res) { | |
res.send('im authenticated!'); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think it would be helpful to define the use cases, of which I see two:
app.use(stormpath.default())
- which will register a global middleware which does your self-proclaimed magic by inspecting all incoming requests and doing the right thing.And both could totally be supported by the same module
The implementation of the first use case is totally up to you and you can modify the solution over time via feedback from developers. You can give the developer some options to be passed into
default({})
, such as: what is the route for the login page? what do i do when someone isn't logged in but needs to be (do i send them to /login, or just show an access denied page? etc). In this situation you will be defining a lot of application behaviour and UX for the user, at which point it's become more than middleware, in my opinionThe latter should be more of a pure middleware situation, meaning: implement a function that does something really discreet, given a specific request. I have good success with the "chaining" style of connect middleware, which would look like this:
Thus your implementation of
stormpath.loginRequired
would look something like:You could support all of those ideas, via options, by having
stormpath.loginRequired
return a function in response to an options object. That would look like:And then you would use it like this:
If the number of options would be really complex, you could also consider implementing several functions such as
redirectOnLoginFailure
errorOnLoginFailure
etc