connect-ensure-login

Login session ensuring middleware for Connect and Express.

  • 所有者: jaredhanson/connect-ensure-login
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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.
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

Build Status

Credits

License

The MIT License

Copyright (c) 2012-2013 Jared Hanson <http://jaredhanson.net/>

主要指標

概覽
名稱與所有者jaredhanson/connect-ensure-login
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2012-06-29 15:27:46
推送於2022-09-17 01:53:33
最后一次提交2013-05-11 12:08:07
發布數2
最新版本名稱v0.1.1 (發布於 2013-05-11 12:08:28)
第一版名稱v0.1.0 (發布於 2012-06-30 19:59:37)
用户参与
星數469
關注者數12
派生數61
提交數18
已啟用問題?
問題數22
打開的問題數12
拉請求數1
打開的拉請求數10
關閉的拉請求數5
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?