Skip to content
Open
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
151 changes: 77 additions & 74 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,26 @@ export type {
FrequencyObject,
FullEvent,
} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/models/event.model.ts';

export type {
ErrorResponse,
MessageResponse,
} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/general.service.ts';

export type {
LoginErrorDetails,
LoginErrorResponse,
LoginSuccessResponse,
MeSuccessResponse,
SignupErrorDetails,
SignupErrorResponse,
SignupSuccessResponse,
} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/auth.service.ts';

export type {
GetAllEventsErrorResponse,
} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/events.service.ts';

export type {
GetAllUsersSuccessResponse,
} from 'https://raw.githubusercontent.com/fac-31/Pro0428-LocalEventShared/main/src/services/users.service.ts';
36 changes: 26 additions & 10 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// deno-lint-ignore-file require-await
import {
Context,
ErrorResponse,
LoginErrorDetails,
LoginErrorResponse,
LoginSuccessResponse,
MeSuccessResponse,
RouterContext,
SignupErrorDetails,
SignupErrorResponse,
SignupSuccessResponse,
Status,
UserLogInSchema,
UserSignUpSchema,
Expand All @@ -13,13 +21,13 @@ export const getCurrentUser = async (ctx: Context) => {

if (!user) {
ctx.response.status = Status.Unauthorized;
ctx.response.body = { message: 'User not authenticated' };
ctx.response.body = { error: 'User not authenticated' } as ErrorResponse;
return;
}
ctx.response.body = {
message: `Hello, ${user.username}!`,
user,
};
} as MeSuccessResponse;
};

export const signUpUser = async (ctx: RouterContext<'/signup'>) => {
Expand All @@ -32,20 +40,24 @@ export const signUpUser = async (ctx: RouterContext<'/signup'>) => {
if (!userInput.success) {
console.log('Validation Errors:', userInput.error);
ctx.response.status = Status.BadRequest;
ctx.response.body = { errors: userInput.error.flatten() };
ctx.response.body = {
errors: userInput.error.flatten() as SignupErrorDetails,
} as SignupErrorResponse;
return;
}
try {
const insertedId = await authService.createUser(userInput.data);
ctx.response.status = Status.Created;
ctx.response.body = insertedId;
ctx.response.body = insertedId as SignupSuccessResponse;
} catch (error) {
ctx.response.status = Status.InternalServerError;
if (error instanceof Error) {
console.log('Sign up user error:', error);
ctx.response.body = { error: error.message };
ctx.response.body = { error: error.message } as ErrorResponse;
} else {
ctx.response.body = { error: 'Unkown error creating user' };
ctx.response.body = {
error: 'Unkown error creating user',
} as ErrorResponse;
}
}
};
Expand All @@ -55,22 +67,26 @@ export const loginUser = async (ctx: RouterContext<'/login'>) => {
const userInput = UserLogInSchema.safeParse(body);
if (!userInput.success) {
ctx.response.status = Status.BadRequest;
ctx.response.body = { errors: userInput.error.flatten() };
ctx.response.body = {
errors: userInput.error.flatten() as LoginErrorDetails,
} as LoginErrorResponse;
console.error(userInput.error);
return;
}
try {
const token = await authService.logInUser(userInput.data);
ctx.response.status = Status.OK;
ctx.response.body = { token };
ctx.response.body = { token } as LoginSuccessResponse;
} catch (error) {
if (error instanceof Error) {
ctx.response.status = Status.Unauthorized;
ctx.response.body = { error: error.message };
ctx.response.body = { error: error.message } as ErrorResponse;
console.error(error.message);
} else {
ctx.response.status = Status.InternalServerError;
ctx.response.body = { error: 'Unkown error creating token' };
ctx.response.body = {
error: 'Unkown error creating token',
} as ErrorResponse;
console.error(error);
}
}
Expand Down
Loading