Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a5ae69d
Partial refactor of auth domain with legacy files moved to a seperate…
ben-shepherd Feb 5, 2025
d33322f
Refactor authentication domain with new ACL and scope management serv…
ben-shepherd Feb 5, 2025
38c3861
Enhance authentication middleware and models with improved scope and …
ben-shepherd Feb 5, 2025
821b097
Refactor User and UserRepository to use AuthUser base class
ben-shepherd Feb 5, 2025
0202ff1
Refactor authentication domain with improved use case and observer pa…
ben-shepherd Feb 5, 2025
00f3dab
Implement comprehensive authentication use cases and controller impro…
ben-shepherd Feb 5, 2025
9cacb5f
Fix router middleware configuration to use provided options
ben-shepherd Feb 5, 2025
4a2b626
Implement logout and token refresh use cases for JWT authentication
ben-shepherd Feb 5, 2025
cda6314
Add user update functionality to authentication domain
ben-shepherd Feb 5, 2025
7f1e222
Enhance JWT authentication configuration and route management
ben-shepherd Feb 5, 2025
92c4e04
Add comprehensive documentation for auth configuration module
ben-shepherd Feb 5, 2025
c3443a1
Refactor authentication domain by removing legacy files and consolida…
ben-shepherd Feb 5, 2025
23984eb
Fixed imports, formatting
ben-shepherd Feb 5, 2025
3a4b165
Enhance JWT authentication configuration and model interfaces
ben-shepherd Feb 5, 2025
c4a864d
Update Tinker configuration to use dynamic providers
ben-shepherd Feb 5, 2025
7bb93a9
fixed imports/formatting
ben-shepherd Feb 5, 2025
ec62a72
Refactor UserRepository with comprehensive documentation and improved…
ben-shepherd Feb 5, 2025
e1fb748
Refactor BaseRequest and add IRequestIdentifiable interface
ben-shepherd Feb 5, 2025
63d9617
Added a comment to the update method in AuthController
ben-shepherd Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/migrations/2024-09-06-create-api-token-table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ApiToken from "@src/app/models/auth/ApiToken";
import ApiToken from "@src/core/domains/auth/models/ApiToken";
import BaseMigration from "@src/core/domains/migrations/base/BaseMigration";
import { DataTypes } from "sequelize";

Expand Down
5 changes: 3 additions & 2 deletions src/app/migrations/2024-09-06-create-user-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export class CreateUserModelMigration extends BaseMigration {
await this.schema.createTable(this.table, {
email: DataTypes.STRING,
hashedPassword: DataTypes.STRING,
groups: DataTypes.JSON,
roles: DataTypes.JSON,
groups: DataTypes.ARRAY(DataTypes.STRING),
roles: DataTypes.ARRAY(DataTypes.STRING),
firstName: stringNullable,
lastName: stringNullable,
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE

})
}

Expand Down
103 changes: 0 additions & 103 deletions src/app/models/auth/ApiToken.ts

This file was deleted.

52 changes: 4 additions & 48 deletions src/app/models/auth/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import UserObserver from "@src/app/observers/UserObserver";
import IUserModel from "@src/core/domains/auth/interfaces/IUserModel";
import AuthUser from "@src/core/domains/auth/models/AuthUser";
import UserObserver from "@src/core/domains/auth/observers/UserObserver";
import { IModelAttributes } from "@src/core/interfaces/IModel";
import Model from "@src/core/models/base/Model";

/**
* User structure
Expand All @@ -23,7 +22,7 @@ export interface UserAttributes extends IModelAttributes {
*
* Represents a user in the database.
*/
export default class User extends Model<UserAttributes> implements IUserModel {
export default class User extends AuthUser {

/**
* Table name
Expand All @@ -38,6 +37,7 @@ export default class User extends Model<UserAttributes> implements IUserModel {
this.setObserverConstructor(UserObserver);
}


/**
* Guarded fields
*
Expand Down Expand Up @@ -66,16 +66,6 @@ export default class User extends Model<UserAttributes> implements IUserModel {
'updatedAt',
]

/**
* Fields that should be returned as JSON
*
* These fields will be returned as JSON when the model is serialized.
*/
json = [
'groups',
'roles'
]

/**
* Retrieves the fields defined on the model, minus the password field.
* As this is a temporary field and shouldn't be saved to the database.
Expand All @@ -86,38 +76,4 @@ export default class User extends Model<UserAttributes> implements IUserModel {
return super.getFields().filter(field => !['password'].includes(field));
}

/**
* Checks if the user has the given role
*
* @param role The role to check
* @returns True if the user has the role, false otherwise
*/
hasRole(roles: string | string[]): boolean {
roles = typeof roles === 'string' ? [roles] : roles;
const userRoles = this.getAttributeSync('roles') ?? [];

for(const role of roles) {
if(!userRoles.includes(role)) return false;
}

return true;
}

/**
* Checks if the user has the given role
*
* @param role The role to check
* @returns True if the user has the role, false otherwise
*/
hasGroup(groups: string | string[]): boolean {
groups = typeof groups === 'string' ? [groups] : groups;
const userGroups = this.getAttributeSync('groups') ?? [];

for(const group of groups) {
if(!userGroups.includes(group)) return false;
}

return true;
}

}
Empty file added src/app/observers/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion src/app/providers/RoutesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RoutesProvider extends BaseProvider {
const httpService = app('http');

// Bind routes
httpService.bindRoutes(app('auth').getAuthRoutes())
httpService.bindRoutes(app('auth.jwt').getRouter())
httpService.bindRoutes(healthRoutes);
httpService.bindRoutes(apiRoutes);

Expand Down
31 changes: 0 additions & 31 deletions src/app/repositories/auth/ApiTokenRepository.ts

This file was deleted.

16 changes: 3 additions & 13 deletions src/app/repositories/auth/UserRepository.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import User from "@src/app/models/auth/User";
import Repository from "@src/core/base/Repository";
import IUserModel from "@src/core/domains/auth/interfaces/IUserModel";
import IUserRepository from "@src/core/domains/auth/interfaces/IUserRepository";
import AuthUserRepository from '@src/core/domains/auth/repository/UserRepository';

export default class UserRepository extends Repository<IUserModel> implements IUserRepository {

export default class UserRepository extends AuthUserRepository {

constructor() {
super(User)
}

/**
* Finds a User by their email address
* @param email
* @returns
*/
public async findOneByEmail(email: string): Promise<IUserModel | null> {
return this.query().where('email', email).first()
}

}
3 changes: 1 addition & 2 deletions src/app/routes/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Route from "@src/core/domains/http/router/Route"

import ExampleController from "../controllers/ExampleController"
import ExampleController from "@src/app/controllers/ExampleController"

export default Route.group(router => {

Expand Down
6 changes: 3 additions & 3 deletions src/app/validators/user/CreateUserValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { auth } from "@src/core/domains/auth/services/AuthService";
import User from "@src/app/models/auth/User";
import { queryBuilder } from "@src/core/domains/eloquent/services/EloquentQueryBuilderService";
import BaseValidator from "@src/core/domains/validator/base/BaseValidator";
import { ValidatorPayload } from "@src/core/domains/validator/interfaces/IValidator";
import Joi, { ObjectSchema } from "joi";
Expand All @@ -15,8 +16,7 @@ class CreateUserValidator extends BaseValidator {
*/
async validateEmailAvailability(payload: ValidatorPayload) {

const repository = auth().getUserRepository();
const user = await repository.findOneByEmail(payload.email as string);
const user = await queryBuilder(User).where('email', payload.email as string).first();

if(user) {
this.setErrorMessage({ email: 'User already exists' });
Expand Down
47 changes: 47 additions & 0 deletions src/config/acl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { IAclConfig } from "@src/core/domains/auth/interfaces/acl/IAclConfig";

// Define available groups
export const GROUPS = {
User: 'group_user',
Admin: 'group_admin',
} as const

// Define available roles
export const ROLES = {
USER: 'role_user',
ADMIN: 'role_admin'
} as const

/**
* ACL configuration
*/
export const aclConfig: IAclConfig = {

// Default user group
defaultGroup: GROUPS.User,

// List of groups
groups: [
{
name: GROUPS.User,
roles: [ROLES.USER]
},
{
name: GROUPS.Admin,
roles: [ROLES.USER, ROLES.ADMIN]
}
],

// List of roles, scopes and other permissions
roles: [
{
name: ROLES.ADMIN,
scopes: []
},
{
name: ROLES.USER,
scopes: []
},
],

}
Loading