Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
import Roles from '@src/core/domains/auth/enums/RolesEnum';
import UserFactory from '@src/core/domains/auth/factory/userFactory';
import { IUserData } from '@src/core/domains/auth/interfaces/IUserModel';
import hashPassword from '@src/core/domains/auth/utils/hashPassword';
import responseError from '@src/core/domains/express/requests/responseError';
import ValidationError from '@src/core/exceptions/ValidationError';
import { App } from '@src/core/services/App';
import { Request, Response } from 'express';
/**
* Creates a new user
*
* @param {Request} req - The request object
* @param {Response} res - The response object
* @returns {Promise<void>}
*/
export default async (req: Request, res: Response): Promise<void> => {
const { email, password, firstName, lastName } = req.body as Pick<IUserData, 'email' | 'password' | 'firstName' | 'lastName'>;
try {
// Check if the user already exists
const repository = App.container('auth').userRepository;
const existingUser = await repository.findOneByEmail(email);
if (existingUser) {
// If the user already exists, throw a validation error
throw new ValidationError('User already exists');
}
// Create a new user
const user = new UserFactory().create({
email,
password,
hashedPassword: hashPassword(password ?? ''),
roles: [Roles.USER],
firstName,
lastName
});
// Save the user to the database
await user.save();
// Generate a JWT token for the user
const token = await App.container('auth').createJwtFromUser(user);
// Return the user data and the JWT token
res.send({
success: true,
token,
user: user.getData({ excludeGuarded: true })
});
}
catch (error) {
// Handle validation errors
if (error instanceof ValidationError) {
res.status(400).send({ error: error.message });
return;
}
// Handle other errors
if (error instanceof Error) {
responseError(req, res, error);
}
}
}
import Roles from '@src/core/domains/auth/enums/RolesEnum';
import UserFactory from '@src/core/domains/auth/factory/userFactory';
import { IUserData } from '@src/core/domains/auth/interfaces/IUserModel';
import hashPassword from '@src/core/domains/auth/utils/hashPassword';
import responseError from '@src/core/domains/express/requests/responseError';
import ValidationError from '@src/core/exceptions/ValidationError';
import { App } from '@src/core/services/App';
import { Request, Response } from 'express';

/**
* Creates a new user
*
* @param {Request} req - The request object
* @param {Response} res - The response object
* @returns {Promise<void>}
*/
export default async (req: Request, res: Response): Promise<void> => {

const { email, password, firstName, lastName } = req.body as Pick<IUserData, 'email' | 'password' | 'firstName' | 'lastName'>;

try {
// Check if the user already exists
const repository = App.container('auth').userRepository;
const existingUser = await repository.findOneByEmail(email);

if (existingUser) {
// If the user already exists, throw a validation error
throw new ValidationError('User already exists');
}

// Create a new user
const user = new UserFactory().create({
email,
password,
hashedPassword: hashPassword(password ?? ''),
roles: [Roles.USER],
firstName,
lastName
});

// Save the user to the database
await user.save();

// Generate a JWT token for the user
const token = await App.container('auth').createJwtFromUser(user);

// Return the user data and the JWT token
res.send({
success: true,
token,
user: user.getData({ excludeGuarded: true })
});
}
catch (error) {
// Handle validation errors
if (error instanceof ValidationError) {
res.status(400).send({ error: error.message });
return;
}

// Handle other errors
if (error instanceof Error) {
responseError(req, res, error);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import IAuthorizedRequest from '@src/core/domains/auth/interfaces/IAuthorizedRequest';
import responseError from '@src/core/domains/express/requests/responseError';
import { Response } from 'express';
/**
* Gets the currently logged in user
*
* @param {IAuthorizedRequest} req
* @param {Response} res
* @returns {Promise<void>}
*/
export default async (req: IAuthorizedRequest, res: Response) => {
try {
// Send the user data without the password
res.send({ success: true, user: req.user?.getData({ excludeGuarded: true }) });
}
catch (error) {
// If there is an error, send the error response
if (error instanceof Error) {
responseError(req, res, error);
}
}
};
import IAuthorizedRequest from '@src/core/domains/auth/interfaces/IAuthorizedRequest';
import responseError from '@src/core/domains/express/requests/responseError';
import { Response } from 'express';

/**
* Gets the currently logged in user
*
* @param {IAuthorizedRequest} req
* @param {Response} res
* @returns {Promise<void>}
*/
export default async (req: IAuthorizedRequest, res: Response) => {
try {
// Send the user data without the password
res.send({ success: true, user: req.user?.getData({ excludeGuarded: true }) });
}
catch (error) {
// If there is an error, send the error response
if (error instanceof Error) {
responseError(req, res, error);
}
}
};
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import unauthorizedError from '@src/core/domains/auth/exceptions/UnauthorizedError';
import responseError from '@src/core/domains/express/requests/responseError';
import { App } from '@src/core/services/App';
import { Request, Response } from 'express';
/**
* Logs in a user
*
* @param {Request} req - The request object
* @param {Response} res - The response object
* @returns {Promise<void>}
*/
export default async (req: Request, res: Response): Promise<void> => {
try {
// Get the email and password from the request body
const { email, password } = req?.body ?? {};
// Attempt to log in the user
const token = await App.container('auth').attemptCredentials(email, password);
// Get the user from the database
const user = await App.container('auth').userRepository.findOneByEmail(email);
// Send the user data and the token back to the client
res.send({
success: true,
token,
user: user?.getData({ excludeGuarded: true })
})
}
catch (error) {
// Handle unauthorized errors
if (error instanceof unauthorizedError) {
res.status(401).send({ error: error.message },)
return;
}
// Handle other errors
if (error instanceof Error) {
responseError(req, res, error)
}
}
}
import unauthorizedError from '@src/core/domains/auth/exceptions/UnauthorizedError';
import responseError from '@src/core/domains/express/requests/responseError';
import { App } from '@src/core/services/App';
import { Request, Response } from 'express';

/**
* Logs in a user
*
* @param {Request} req - The request object
* @param {Response} res - The response object
* @returns {Promise<void>}
*/
export default async (req: Request, res: Response): Promise<void> => {
try {
// Get the email and password from the request body
const { email, password } = req?.body ?? {};

// Attempt to log in the user
const token = await App.container('auth').attemptCredentials(email, password);

// Get the user from the database
const user = await App.container('auth').userRepository.findOneByEmail(email);

// Send the user data and the token back to the client
res.send({
success: true,
token,
user: user?.getData({ excludeGuarded: true })
})
}
catch (error) {
// Handle unauthorized errors
if (error instanceof unauthorizedError) {
res.status(401).send({ error: error.message },)
return;
}

// Handle other errors
if (error instanceof Error) {
responseError(req, res, error)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import IAuthorizedRequest from '@src/core/domains/auth/interfaces/IAuthorizedRequest';
import responseError from '@src/core/domains/express/requests/responseError';
import { Response } from 'express';
/**
* Returns the currently logged in user
*
* @param {IAuthorizedRequest} req - The request object
* @param {Response} res - The response object
* @returns {Promise<void>}
*/
export default (req: IAuthorizedRequest, res: Response) => {
try {
// Send the user data without the password
res.send({ success: true, user: req.user?.getData({ excludeGuarded: true }) });
}
catch (error) {
// Handle any errors
if (error instanceof Error) {
responseError(req, res, error);
}
}
};
import IAuthorizedRequest from '@src/core/domains/auth/interfaces/IAuthorizedRequest';
import responseError from '@src/core/domains/express/requests/responseError';
import { Response } from 'express';

/**
* Returns the currently logged in user
*
* @param {IAuthorizedRequest} req - The request object
* @param {Response} res - The response object
* @returns {Promise<void>}
*/
export default (req: IAuthorizedRequest, res: Response) => {
try {
// Send the user data without the password
res.send({ success: true, user: req.user?.getData({ excludeGuarded: true }) });
}
catch (error) {
// Handle any errors
if (error instanceof Error) {
responseError(req, res, error);
}
}
};
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/**
* Constants for auth routes
*/
const authConsts = {
/**
* Route for creating a new user
*/
routes: {
/**
* Route for creating a new user
*/
authCreate: 'authCreate',
/**
* Route for logging in
*/
authLogin: 'authLogin',
/**
* Route for retrieving the current user
*/
authUser: 'authUser',
/**
* Route for revoking a token
*/
authRevoke: 'authRevoke',
/**
* Route for updating the current user
*/
authUpdate: 'authUpdate'
}
}
export default authConsts
/**
* Constants for auth routes
*/
const authConsts = {

/**
* Route for creating a new user
*/
routes: {

/**
* Route for creating a new user
*/
authCreate: 'authCreate',

/**
* Route for logging in
*/
authLogin: 'authLogin',

/**
* Route for retrieving the current user
*/
authUser: 'authUser',

/**
* Route for revoking a token
*/
authRevoke: 'authRevoke',

/**
* Route for updating the current user
*/
authUpdate: 'authUpdate'
}
}

export default authConsts
Loading