###Motivation
Express.js is awesome, but getting user accounts and authentication up and running with a relational backend is kind of a pain.
The goal of this project is to create a fast, lightweight, and extensible library for creating and handling user accounts.
This is not meant to be a comprehensive ORM solution, but it is meant to create a basic, clean users schema that can be extended and used with other front ends.
###Set up your schema
node ./script/setup-schema.js [-s <path to schema.json>]
var UsersAuth = require('user-auth');
UsersAuth.initialize({
});
UsersAuth.register(app);
Make sure to register express-session before users-auth
function(req, res, next) {
req.app.userAuth.loginWithEmail(email, password)
.then(function(newUser) {
//User is logged in
})
.catch(function(error) {
//UserNotFoundError
//InvalidCredentialsError
//MissingValueError
//InvalidValueError
});
}
function(req, res, next) {
req.app.userAuth.logout()
.then(function() {
//Logged out
})
.catch(next);
}
function(req, res, next) {
var currentUser = req.app.userAuth.currentUser;
if (currentUser) {
//Do something with logged in user
} else {
//Not logged in
}
}
var userData == {
email: 'email or username required',
username: 'email or username required',
password: 'required',
role: 'default determined by datastore',
<other fields in user schema>
};
req.userAuth.signupUser(userData)
.then(function(newUser) {
//User is logged in
})
.catch(function(error) {
//EmailTakenError
//status: 409
//InvalidFieldError
//MissingFieldError
});
Basic authentication middleware is provided.
app.get('/users/:userId', UserAuth.restrictToLoggedIn({redirect: '/login'}), handleResponse);
app.get('/users/:userId', UserAuth.restrictToRole({role: 'admin', redirect: '/unauthorized'}, handleResponse);
var user = UserAuth.getUserById(userId);
//Update consists of fields on the user table in your datastore.
//Errors will be thrown if you try to put in an invalid value.
UserAuth.updateUser(userId, update);
UserAuth.deleteUser(userId);