backend logging
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
language: "node_js"
|
||||
node_js:
|
||||
- "0.4"
|
||||
- "0.6"
|
||||
- "0.8"
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2012-2013 Jared Hanson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
# connect-ensure-login
|
||||
|
||||
This middleware ensures that a user is logged in. If a request is received that
|
||||
is unauthenticated, the request will be redirected to a login page. The URL
|
||||
will be saved in the session, so the user can be conveniently returned to the
|
||||
page that was originally requested.
|
||||
|
||||
## Install
|
||||
|
||||
$ npm install connect-ensure-login
|
||||
|
||||
## Usage
|
||||
|
||||
#### Ensure Authentication
|
||||
|
||||
In this example, an application has a settings page where preferences can be
|
||||
configured. A user must be logged in before accessing this page.
|
||||
|
||||
app.get('/settings',
|
||||
ensureLoggedIn('/login'),
|
||||
function(req, res) {
|
||||
res.render('settings', { user: req.user });
|
||||
});
|
||||
|
||||
If a user is not logged in when attempting to access this page, the request will
|
||||
be redirected to `/login` and the original request URL (`/settings`) will be
|
||||
saved to the session at `req.session.returnTo`.
|
||||
|
||||
#### Log In and Return To
|
||||
|
||||
This middleware integrates seamlessly with [Passport](http://passportjs.org/).
|
||||
Simply mount Passport's `authenticate()` middleware at the login route.
|
||||
|
||||
app.get('/login', function(req, res) {
|
||||
res.render('login');
|
||||
});
|
||||
|
||||
app.post('/login', passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' }));
|
||||
|
||||
Upon log in, Passport will notice the `returnTo` URL saved in the session and
|
||||
redirect the user back to `/settings`.
|
||||
|
||||
#### Step By Step
|
||||
|
||||
If the user is not logged in, the sequence of requests and responses that take
|
||||
place during this process can be confusing. Here is a step-by-step overview of
|
||||
what happens:
|
||||
|
||||
1. User navigates to `GET /settings`
|
||||
- Middleware sets `session.returnTo` to `/settings`
|
||||
- Middleware redirects to `/login`
|
||||
2. User's browser follows redirect to `GET /login`
|
||||
- Application renders a login form (or, alternatively, offers SSO)
|
||||
3. User submits credentials to `POST /login`
|
||||
- Application verifies credentials
|
||||
- Passport reads `session.returnTo` and redirects to `/settings`
|
||||
4. User's browser follows redirect to `GET /settings`
|
||||
- Now authenticated, application renders settings page
|
||||
|
||||
## Tests
|
||||
|
||||
$ npm install --dev
|
||||
$ make test
|
||||
|
||||
[](http://travis-ci.org/jaredhanson/connect-ensure-login)
|
||||
|
||||
## Credits
|
||||
|
||||
- [Jared Hanson](http://github.com/jaredhanson)
|
||||
|
||||
## License
|
||||
|
||||
[The MIT License](http://opensource.org/licenses/MIT)
|
||||
|
||||
Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Ensure that a user is logged in before proceeding to next route middleware.
|
||||
*
|
||||
* This middleware ensures that a user is logged in. If a request is received
|
||||
* that is unauthenticated, the request will be redirected to a login page (by
|
||||
* default to `/login`).
|
||||
*
|
||||
* Additionally, `returnTo` will be be set in the session to the URL of the
|
||||
* current request. After authentication, this value can be used to redirect
|
||||
* the user to the page that was originally requested.
|
||||
*
|
||||
* Options:
|
||||
* - `redirectTo` URL to redirect to for login, defaults to _/login_
|
||||
* - `setReturnTo` set redirectTo in session, defaults to _true_
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* app.get('/profile',
|
||||
* ensureLoggedIn(),
|
||||
* function(req, res) { ... });
|
||||
*
|
||||
* app.get('/profile',
|
||||
* ensureLoggedIn('/signin'),
|
||||
* function(req, res) { ... });
|
||||
*
|
||||
* app.get('/profile',
|
||||
* ensureLoggedIn({ redirectTo: '/session/new', setReturnTo: false }),
|
||||
* function(req, res) { ... });
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
module.exports = function ensureLoggedIn(options) {
|
||||
if (typeof options == 'string') {
|
||||
options = { redirectTo: options }
|
||||
}
|
||||
options = options || {};
|
||||
|
||||
var url = options.redirectTo || '/login';
|
||||
var setReturnTo = (options.setReturnTo === undefined) ? true : options.setReturnTo;
|
||||
|
||||
return function(req, res, next) {
|
||||
if (!req.isAuthenticated || !req.isAuthenticated()) {
|
||||
if (setReturnTo && req.session) {
|
||||
req.session.returnTo = req.originalUrl || req.url;
|
||||
}
|
||||
return res.redirect(url);
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Ensure that no user is logged in before proceeding to next route middleware.
|
||||
*
|
||||
* This middleware ensures that no user is logged in. If a request is received
|
||||
* that is authenticated, the request will be redirected to another page (by
|
||||
* default to `/`).
|
||||
*
|
||||
* Options:
|
||||
* - `redirectTo` URL to redirect to in logged in, defaults to _/_
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* app.get('/login',
|
||||
* ensureLoggedOut(),
|
||||
* function(req, res) { ... });
|
||||
*
|
||||
* app.get('/login',
|
||||
* ensureLoggedOut('/home'),
|
||||
* function(req, res) { ... });
|
||||
*
|
||||
* app.get('/login',
|
||||
* ensureLoggedOut({ redirectTo: '/home' }),
|
||||
* function(req, res) { ... });
|
||||
*
|
||||
* @param {Object} options
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
module.exports = function ensureLoggedOut(options) {
|
||||
if (typeof options == 'string') {
|
||||
options = { redirectTo: options }
|
||||
}
|
||||
options = options || {};
|
||||
|
||||
var url = options.redirectTo || '/';
|
||||
|
||||
return function(req, res, next) {
|
||||
if (req.isAuthenticated && req.isAuthenticated()) {
|
||||
return res.redirect(url);
|
||||
}
|
||||
next();
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Expose middleware.
|
||||
*/
|
||||
exports.ensureAuthenticated =
|
||||
exports.ensureLoggedIn = require('./ensureLoggedIn');
|
||||
|
||||
exports.ensureUnauthenticated =
|
||||
exports.ensureNotAuthenticated =
|
||||
exports.ensureLoggedOut =
|
||||
exports.ensureNotLoggedIn = require('./ensureLoggedOut');
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "connect-ensure-login",
|
||||
"version": "0.1.1",
|
||||
"description": "Login session ensuring middleware for Connect.",
|
||||
"keywords": ["connect", "express", "auth", "authn", "authentication", "login", "session", "passport"],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/jaredhanson/connect-ensure-login.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "http://github.com/jaredhanson/connect-ensure-login/issues"
|
||||
},
|
||||
"author": {
|
||||
"name": "Jared Hanson",
|
||||
"email": "jaredhanson@gmail.com",
|
||||
"url": "http://www.jaredhanson.net/"
|
||||
},
|
||||
"licenses": [ {
|
||||
"type": "MIT",
|
||||
"url": "http://www.opensource.org/licenses/MIT"
|
||||
} ],
|
||||
"main": "./lib",
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {
|
||||
"vows": "0.6.x"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
|
||||
},
|
||||
"engines": { "node": ">= 0.4.0" }
|
||||
}
|
||||
Reference in New Issue
Block a user