user-managements-node-server
v2.0.1
Published
starter for express node server with user managements, authentication authorization
Downloads
11
Maintainers
Readme
README
What is this repository for?
This repository is node server to managements users on back-end side
This repository has the following APIs
sign-up:
- Sending activation link to to (user email that sign up / or to admin if verifyUserBy set to true)
- route: /user/sign-up
- body:
{ username: '[email protected]', password: 'user-password' }
- method: 'POST'
- response:
- json:
http-status: 200 OK{ message: 'User create, email verification sent' }
verify-user
- Verify user by clicking on activation link
- The linked that sent with an email
- route: /user/verify/:actionId
- method: 'GET'
- response: HTML that says if activation succeeded or faild
sign-in
- Sign in by user credentials or by thired party like (Facebook / Google) if loginWithThirdParty set to true return token that needs to be send each other api call to identify the user
- route: /user/sign-in
- body:
- a.
Or in case sign in via thired party (Facebook/Google){ username: '[email protected]', password: 'user-password' }
- b.
{ username: '[email protected]', password: 'thired-party-token-reponse', thireParty: 'FACEBOOK' | 'GOOGLE' }
- a.
- method: 'POST'
- response:
- json:
http-status: 200 OK{ token: 'generated-token' }
** The above response should be send in an header of each other requests (via interceptor or other)
forgot-password
- Forgot password will send to the email link (changePasswordUrl + actionId that set in config)
- route: /forgot-password
- body:
{ username: '[email protected]' }
- method: 'POST'
- reaponse:
- json:
http-status: 200 OK{ message: 'Password reset link sent to your Email' }
- Email will be send to the user with a link to change password, this link will combine actionId as a part of thr URL
change-password
- Change password - post to /user/change-password/{actionId} with { password } as payload
- route: /user/change-password/:actionId
- body:
{ password: 'new-password' }
- method: 'POST'
- response:
- json:
http-status: 200 OK{ message: 'Password successfully changed' }
get user info /user/info (token needs to be send in header)
- get user info
- route: /user/info
- method: 'GET'
- header:
{ token: 'generated-token' }
contact-us
- Sending email to that admin user (admin user in config file) (token needs to be send in header)
- route: /user/contact-us
- body:
{ username: '[email protected]', subject: 'contact-us-subjet', description: 'contact-us-description' }
- method: 'POST'
- response: email with the payload will be sen to the adminEmail
version: 1.0.7
How do I get set up?
- yarn add user-managements-node-server / npm --save user-managements-node-server]
- This module use Sequelize so, it supports the following databases
- Create one of the following database 'postgres'|'mysql'|'sqlite'|'mssql'
- Create database login user
- Database Tables - You can create it manually or use the 'dbcreate' script below :
- Table: ActionVerifications
ActionVerifications: {
actionId: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: () => uuid.v4()
},
username: {
type: DataTypes.STRING,
allowNull: false
},
actionType: {
type: DataTypes.INTEGER,
allowNull: false
},
deleted: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}
Tokens: {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: () => uuid.v4()
},
userId: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true
},
ip: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
token: {
type: DataTypes.STRING,
allowNull: true
},
userAgentIdentity: {
type: DataTypes.STRING,
allowNull: false
}
}
- Table: Users
Users:
{
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: () => uuid.v4()
},
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.TEXT
},
fullName: {
type: DataTypes.STRING,
allowNull: true
},
token: {
type: DataTypes.STRING,
allowNull: true
},
fbToken: {
type: DataTypes.TEXT,
allowNull: true
},
googleToken: {
type: DataTypes.TEXT,
allowNull: true
},
profilePhoto: {
type: DataTypes.STRING,
allowNull: true
},
isValid: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
termsOfUse: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
}
- Table: Audit
Audit: {
id: {
type: DataTypes.UUID,
allowNull: false,
primaryKey: true,
defaultValue: () => uuid.v4()
},
username: {
type: DataTypes.STRING,
allowNull: false
},
actionName: {
type: DataTypes.STRING,
allowNull: false
},
requestParams: {
type: DataTypes.TEXT,
allowNull: true
}
}
- Edit the following config with the above settings
- Configuration:
- Setup the following config file: [config.js]
{
"verifyUserBy": "ADMIN", // "ADMIN" | "AUTO" : "EMAIL"
"verify3rdPartyUserBy": "AUTO", // "ADMIN" | "AUTO" : "EMAIL"
"useSingleToken": false, // true - Multiple tokens for diffrent devices
"appName":"<Your application name>", // The appName will be uses when sending emails to user/admin
"port": 5000,
"database": {
"name": "user-managements-db",
"username": "user-managements-username",
"password": "user-managements-password",
"settings": {
// Database location
"host": "localhost",
// You can use one of the the foolowing databases
"dialect": " mysql | mariadb | sqlite | postgres | mssql ",
// In case you use sqlite storage file path is required
"storage": "/tmp/user-managements.sqlite",
"pool": {
"max": 5,
"min": 0,
"idle": 10000
},
// To log database queries set the following to true
"logging": false
}
},
// The following email setting are require if you want the system to send emails to users
"email": {
"service": "<Email Service>",
"user": "<Email Username>",
"pass": "<Email Password>",
"from": "<Email From>"
},
"verificationUrl": "http://your-web-site-domain/user/verify/",
"changePasswordUrl": "http://your-web-site-domain/change-password/",
"loginUrl": "http://your-web-site-domain/login",
"tokenHash": "dev-token-key",
"adminEmail": "<admin@your-domain>",
"log4js": {
"appenders": { "app": { "type": "file", "filename": "/var/log/your-app-name/app-name.log" } },
"categories": { "default": { "appenders": ["app"], "level": "all" } }
},
"templates": {
"activation":{
"body" : "./email-templates/activation/body.html",
"subject" : "./email-templates/activation/subject.html"
},
"approved":{
"body" : "./email-templates/approved-activation/body.html",
"subject" : "./email-templates/approved-activation/subject.html"
},
"notify":{
"body" : "./email-templates/notify-admin-when-user-created/body.html",
"subject" : "./email-templates/notify-admin-when-user-created/subject.html"
},
"activationResponse" : "./email-templates/activation/response.html"
},
"userRoutePrefix": "/user"
}
Run: yarn dbcreate Or npm run dbcreate - This script will create the relevant tables
Run the server:
Please see example how to run this module: run-user-managements-node-server-exp
Templates - any of the following templates can be edited with your own HTML template I've choose to work with dot module. You need to build your custom template with dot syntax. For example the default activationBody look like
export default `<!doctype html> <html> <head> <meta charset='utf-8'> <title>{{=it.appName}}</title> <style type='text/css'> body { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; font-family: sans-serif; } </style> </head> <body> <div> <div>Hello, {{=it.username}}</div> Thank you for registration our app, to finish activation please click <a href='{{=it.verificationUrl}}{{=it.actionId}}'>here</a> </div> </body> </html> `
1. activationBody
Exposed fields:
a. appName - appName from config
b. username - current handled username
c. verificationUrl - verificationUrl from config
d. actionId - current handled actionId
2. activationSubject
Exposed fields:
a. appName - appName from config
3. activationResponse
Exposed fields:
a. appName - appName from config
b. error - boolean indicator
c. link - loginUrl from config
4. activationBodyApproved
a. appName - appName from config
b. username - current handled username
c. loginUrl - loginUrl from config
5. activationSubjectApproved
a. appName - appName from config
6. notifyBodyAdminWhenUserCreated
a. appName - appName from config
b. admin - adminEmail from config
c. username - current handled username
d. fullName - current handled user's fullName
7. notifySubjectAdminWhenUserCreated
a. appName - appName from config
b. username - current handled username
* Haim Rubin
* [email protected]
- Please let me know on any issue