All-inclusive feature reach Authentication service with Node.js, Express.js and PostgreSQL. No other libraries are needed.
When considering complex authentication scenarios, one might initially think of using solutions like Keycloak or cloud-based services such as Auth0. However, this app demonstrates that this might not be necessary. It offers a comprehensive authentication solution based on Node.js and Express.js, without relying on additional modules like Passport.js or Object-Relational Mapping (ORM) tools.
The article with explanation of the advanced features
You can see this app in action at www.authdemoapp.com
Frontend part of the app can be found here
- Create a new user
- Login with email and password
- 'Remember me' feature to stay logged in
- Logout from the current session
- Delete your own account
- OAuth via Facebook or Google
- Email verification
- Access based on the email verification status
- Change the current password
- Reset the forgotten password
- As admin request a user to change the password
- As admin block a user
- As admin request a user to re-login
- Send the link to restore the deleted account
- Link local account with OAuth accounts
- Prevent some user roles to use OAuth
- Logout from OAuth providers
- 2FA including 2FA with OAuth
- Extending JWT for active users
- Auto logout with timeout counter
- Advanced role based access control
- One login only
- Logout from all devices
- Login as another user
- Require password check for the critical actions
- Sync auth status across browser tab
- Rename
.env.example
to.env
- Using the instructions below fill in
.env
file with your own data - Update
LOCAL_DOMAIN
const in the filesrc/utils/const.js
with the host where frontend request come from
- Setup environment variables in your CI/CD pipleline from the
.env
using the instructions below - Update
DOMAIN
const in the filesrc/utils/const.js
with your production domain name
- Create an account in SendGrid
- Create and save the API key
- Replace
SENDGRID_API_KEY
in.env
file to yours - Create Dynamic Templates and import respective templates from
emailTemplates
folder of this repository. You may only import*_en
templates if you do not want to send email in German. - Replace template IDs in
src/utils/email_const.js
to yours
- Each OAuth provider has its own way to obtain the
client id
and theclient secret
. But usually the process is straightforward. Please refer to the documentation of respective provider. After obtaining theclient id
and theclient secret
put them in.env
file in the respective sections. - When configuring the app with the respective provider you need to provide the return url.
The return url looks like this: http://localhost:4000/auth/oauthCallback/linkedin where
http://localhost:4000 is the base url and
linkedin
is the OAuth provider name. For the Facebook, it isfacebook
and for the Google it isgoogle
. Pathauth/oauthCallback/
is common for all the providers. - If you want to add another provider you will be able to see the pattern in the source code. Make sure you implement the
OpenID Connect
flow.
- Install local PostgreSQL server or use one in the cloud. Create a database
- Replace
Database Config
parameters in.env
to yours - Replace admin user information in
src/database/initAdminUser.js
. Keep the roles - There are some important configuration parameters
You can change them before running the database setup script or after. See the section
Configuration parameters
below - Run the database setup script by running
npm run dbInit
Application configuration is set up via the table general_config
You can change the parameters in the table setup object in src/database/initData.js
There are 4 parameters that affect the app functionality:
- oneLoginOnly: if true, a user will not be able to be logged in multiple browsers/devices.
- autoLogout: switches on the auto logout feature. After
timeout
time a user will be logged out if no activity on the site. When awarningTime
is left before the timeout the visual countdown will appear on the screen. - socialLoginNotAllowed prohibits to use OAuth for the specified roles
- roleDependencies: The role in the object key should also have all the roles in the object value, which is an array of roles. In the initial config it means that the admin needs to have the role 2FA which effectively means that the admin should have 2FA configured.
- Install dependencies with
npm i
- run
npm start
If you have found a bug, please report it in this repository's issues section.
This project is licensed under the MIT license. See the LICENSE file for more info.