@pd-solucoes/adonis-basic-crud
v1.0.2
Published
A simple and effective user crud with e-mail
Downloads
12
Readme
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 VERB | ROUTE SUFFIX | BODY | AUTHORIZATION | AUTHORIZATION TYPE|
|-|-|-|-|-|
| POST | /users | {"name": "exampe", "email": "[email protected]","password": "mySecretPassword"}
| false | - |
| GET | /users | - | true | bearer token|
| PUT | /users | {"name": "Update example","email": "[email protected]"}
| true | bearer token|
| DELETE | /users | - | true | bearer token|
| POST | /session | {"email": "[email protected]","password": "mySecretPassword"}
| false | - |
| POST | /password/forgot | {"email": "[email protected]"}
| 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"}
| true | bearer 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)