DEV Community

Lucky Oniovosa
Lucky Oniovosa

Posted on

Build a Complete User Authentication API in AdonisJs - Part 1 (project setup)

This tutorial is about building a user authentication system using AdonisJs.
AdonisJs is a Node.js MVC framework that runs on all major operating systems. It offers a stable ecosystem to write server-side web applications so you can focus on business needs over finalizing which package to choose or not.

Here are some of the benefit of using AdonisJs in my own opinion:

  • It is easy to set up a project and also integrates database
  • It has good documentation which makes it easy to learn.
  • It modularizes your codebase based on its opinionated nature.
  • It reduces your development time.

This tutorial is going to be in series and will cover the following areas:

  • Project setup
  • User Signup and login
  • User's Input field validation
  • Phone and Email verification
  • Forget and reset password functionality
  • Social Media Login System
  • Role-based authorization system.


Adonis by default can be used to create a full-stack app, but since we'll be building a REST API, we'll set up with the --api only flag. This will install the modules or dependencies for building the API.

Below are the following steps to follow to set up the project:
Step 1: install Adonis CLI globally

 npm i -g @adonisjs/cli 

Step 2: create an Adonis project by running the command below in the terminal

adonis new authsys --api-only

Step 3: cd into authsys project directory and run the server

cd authsys
adonis serve --dev

Confirm that the server runs on localhost( and also on port 3333 by entering on our browser URL. If everything goes well then it should return {"greeting":"Hello world in JSON"} as the response.

{"greeting":"Hello world in JSON"}

Step 4:

Database Setup

Since we are creating a user authentication system, we'll only need a user table in our database. Adonis.js comes with a user migration file and a token migration file, but for our project, we'll update the user migration file to create our user table on our database. We'll also take advantage of other authentication feature like password hashing, JWT authentication, timestamps etc which Adonis.js has made available for us.

we'll make use of MySQL but you can make use of other relational databases such as PostgreSQL, SQLite etc. you can following this link to install MySQL Community Server and MySQL workbench

  • we'll create the database from the workbench interface, we'll name it authsys_db
    Workbench database Image

  • install MySQL npm package, this will connect the app to the MySQL server

npm i mysql --save
  • update the user migration file with firstname, lastname and phone field. We also need to remove the username field.

class UserSchema extends Schema {
  up() {
    this.create('users', (table) => {
      table.string('firstname', 80).notNullable()
      table.string('lastname', 80).notNullable()
      table.string('email', 254).notNullable().unique()
      table.string('password', 60).notNullable()
      table.string('phone', 40).notNullable().unique()
  down() {
  • update the .env file to what is shown below
  • update the database.js file in the config folder to the following
connection: Env.get('DB_CONNECTION', 'mysql'),
mysql: {
    client: 'mysql',
    connection: {
      host: Env.get('DB_HOST', 'localhost'),
      port: Env.get('DB_PORT', ''),
      user: Env.get('DB_USER', 'root'),
      password: Env.get('DB_PASSWORD', ''),
      database: Env.get('DB_DATABASE', 'authsys_db')
    debug: Env.get('DB_DEBUG', false)
  • run our migration
adonis migration:run 

upon successful migration, we should have something that looks like this below

$ adonis migration:run
migrate: 1503250034279_user.js
migrate: 1503250034280_token.js
Database migrated successfully in 2.03 s

the MySQL workbench should also look like this upon successful migration
MySQL schema preview


In this tutorial, we learnt about Adonis.Js and why you'll want to use it in your next project. we also learnt how to setup create our project using Adonis CLI. we were able to create a MySQL database and connect it to our project.
In the next part, we will create a signup and login controller and learn how to easily validate our input using Adonis.
Thank you for time and see you in the next section.

Top comments (2)

patarapolw profile image
Pacharapol Withayasakpunt

It increases your development time.

Is this a good thing?

luckzman profile image
Lucky Oniovosa

Thanks for pointing this out..... I meant it reduces your development time.