Express User Model
This is a library to provide a user backbone with basic requirements to start with. It assumes you are using mongoose with the driver for Mongoose. This plugin will add following fields to your User Model.
- username
- first_name
- last_name
For password storage we recommend passport-local-mongoose with passport though you are free to use any required.
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install express-user-model
Usage
Create a user schema first and use the plugin as below.
var mongoose = ;var Schema = mongooseSchema;var EUM = ; const UserSchema = {};UserSchema;
Optionally, To add authentication and password fields, you can use passport-local as below.
const passportLocalMongoose = ;UserSchema;
If you want to use Email verification and email management only , you can use the following code instead of whole EUM plugin as:
UserSchema;
For basic features of first_name, last_name and username only, you can use as:
UserSchema;
Motivation
Coming from Django background, I thought of bringing all user related features to one place.
API Reference
Options:
You can pass options to the plugin as
UserSchema;
Main options:
- usernameField:- Default username field to create ,Default value is username
- usernameUnique:- Default value: true
Let us consider following scenario.
var user = username: '12345';
Basic:
Fields:
userusername = '12345'; userfirst_name = 'Amrit';userlast_name = 'Ghimire';
Methods:
user // Returns the full name of user account (Amrit Ghimire)
Fields:
useremails = address:''verified:falsetoken:''; // An array of subdocument each with address,verified and tokenuseremail = '' // To store primary email
Methods:
Add email:
user; // Add an email to user accountuser;
Returns false if email already exists else return the email sub-document.
GetEmail:
var email = user // Get email object// { address: ,verified: , token: }
Return email objects if exists
setPrimaryEmail
user; // Set email as primary key
Sets email as primary email and it will throw an error if the email is not in users account. Will return new primary email otherwise.
getPrimaryEmail
user; // Will return primary Email
Primary key will be either the first email or the email set as primary key exclusively. Will return false if there is no primary key.
sendVerificationToken
user;
The call back function will be responsible for sending the token. The call back will receive parameter in one of these 3 formats.
- email, token : In successful scenario
- null, null : If email is already verified.
- email, null : If email is not found or token is not found.
For verification of the token, use the middleware as:
app;
Verify middleware is passed with the User Model. If the verification code cause some error, standard error is raised.
Tests
For development purpose, you can clone this repository and run test with following commands.
make test
You can view test.js
file under test
directory for some example usage of the library.
Contributors
All bugs, feature requests, pull requests, feedback, etc., are welcome. Create an issue.
License
This library is licensed under MIT License