1.0.2 • Published 2 years ago

@pd-solucoes/adonis-basic-crud v1.0.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
2 years ago

Project description

This lib is a fast way to create a User crud with login and mail recovery, build over AdonisJS.

Instalation process

npm:

yarn:

Configuration

This project needs some configurations on host project.

In your .env file is necessary to add soma variables.

In your routes file (./start/routes), need to import routes from LIB.

Database config

Your project need to config database connection, configuration can be found at this link: adonis databse configuration

Befere step above you will need create some migrations.

User:

Tokens:

Mailer config

Your project also needs to install and configure mail providers, configuration can be found at this link: adonis mailer config Project only support this two email providers: SES and SMTP

Lib resources

The lib provides de follow routes:

HTTP VERBROUTE SUFFIXBODYAUTHORIZATIONAUTHORIZATION TYPE
POST/users{"name": "exampe", "email": "example@email.com","password": "mySecretPassword"}false-
GET/users-truebearer token
PUT/users{"name": "Update example","email": "example1@email.com"}truebearer token
DELETE/users-truebearer token
POST/session{"email": "example1@email.com","password": "mySecretPassword"}false-
POST/password/forgot{"email": "example1@email.com"}false-
POST/password/reset{"token": "token sent to email","new_password": "myNewSecretPassword","new_password_confirmation": "myNewSecretPassword"}false-
POST/password/change{"password": "myActualSecretPassword","new_password": "myNewSecretPassword","new_password_confirmation": "MyNewSecretPassword"}truebearer token

This lib provides a auth middleware to use in your host project. On use this middleware it automatically provides a user to request.

User object is like that:

user: {
    id: '1'
}

Example, turns /hello route in private route:

   import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
   import Route from '@ioc:Adonis/Core/Route'

   import {auth} from "@pdsolucoes/adonis-basic-crud"

   Route.post('hello', ({request, response}: HttpContextContract) => {
       return response.ok({
           ok:true
       })
   }).middleware(auth.authenticate)

In order to avoid type errors is recomended use the exported AuthHttpContract from lib.

Example, in hello routes need to show logged user_id:

   import Route from '@ioc:Adonis/Core/Route'

   import {auth, AuthHttpContract} from "@pdsolucoes/adonis-basic-crud"

   Route.post('hello', ({request, response}: AuthHttpContract) => {
       return response.ok({
           ok:true,
           logged_user: request.user.id
       })
   }).middleware(auth.authenticate)

This lib also provide a generic error handler. In order to use this functionality is needed to use StatusCode enum.

Examaple:

  import {GenericExceptionHandler, StatusCode} from "@pdsolucoes/adonis-basic-crud"

  throw new GenericExceptionHandler("My error message", StatusCode.Forbidden)