@hicoder/express-auth-server
Version:
Model Driver Development Stack - authentication and authorization server for mongoose and express based application. It can be enabled to work as authentication, user profile managment, and authorization management servers.
101 lines (91 loc) • 3.27 kB
JavaScript
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const phonePattern = /\(?\d{3}\)?-? *\d{3}-? *-?\d{4}/
const validatePhone = function (phone) {
return phonePattern.test(phone);
};
const emailPattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
const validateEmail = function (email) {
return emailPattern.test(email);
};
const accountSchema = new Schema({
username: { type: String, required: true, index: { unique: true, sparse: true } },
email: {
type: String,
trim: true,
lowercase: true,
index: { unique: true, sparse: true },
//required: 'Email address is required',
validate: [validateEmail, 'Please fill a valid email address'],
match: [emailPattern, 'Please enter a valid email address'],
mraEmailRecipient: true, // if this email can be used by sendEmail Action
},
phone: {
type: String,
trim: true,
index: { unique: true, sparse: true },
validate: [validatePhone, 'Please fill a valid phone number'],
match: [phonePattern, 'Please enter a valid phone number'],
},
status: {type: String, enum: ['Enabled', 'Disabled', 'Pending'], default: 'Enabled'},
since: { type: Date, default: Date.now },
regtype: {
type: String,
description:
'Registration type. E.g.: the registration source. You can enter \'admin\' if creating account from admin portal.',
},
password: { type: String, required: true, minlength: 6 },
firstname: {type: String, maxlength: 100},
lastname: {type: String, maxlength: 100},
photo: {type: String,
mraType: 'picture', mraSharable: false},
description: {type: String, textarea: true},
});
const accountBrief = "username[User Name] email phone firstname[First Name] lastname[Last Name] since regtype status";
const accountDetail = "username[User Name] email phone firstname[First Name] lastname[Last Name] since regtype status";
const accountCreat = "username[User Name] email phone firstname[First Name] lastname[Last Name] regtype status password";
const accountEdit = "username[User Name] email phone firstname[First Name] lastname[Last Name] regtype status";
const accountTextSearch = "username email phone";
const accountIndex = "username";
const schemas = {
"maccount": {
schema: accountSchema,
views: [accountBrief, accountDetail, accountCreat, accountEdit, accountTextSearch, accountIndex],
tags: ['auth-user'], // used as authentication 'user' model
name: 'Account',
api: 'LRCUDM', // M - email
mraUI: {
listWidgets: {
general: {
views: ['table', 'list', 'grid',],
},
select: {
views: ['table', 'list',],
},
sub: {
views: ['table', 'list',],
}
},
listWidgetTypes: {
general: 'general',
select: 'select',
sub: 'sub',
},
},
mraBE: {
valueSearchFields: ['regtype'],
enableHistory: true,
},
},
};
const authn = {
authUserSchema: "maccount",
authUserFields: "username email phone",
authProfileFields: "firstname lastname phone email",
authPasswordField: "password",
};
const DB_CONFIG = {
APP_NAME: process.env.APP_NAME,
MODULE_NAME: 'AUTH',
};
module.exports = {schemas: schemas, authn: authn, DB_CONFIG};