Skip to content

Plugin to check Permissions. #627

Closed Answered by mtt-artis
apescione asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @apescione
I'll tell you what I've done not knowing if it is best practice.

I used to inline all my routes on the same Elysia instance. I check the authorisation in a scoped middleware and then check the user's profile in the beforeHandle of each route like below

import { Elysia, t } from "elysia";

const hasRole = (profil: string) =>  ({ error, jwt }) => { 
  if (!jwt.roles.includes(role)) throw error("Unauthorized");
};

export const routes = new Elysia()
    .use(jwt())
    .get('/', () => 'hi', {
        beforeHandle: hasRole("BASIC_USER")
    })
   .get('/admin', () => 'admin', {
        beforeHandle: hasRole("ADMIN")
    })

Replies: 2 comments 8 replies

Comment options

You must be logged in to vote
8 replies
@apescione
Comment options

@apescione
Comment options

@apescione
Comment options

@mtt-artis
Comment options

Answer selected by apescione
@apescione
Comment options

@apescione
Comment options

@mtt-artis
Comment options

@mtt-artis
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants