added login / logout functionality and authorization rules via bouncer
This commit is contained in:
67
start/bouncer.ts
Normal file
67
start/bouncer.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Contract source: https://git.io/Jte3T
|
||||
*
|
||||
* Feel free to let us know via PR, if you find something broken in this config
|
||||
* file.
|
||||
*/
|
||||
|
||||
import Bouncer from '@ioc:Adonis/Addons/Bouncer'
|
||||
import User from 'App/Models/User'
|
||||
import Employee from 'App/Models/Employee'
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Bouncer Actions
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Actions allows you to separate your application business logic from the
|
||||
| authorization logic. Feel free to make use of policies when you find
|
||||
| yourself creating too many actions
|
||||
|
|
||||
| You can define an action using the `.define` method on the Bouncer object
|
||||
| as shown in the following example
|
||||
|
|
||||
| ```
|
||||
| Bouncer.define('deletePost', (user: User, post: Post) => {
|
||||
| return post.user_id === user.id
|
||||
| })
|
||||
| ```
|
||||
|
|
||||
|****************************************************************
|
||||
| NOTE: Always export the "actions" const from this file
|
||||
|****************************************************************
|
||||
*/
|
||||
export const { actions } = Bouncer
|
||||
|
||||
.define('employees.index', (user: User) => {
|
||||
return user.role === 'admin'
|
||||
})
|
||||
|
||||
.define('employees.show', (user: User, employee : Employee) => {
|
||||
return user.role === 'admin' || user.id === employee.userId
|
||||
})
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Bouncer Policies
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Policies are self contained actions for a given resource. For example: You
|
||||
| can create a policy for a "User" resource, one policy for a "Post" resource
|
||||
| and so on.
|
||||
|
|
||||
| The "registerPolicies" accepts a unique policy name and a function to lazy
|
||||
| import the policy
|
||||
|
|
||||
| ```
|
||||
| Bouncer.registerPolicies({
|
||||
| UserPolicy: () => import('App/Policies/User'),
|
||||
| PostPolicy: () => import('App/Policies/Post')
|
||||
| })
|
||||
| ```
|
||||
|
|
||||
|****************************************************************
|
||||
| NOTE: Always export the "policies" const from this file
|
||||
|****************************************************************
|
||||
*/
|
||||
export const { policies } = Bouncer.registerPolicies({})
|
||||
@@ -20,4 +20,15 @@
|
||||
|
||||
import Route from '@ioc:Adonis/Core/Route'
|
||||
|
||||
Route.resource('employees', 'EmployeesController').except(['create', 'edit'])
|
||||
Route.group(() => {
|
||||
Route.post('login', 'AuthController.login').as('login')
|
||||
})
|
||||
.prefix('api/v1')
|
||||
|
||||
Route.group(() => {
|
||||
Route.get('logout', 'AuthController.logout').as('logout')
|
||||
|
||||
Route.resource('employees', 'EmployeesController').apiOnly()
|
||||
})
|
||||
.prefix('api/v1')
|
||||
.middleware('auth')
|
||||
|
||||
Reference in New Issue
Block a user