npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

just-login-example-session-manager

v1.2.1

Published

Server code for the Just Login module

Downloads

6

Readme

just-login-example-session-manager

Build Status

Run this on your server with just-login-core@1.

Deprecation Notice

This module is deprecated. just-login-session-state replaced it in just-login-core@2.

Example

//Require the modules you'll need
var level = require('level')
var SessionManager = require('just-login-example-session-manager')
var Core = require('just-login-core')

//Construct your objects
var db = level('./storage')
var sessionDb = level('./session-storage')

var core = Core(db)
var mngr = SessionManager(core, sessionDb)

//Give the session manager's methods to your client here...

Now you've got your session manager, but you need your clients to have sessions. Give your clients the session manager.

//Get the session manager's methods from the server here...

//Lets make a function for aquiring a session
function establishSession(cb) {
	var session = localStorage.getItem('session')
	mngr.continueSession(session, function (err, api, sessionId) {
		if (!err) {
			cb(err, api)
		} else {
			mngr.createSession(function (err, api, sessionId) {
				if (!err) {
					localStorage.setItem('session', sessionId)
				}
				cb(err, api)
			})
		}
	})
}

establishSession(function (err, api) {
	if (err) throw err
	
	//right here you can do stuff with the api
	//the documentation for the api is below
})
	

API

var SessionManager = require('just-login-example-session-manager')

var mngr = SessionManager(core, sessionDb, [opts])

This is the only function/method that should be called from the server.

  • core is a just-login-core object
  • sessionDb is a level database object
  • opts is an object for your options. Optional.
    • timeoutMs is a property of opts that sets the session's life. Optional, defaults to 1 day (86400000).
    • checkIntervalMs is a property of opts that sets the interval between session death checks. Optional, defaults to 1 second (1000).

mngr.createSession(cb)

The method you call on the client to create a new session.

  • cb is a function that is called on completion, with the following arguments:
    • err should be falsey or an error object
    • api should be the functions you need for basic authentication. See API Methods below.
    • sessionId should be a string
mngr.createSession(function (err, api, sessionId) {
	if (!err) {
		console.log(api)
		//logs: { beginAuthentication: [Function], isAuthenticated: [Function], unAuthenticate: [Function] }
		console.log(sessionId)
		//logs the session id string, e.g. '64BDA9CC-66A2-11E4-96D1-3BA1DFC16A55'
	} else {
		console.log("error:", err.message)
	}
})

mngr.continueSession(sessionId, cb)

The method you call on the client to attempt to use your old session.

  • sessionId is a string
  • cb is a function that is called on completion, with the following arguments:
    • err should be falsey or an error object
    • api should be the functions you need for basic authentication. See API Methods below.
    • sessionId should be a string
mngr.continueSession(sessionId, function(err, api, sessionId) {
	
	if (err) { throw err }
	console.log(api) //=> { beginAuthentication: [Function], isAuthenticated: [Function], unAuthenticate: [Function] }
	console.log(sessionId) //=> '64BDA9CC-66A2-11E4-96D1-3BA1DFC16A55'
})

api

Once you have successfully established a session, you are given a few api methods.

The methods createSession() and continueSession() each have the argument api in their callbacks. You call these methods on your client to do authentication stuff on the server.

api.isAuthenticated(cb)

Checks if a user is authenticated. (Logged in.)

  • cb is a function with the following arguments:
    • err is falsey if there was no error, and is an Error object if there was an error.
    • contactAddress is falsey if the session isn't authenticated, and is a string of their contact address if they are authenticated.
api.isAuthenticated(function(err, contactAddress) {
	if (!err) console.log(contactAddress)
	//for an authenticated user, logs: [email protected]
	//for an UNauthenticated user, logs: null
})

api.beginAuthentication(contactAddress)

  • contactAddress is string of the user's contact info, (usually an email address).

The just-login-core will emit the event, 'authentication initiated' when this is called. If you have a listener on that event, you can make it send a message to the contactAddress.

//This is on the client
mngr.beginAuthentication("[email protected]")
//This is on the server, but can be handled by the just-login-emailer
core.on('authentication initiated', function(authInit) { // Note that this is the core, not the sessionManager
	console.log(authInit.token)     //logs the token
	console.log(authInit.sessionId) //logs the session id
})

You can use the just-login-emailer to catch the event.

api.unauthenticate(cb)

Logs out a session.

  • cb is a function with the argument err. Defaults to noop: function(){}.
    • err is either falsey or an error object.

You can write half a dozen lines of code:

api.unauthenticate(function(err) {
	if (err) {
		console.log("you already weren't logged in\nerror:", err.message)
		//this is expected for unauthenticated sessions
	} else {
		console.log("you have been logged out")
		//this is expected for authenticated sessions (previously logged in)
	}
})

Or you can write one line of code:

api.unauthenticate() //the callback is a noop function

Install

Install them with npm:

npm i just-login-core
npm i just-login-session-state
npm i just-login-example-session-manager

License

VOL