umpack
user management pack for express framework app.
you can use this package in typescript too.
Install Guide
Install npm package
npm install umpack-express -S -E
Set Options and Router(express app)
- accessTokenExpiresIn time span string description
var umpack = mongodbConnectionString: 'mongodb://172.17.7.92:27017/umpack' accessTokenSecret: 'myrandomstring' passwordHashSecret: 'mypasswordsecret' accessTokenExpiresIn: '1m' cookieAccessTokenName: 'accessToken' passwordResetData: smtpData: host: 'smtp host' port: 'smtp port. optional' user: 'username for logging into smtp' password: 'password for logging into smtp' timeout: 5000 // number of milliseconds to wait. default 5000 ssl: false //boolean or object with fields: key, ca, cert. default false senderEmail: 'sender@email.com' resetKeyExpiresIn: '2h' //password reset key expiration { return 'message to send. use key. for example: http://example.com?key=' + key; } { return 'someone with ip: ' + clientIp + ' requested password reset on the site example.com'; //message to send to input email, when user with input email does not exist } passwordResetPhoneData: resetKeyExpiresIn: '2h' { // send sms to the phone. // return promise or nothing. } deviceControl: false // default false. if it is true, user's devices access is controlled userNameCaseSensitive: false // if it is true, userName is case sensitive, if false - it is not. logger: loggerObject // loggerObject should have methods: error, warn, info, debug and trace. it should have logging level restriction itself. // by default logger field is logger object that logs only warnings and errors. activateOnSignup: false // if true, when user signs up account doesn't need to activate userDefaultRole: 'user' //not works if activateOnSignup is false. on signup user has this role;//.....app;//.....
umpack API Methods
This methods should be called without authorization header
Login
POST : baseurl/loginrequest - data/body : userName: 'user' email: 'user@test.com' // userName or email is required password: 'userpassword' deviceToken: 'device token' //required if device control is enabledresponse - 'user access token'
Signup
POST : baseurl/signuprequest - data/body : userName: 'user' //required password: 'userpassword' //required firstName: 'first name' lastName: 'last name' email: 'user@test.com' phone: '123456' address: 'usa/de' additionalInfo: 'user additional info' response - success: true message: 'Thanks for signUp'
Next methods requires authorization header (access token).
headers:'authorization': 'user access token'
Password Reset
POST : baseurl/resetpassrequest - data/body : userName: 'admin' oldPassword: 'admin' newPassword: '123456789'response - success: true message: 'Password Reset Done'
Get all users
GET : baseurl/usersresponse - id: '34jhb5jh45b6' userName: 'user name' isActivated: 'true/false' roles: 'admin''provider''root''etc.'
Get all roles
GET : baseurl/rolesresponse - name:'admin' description: ''name:'user' description: ''name:'provider' description: ''name:'root' description: ''name:'organizationUser' description: ''
Update user status (Activate / Deactivate)
POST : baseurl/updateUserStatusrequest - data/body : id: 'user id' isActivated: true/false response - id: 'user id' isActivated: 'true/false' userName: 'user name' roles: 'admin''provider''root''sys''etc.'
Update user roles (assigne or remove role from user)
POST : baseurl/updateUserRolesrequest - data/body : userId: 'user id' roleName: 'admin' enable: 'true/false' response - id: 'user id' isActivated: 'true/false' userName: 'user name' roles: 'admin''provider''root''sys''etc.'
Get User Object
GET : baseurl/users/userIdresponse - id: '' userName: 'name' firstName: 'firstName' lastName: 'lastName' email: 'test@email.com' phone: '' address: '' additionalInfo: '' isActivated: true/false roles: 'user' 'admin' metaData: {}
Get User Object By userName
GET : baseurl/users/userName/fullresponse - id: '' userName: 'name' firstName: 'firstName' lastName: 'lastName' email: 'test@email.com' phone: '' address: '' additionalInfo: '' isActivated: true/false roles: 'user' 'admin' metaData: {}
Change User's userName
PUT : baseurl/users/userId/usernamerequest - data/body : userName: 'userName'response - success : true
Change User Info
PUT : baseurl/users/userId/inforequest - data/body : firstName: '' lastName: '' email: '' phone: '' address: '' additionalInfo: ''response - success : true
Delete User
DELETE : baseurl/users/userIdresponse - success: true
Lost Password Reset Request
POST : baseurl/users/passwordResetRequestrequest - data/body : email: 'test@email.com'response - success : trueinstructions are sent to the email
Lost Password Reset
POST : baseurl/users/passwordResetrequest - data/body : resetKey: '' //password reset key sent to the email newPassword: 'password'response - success : true
Lost Password Reset By Phone Request
POST : baseurl/users/userName/passwordResetRequestByPhonerequest - data/body : {} //empty objectresponse - success : truepassword reset key is sent to the user phone
Lost Password Reset By Phone
POST : baseurl/users/userName/passwordResetByPhonerequest - data/body : resetKey: '' //key sent to the phone newPassword: 'password'response - success : true
Get User's All Registered Devices
GET : baseurl/users/userName/devicesresponse - deviceToken: 'token' canAccess: true/false lastUsageDate: //last usage date
Get User's All Permitted Devices
GET : baseurl/users/userName/devices/permittedresponse - deviceToken: 'token' canAccess: true lastUsageDate: //last usage date
Grant User's Device Access
POST : baseurl/users/userName/devices/accessrequest - data/body : deviceToken: 'device token'response - success: true
Restrict User's Device From Access
POST : baseurl/users/userName/devices/restrictionrequest - data/body : deviceToken: 'token'response - success: true
Get metadata
GET : baseurl/metadataresponse - metadata object
Update metadata
PUT : baseurl/metadatarequest - data/body : metadata objectresponse - success: true message: 'metadata updated'
Set metadata field
PUT : baseurl/metadata/fieldNamerequest - data/body : value: 'some value of any type'response - success: true message: 'metadata key: {fieldName} updated'
Create New Role
POST : baseurl/rolesrequest - data/body : name: 'admin' description: 'description'response - success: true
Get Role Full Object
GET : baseurl/roles/roleNameresponse - name: 'admin' description: 'description' actions: id: '464sadfsdf6' pattern: '/api/*' name: 'action name' verbGet: true verbPost: true verbPut: true verbDelete: true verbHead: true
Change Role's name and description
PUT : baseurl/roles/roleNamerequest - data/body : name: 'role name' description: 'role description'response - success: true
Delete Role
DELETE : baseurl/roles/roleNameresponse: success: true
Permit Action to Role
POST : baseurl/roles/roleName/actionsrequest - data/body : pattern: '/api/*' name: 'name' verbGet: true verbPost: true verbPut: true verbDelete: true verbHead: trueresponse - success: true actionId: 'action id'
Edit Role's Action
PUT : baseurl/roles/roleName/actions/actionIdrequest - data/body : pattern: '/api/something' name: 'name' verbGet: true verbPost: true verbPut: true verbDelete: false verbHead: falseresponse - success : true
Remove Permitted Action From Role
DELETE : baseurl/roles/roleName/actions/actionIdresponse - success: true
Umpack Initialization.
- saves root user and admin role if they do not exist.
- if device control is enabled, it saves one permitted device of the root for administration.
POST : baseurl/initializationrequest - data/body : umBaseUrl: '/um' deviceToken: 'token' //not required if device control is disabled password: '123' // password for root user. optional. if it isn't passed new password is generated randomly.response - success: true password: 'password' //generated or parameter password for root user
Authorization Route
it is used for validating access token
HEAD : baseurl/authorization
API Response Internal Statuses
- Every response with status 400/401 has also internal status for example :
message:User Is Not Activated internalStatus:601
- All internal status
code: 601 message: 'User Is Not Activated' code: 602 message: 'User Name Or Email Already Exists' code: 603 message: 'Wrong User Name Or Password' code: 604 message: 'Wrong Password' code: 605 message: 'User Does Not Exists' code: 606 message: 'Can\'t Find JWT Token Inside The Request Header' code: 607 message: 'Invalid JWT Token' code: 608 message: 'Token Expired' code: 609 message: 'Access Denied' code: 701 message: 'Wrong Role Name' code: 702 message: 'Role Already Exists' code: 703 message: 'Invalid Action Pattern' code: 704 message: 'Action Pattern Already Exists' code: 800 message: 'password reset key is expired' code: 801 message: 'password reset key is invalid' code: 802 message: 'password reset by email is not supported' code: 803 message: 'password reset by phone is not supported' code: 804 message: 'invalid phone number' code: 805 message: 'invalid device token' code: 806 message: 'access is denied for your device' code: 807 message: 'devices control is not supported' code: 900 message: 'invalid userName' code: 901 message: 'invalid email'
Use Authorization Middleware
- if user is not authorized then response status is 401
- if user has no access right then response status is 403
- if device control is enabled and user's device has no access right then response status is 403 too
- if response status is 401 or 403 response body is object with error message and internalStatus
{ message: err.message, internalStatus: err.internalStatus }
var umpack = ; router;
User's Metadata Management
- if you need to add additional info, attribute, etc. you can use user's metadata to manage it.
- metadata is custom field/subdocument of user doc which can contains any kind of object.
- example :
assigne/update user metadata
var organizationInfo = organizationId: '2222' organiationName: 'bbbbb' organizationTaxCode: '777777' ; umpack ;
get user metadata by user name
router;
get user metadata by request
router;
Filter users by metadata param
router;
Get User Full Name
router;
Get User Roles By User Name
router;
Get User Roles From Request
router;
Filter users by role
router;
Get Full User Object
router;
Get Full User Object From Request
router;
Initialize Umpack
- saves root user and admin role if they do not exist.
- if device control is enabled, it saves one permitted device of the root for administration.
router;
Initialize Umpack With Full Api Access to admin Role
- saves root user and admin role if they do not exist.
- saved admin role has permission of everything.
- if device control is enabled, it saves one permitted device of the root for administration.
router;
Get UserName From Request
router;
Signup method
router;