@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.
159 lines (144 loc) • 4.24 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 userSchema = new Schema({
username: {
type: String,
required: true,
index: { unique: true, sparse: true },
},
email: {
type: String,
required: true,
trim: true,
lowercase: true,
index: { unique: true, sparse: true },
//required: 'Email address is required',
validate: [validateEmail, 'Please enter 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 enter a valid phone number'],
match: [phonePattern, 'Please enter a valid phone number'],
},
status: {
type: String,
enum: ['Enabled', 'Disabled', 'Pending'],
default: 'Enabled',
},
regtype: {
type: String,
description:
'Registration type. E.g.: the registration source. You can enter \'admin\' if creating account from admin portal.',
},
since: { type: Date, default: Date.now },
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 userBrief =
'username[User Name] email phone firstname[First Name] lastname[Last Name] since regtype status';
const userDetail =
'username[User Name] email phone firstname[First Name] lastname[Last Name] since regtype status';
const userCreat =
'username[User Name] email phone firstname[First Name] lastname[Last Name] regtype status password';
const userEdit =
'username[User Name] email phone firstname[First Name] lastname[Last Name] regtype status';
const userTextSearch = 'username[User Name] email phone';
const userIndex = 'username';
const schemas = {
muser: {
schema: userSchema,
views: [
userBrief,
userDetail,
userCreat,
userEdit,
userTextSearch,
userIndex,
],
tags: ['auth-user'], // used as authentication 'user' model
name: 'User',
api: 'LRCUM', // M - email
mraUI: {
listWidgets: {
general: {
views: ['table', 'list', 'grid'],
},
select: {
views: ['table', 'list'],
},
sub: {
views: ['table', 'list'],
},
},
listWidgetTypes: {
general: 'general',
select: 'select',
sub: 'sub',
},
widgetCustomTemplates: {
'list-view': {
// widgit category
table: {
// widgit name
css: undefined,
html: '../../../users-cust/cust/muser-list-view-widget-table.component.html',
},
list: {
css: undefined,
html: '../../../users-cust/cust/muser-list-view-widget-list.component.html',
},
grid: {
css: undefined,
html: '../../../users-cust/cust/muser-list-view-widget-grid.component.html',
},
},
},
},
mraBE: {
valueSearchFields: ['regtype'],
enableHistory: true,
},
},
};
const dateFormat = 'MM-DD-YYYY';
const timeFormat = 'hh:mm:ss';
const config = {
dateFormat: dateFormat,
timeFormat: timeFormat,
};
const authn = {
authUserSchema: 'muser',
authUserFields: 'username email',
authPasswordField: 'password',
authProfileFields: 'firstname lastname phone email',
};
const authz = {
'module-authz': { LoginUser: { others: '', own: 'RU' }, Anyone: '' },
muser: { LoginUser: { others: '', own: 'RU' }, Anyone: '' },
};
const DB_CONFIG = {
APP_NAME: process.env.APP_NAME,
MODULE_NAME: 'AUTH',
};
module.exports = {
schemas: schemas,
config: config,
authn: authn,
authz: authz,
DB_CONFIG,
};