This project is archived/deprecated. Use auth3000 instead.
A really simple Node.js Authentication library.
This library is not a suggestion of the right way to do anything, or advice on how you should do something. It's just how I start out doing authentication on some of the apps I build.
usersrequired Provide an Array of user objects. If you have users in a database, or a custom implementation, take a look atusers.js. The default implementation is just providing two async functions,all()andfind(email). Provide an object with the expected functions and behavior to use your existing user database.signinTimeoutTime in milliseconds for a sign in request to expire. This is the time from when a user initiates the sign in process until they must complete it to avoid the sign in epxiring. Defaults to 10 minutes.sessionTimeoutTime in milliseconds for a session to expire. This is how long a token is valid for beginning with when it was issued. Sessions won't expire unless this is provided.
dirIf using built-in sessions, this is the directory to keep the session details in. Defaults to./.authentication/.sessionsIf built-in sessions is not to be used, override it by providing an object here. Take a look atsessions-fs.jsto see what functions must be present and how they should behave.
Configure these options if you are using the built-in email function
lib/email.js.
namerequired The name of the app.envEmails are not sent indevelopment. Defaults todevelopment.domainrequired Domain of the app.protoThe protocol of the app. Used by the built-in email delivery function to createsigninLink.
These options are passed directly to Nodemailer, so take a look at those docs if you're using the built-in email delivery function.
smtprequired Nodemailer optionssmtp.hostrequired SMTP hostsmtp.portSMTP portsmtp.secureSMTP secure (boolean)smtp.userSMTP usersmtp.passSMTP pass
Or pass in your own email function as opts.email.
signinTokenA shorter token that's generated when a user starts the sign in process. This will also serve as the session identifer.authorizationTokenA longer token that is generated when a user claims their token.tokenThe token used for authentication.
Async functions provided by the returned object that are used in an implementation.
signin({ email, attrs })Start the sign in process. Email the user a signinToken.exchange({ signinToken, attrs, [session] })Complete the sign in process. Exchange a signinToken for an authorizationToken. Resolves the session and user. Sometimes you're doing things that require looking up the session before you exchange. If you already have the session, you can pass it along.verify(token)Verify a token. Resolves the session and user. Throws errors for all failures. Catch!signout(signinToken)Destroy the session associated with this token.
This library will throw these error codes. Watch for them and reveal to the client what you choose to.
ERR_USER_NOT_FOUNDUser not found.ERR_SIGNIN_EXPIREDSign In expired.ERR_SESSION_EXPIREDSession expired.ERR_SESSION_NOT_FOUNDSession not found.ERR_SESSION_CLAIMEDSession already claimed.ERR_INVALID_TOKENInvalid token.ERR_NOT_CLAIMEDThis session was never claimed. We should never get this error.