feat: introduce role based access control

This commit is contained in:
Ali BARIN
2023-06-22 22:20:10 +00:00
parent a5c7da331a
commit 69582ff83d
25 changed files with 376 additions and 19 deletions

View File

@@ -12,7 +12,15 @@ const isAuthenticated = rule()(async (_parent, _args, req) => {
const { userId } = jwt.verify(token, appConfig.appSecretKey) as {
userId: string;
};
req.currentUser = await User.query().findById(userId).throwIfNotFound();
req.currentUser = await User
.query()
.findById(userId)
.joinRelated({
permissions: true,
})
.withGraphFetched({
permissions: true,
});
return true;
} catch (error) {

View File

@@ -1,10 +1,11 @@
import { Model } from 'objection';
import ExtendedQueryBuilder from '../models/query-builder';
import type Base from '../models/base';
const paginate = async (
query: ExtendedQueryBuilder<Model, Model[]>,
limit: number,
offset: number
offset: number,
) => {
if (limit < 1 || limit > 100) {
throw new Error('Limit must be between 1 and 100');
@@ -20,11 +21,9 @@ const paginate = async (
currentPage: Math.ceil(offset / limit + 1),
totalPages: Math.ceil(count / limit),
},
edges: records.map((record: Model) => {
return {
node: record,
};
}),
edges: records.map((record: Base) => ({
node: record,
})),
};
};