GithubHelp home page GithubHelp logo

global-localhost / node-connect-login Goto Github PK

View Code? Open in Web Editor NEW

This project forked from continuumio/node-connect-login

0.0 0.0 0.0 148 KB

Connect Login middleware for NodeJS

License: MIT License

JavaScript 93.49% HTML 6.51%

node-connect-login's Introduction

node-connect-login

A simple login middleware that does not impose any methodology on you!

This module is based of of Python's Flask-Login module.

It will:

  • Store the active user’s ID in the session, and let you log them in and out easily.
  • Let you restrict views to logged-in (or logged-out) users.

However, it does not:

  • Impose a particular database or other storage method on you. You are entirely in charge of how the user is loaded.
  • Restrict you to using usernames and passwords, OpenIDs, or any other method of authenticating.
  • Handle permissions beyond “logged in or not.”
  • Handle user registration or account recovery.

Install

npm install --save connect-login

Usage

You can allso see a full example here.

var connectLogin = require('connect-login');
var loginManager = connectLogin();

loginManager.anonymousUser(function(){
	return {username: null};
});


loginManager.userLoader(function(user_id, next){
	myDB.lookup(user_id, function(err, user){
		next(err, user);
	});
});

loginManager.userSerializer(function(user){
   return user.username;
});


app.use(loginManager);

Connect Login middleware for NodeJS

API

User objects

When configuring connect-login the user argument must only have one property user.username if user.username is null or undefined, the user is assumed to be anonymous.

`connectLogin(options)

var connectLogin = require('connect-login');
var loginManager = connectLogin(options);

options.loginPath

Url to redirect users to when a login is required. defaults to /login

loginManager

The login manager is middleware that must be used by the app.

loginManager.userLoader(loadFunction)

The loadFunction function must accept the arguments loadFunction(user_id, callback) and where callack must be called with callback(err, user)

loginManager.userSerializer(storeFunction)

The storeFunction function must user object from the userLoader and return a user_id that can be used to store in a session cookie user_id = storeFunction(user)

loginManager.anonymousUser(anonymousFunction)

anonymousUser allows an anonymous user to be created and used when a user is not logged in.

anonUser = anonymousFunction()

connectLogin.loginRequired

This middleare can be used in a route to require that the user be logged in to view it. If the user is not logged in a redirect to options.loginPath is sent

app.get('/private', connectLogin.loginRequired, function(req, res){
	res.send("Shhh!");
});

Added functions to the req object

Once the loginManager is used. all req objects will have the following methods

req.login(user, rememberMe)

This can be used from within a /login route so you can set a user as being logged in.

var user = myUserLoader(some_username);
req.login(user);

req.logout()

Log out the current user

req.user

The currently logged in user

req.loginManager

The current login manager

node-connect-login's People

Contributors

srossross avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.