Create a secure API with AdonisJS and Postgres — Part 1

npm i -g @adonisjs/cli
sudo npm i -g @adonisjs/cli
adonis new secure-api --api-only
AdonisJS API new project
adonis serve --dev
Project’s structure
HOST=127.0.0.1
PORT=3333 #server's port
NODE_ENV=development #if you set it to production, debug errors will be turned off
APP_NAME=RestAPI #your application's name
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=mulhgTjgI0xDvpsDWLXMJfJYCzn30B5F
DB_CONNECTION=pg
##database
DB_HOST=*****.alexcasarrubias.com.mx
DB_PORT=5432
DB_USER=myusername
DB_PASSWORD=mypassword123!.
DB_DATABASE=adonis
## end - database section
HASH_DRIVER=bcrypt
adonis install @adonisjs/auth
  '@adonisjs/auth/providers/AuthProvider'
const providers = [
"@adonisjs/framework/providers/AppProvider",
"@adonisjs/auth/providers/AuthProvider",
"@adonisjs/bodyparser/providers/BodyParserProvider",
"@adonisjs/cors/providers/CorsProvider",
"@adonisjs/lucid/providers/LucidProvider",
"@adonisjs/auth/providers/AuthProvider",
];
globalMiddleware =>  'Adonis/Middleware/AuthInit'

namedMiddleware => auth: 'Adonis/Middleware/Auth'
const globalMiddleware = ["Adonis/Middleware/BodyParser","App/Middleware/ConvertEmptyStringsToNull","Adonis/Middleware/AuthInit",];
const namedMiddleware = {
auth: "Adonis/Middleware/Auth",guest: "Adonis/Middleware/AllowGuestOnly",};

Configuration

authenticator — jwt
adonis make:controller UserController
  • login
  • showUser
async login({ request, auth }) {const { email, password } = request.all();await auth.attempt(email, password);return "Hell yeah!";}show({ auth, params }) {if (auth.user.id !== Number(params.id)) {return "Unauthorized: Not your profile";}return auth.user;}
Route.post('login', 'UserController.login')
adonis serve --dev
Method GET not found
Route.post('login', 'UserController.login')
Epic fail, again
npm install pg
adonis migration:run
Route.post(“register”, “UserController.register”);
async register({ request, auth }) {const { email, username, password } = request.all();const User = use("App/Models/User");const user = new User();user.username = username;user.email = email;user.password = password;await user.save();return user;
}
{"username":"myuser","email":"myuser@yeah.com","password":"test1234"}
{"email":"myuser@yeah.com","password":"test1234"}
async login({ request, auth }) {const { email, password } = request.all();return await auth.attempt(email, password);}
{“type”: “bearer”,“token”: “eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjMsImlhdCI6MTYwOTU1MzM0OX0.LjQRh6Rk0oJWaCUUYde91YoesKMgTO3v10TUsimAz3Y”,“refreshToken”: null}

--

--

--

Mexicano, emprendedor, director de Software y amante del arte, la fotografía y la cerveza. ¡Salud!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Converting a Sorted Array to Binary Search Tree in Vanilla Javascript

Day 110 — Adding Analytics to the App

The Truth About React Native By React Native Developer

The Truth About React Native By React Native Developer

React Native ScrollView Expanding Item Animation

Building More Accessible Angular Apps

Five tips I wish I knew when I started with Typescript

Laravel Response

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alex Casarrubias

Alex Casarrubias

Mexicano, emprendedor, director de Software y amante del arte, la fotografía y la cerveza. ¡Salud!

More from Medium

How to dockerize a react back-end app and postgres

MEAN Stack — A Popular Choice for Web App Development

SE3050 -Application Frameworks Blog

To future me, here is the way to set JAVA_HOME