Build a Complete User Authentication API in AdonisJs - Part 1 (project setup)
Tags: AdonisJs, api
Published: 2020-05-16 19:46:14

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.

### PROJECT SETUP
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 <i>--api only</i> flag. This will install the modules or dependencies for building the API.

Below are the following steps to follow to set up the project:
<b>Step 1:</b> install Adonis CLI globally
<pre> npm i -g @adonisjs/cli </pre>

<b>Step 2:</b> create an Adonis project by running the command below in the terminal
<pre>adonis new authsys --api-only</pre>

<b>Step 3:</b> cd into <b>authsys</b> project directory and run the server
<pre>
cd authsys
adonis serve --dev
</pre>

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

<b>Step 4: </b>
#### 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 <b>user migration file</b> and a <b>token migration file</b>, 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 https://dev.mysql.com/downloads/mysql/ and MySQL workbench https://dev.mysql.com/downloads/workbench/

- we'll create the database from the workbench interface, we'll name it <b>authsys_db</b>
![Workbench database Image](https://res.cloudinary.com/dx0nauane/image/upload/v1589122957/Dev.To/mysql_db.jpg)

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

<pre>npm i mysql --save</pre>

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

<pre><code>
class UserSchema extends Schema {
  up() {
    this.create('users', (table) => {
      table.increments()
      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()
      table.timestamps()
    })
  }
  down() {
    this.drop('users')
  }
}</code>
</pre>


- update the .env file to what is shown below

<pre>
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=
DB_DATABASE=authsys_db
</pre>

- update the database.js file in the config folder to the following

<pre>
connection: Env.get('DB_CONNECTION', 'mysql'),
</pre>

<pre>
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)
  },
</pre>

- run our migration 

<pre>adonis migration:run </pre>

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

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

the MySQL workbench should also look like this upon successful migration
![MySQL schema preview](https://res.cloudinary.com/dx0nauane/image/upload/v1589127194/Dev.To/database_schema.jpg)

#### Summary
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.