feat: add event count endpoints and improve auth logging with constants
This commit is contained in:
@@ -40,6 +40,7 @@ const INVALID_AUTH_TOKEN = 'Invalid authorization token';
|
||||
const USER_ID_NOT_FOUND = 'userId not found';
|
||||
const USER_TOKEN_OK = 'user token check ok';
|
||||
const AUTHORIZATION_TOKEN_MISSING_OR_INVALID = 'Authorization token missing or invalid';
|
||||
const USER_BANNED = 'user banned';
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const debug = { 'req.headers': flattenNextjsRequest(req) };
|
||||
@@ -55,8 +56,6 @@ export async function GET(req: NextRequest) {
|
||||
const accessToken = `${authorization}`.split(' ')[1];
|
||||
const data = await verify(accessToken, JWT_SECRET);
|
||||
|
||||
console.log({ data });
|
||||
|
||||
if (data.userId) {
|
||||
const { userId } = data;
|
||||
|
||||
|
21
03_source/cms_backend/src/app/api/event/helloworld/route.ts
Normal file
21
03_source/cms_backend/src/app/api/event/helloworld/route.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { logger } from 'src/utils/logger';
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { countTotalEvents } from 'src/app/services/eventItem.service';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/** **************************************
|
||||
* GET - Events, obsoleted
|
||||
*************************************** */
|
||||
export async function GET() {
|
||||
try {
|
||||
const numOfEvent = await countTotalEvents();
|
||||
|
||||
logger('[Event] list', numOfEvent);
|
||||
|
||||
return response({ numOfEvent }, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('Event - Get list', error);
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
###
|
||||
|
||||
GET http://localhost:7272/api/event/helloworld
|
22
03_source/cms_backend/src/app/api/event/numOfEvent/route.ts
Normal file
22
03_source/cms_backend/src/app/api/event/numOfEvent/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { logger } from 'src/utils/logger';
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
// src/app/api/event/list/route.ts
|
||||
import { countTotalEvents, listEvents } from 'src/app/services/eventItem.service';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/** **************************************
|
||||
* GET - Events, obsoleted
|
||||
*************************************** */
|
||||
export async function GET() {
|
||||
try {
|
||||
const numOfEvents = await countTotalEvents();
|
||||
|
||||
// logger('[Event] list', numOfEvents.length);
|
||||
|
||||
return response({ numOfEvents }, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('Event - Get list', error);
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
###
|
||||
|
||||
GET http://localhost:7272/api/event/numOfEvent
|
@@ -0,0 +1,23 @@
|
||||
//
|
||||
//
|
||||
import { logger } from 'src/utils/logger';
|
||||
import { STATUS, response, handleError } from 'src/utils/response';
|
||||
|
||||
import { countTotalEvents } from 'src/app/services/eventItem.service';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/** **************************************
|
||||
* GET - Events, obsoleted
|
||||
*************************************** */
|
||||
export async function GET() {
|
||||
try {
|
||||
const numOfEvent = await countTotalEvents();
|
||||
|
||||
logger('[Event] list', numOfEvent);
|
||||
|
||||
return response({ numOfEvent }, STATUS.OK);
|
||||
} catch (error) {
|
||||
return handleError('Event - Get list', error);
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
###
|
||||
|
||||
GET http://localhost:7272/api/party-event/numOfEvent
|
@@ -0,0 +1,6 @@
|
||||
export const ERR_USER_NOT_FOUND = 'There is no user corresponding to the email address.';
|
||||
export const ERR_WRONG_PASSWORD = 'Wrong password';
|
||||
export const LOG_USER_TRIED_LOGIN_WITH_EMAIL = `user tried login with email`;
|
||||
export const LOG_USER_LOGGED_WITH_WRONG_PASSWORD = 'user logged with wrong password';
|
||||
export const LOG_ACCESS_GRANTED = 'access granted';
|
||||
export const LOG_ATTEMPTED_LOGIN_BUT_FAILED = 'attempted login but failed';
|
@@ -10,9 +10,16 @@ import { createAccessLog } from 'src/app/services/access-log.service';
|
||||
|
||||
import prisma from '../../../lib/prisma';
|
||||
import { flattenNextjsRequest } from './flattenNextjsRequest';
|
||||
import {
|
||||
LOG_USER_TRIED_LOGIN_WITH_EMAIL,
|
||||
ERR_USER_NOT_FOUND,
|
||||
LOG_USER_LOGGED_WITH_WRONG_PASSWORD,
|
||||
ERR_WRONG_PASSWORD,
|
||||
LOG_ACCESS_GRANTED,
|
||||
LOG_ATTEMPTED_LOGIN_BUT_FAILED,
|
||||
} from './constants';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* This API is used for demo purpose only
|
||||
* You should use a real database
|
||||
@@ -21,9 +28,6 @@ import { flattenNextjsRequest } from './flattenNextjsRequest';
|
||||
* You should not expose the JWT_SECRET in the client side
|
||||
*/
|
||||
|
||||
const ERR_USER_NOT_FOUND = 'There is no user corresponding to the email address.';
|
||||
const ERR_WRONG_PASSWORD = 'Wrong password';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const debug = { 'req.headers': flattenNextjsRequest(req) };
|
||||
|
||||
@@ -32,12 +36,12 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
const currentUser = await prisma.partyUser.findFirst({ where: { email } });
|
||||
if (!currentUser) {
|
||||
await createAccessLog('', `user tried login with email ${email}`, { debug });
|
||||
await createAccessLog('', LOG_USER_TRIED_LOGIN_WITH_EMAIL, { email, debug });
|
||||
return response({ message: ERR_USER_NOT_FOUND }, STATUS.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
if (currentUser?.password !== password) {
|
||||
await createAccessLog(currentUser.id, 'user logged with wrong password', { debug });
|
||||
await createAccessLog(currentUser.id, LOG_USER_LOGGED_WITH_WRONG_PASSWORD, { debug });
|
||||
return response({ message: ERR_WRONG_PASSWORD }, STATUS.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@@ -45,11 +49,11 @@ export async function POST(req: NextRequest) {
|
||||
expiresIn: JWT_EXPIRES_IN,
|
||||
});
|
||||
|
||||
await createAccessLog(currentUser.id, 'access granted', { debug });
|
||||
await createAccessLog(currentUser.id, LOG_ACCESS_GRANTED, { debug });
|
||||
|
||||
return response({ user: currentUser, accessToken }, STATUS.OK);
|
||||
} catch (error) {
|
||||
await createAccessLog('', 'attempted login but failed', { debug, error });
|
||||
await createAccessLog('', LOG_ATTEMPTED_LOGIN_BUT_FAILED, { debug, error });
|
||||
|
||||
return handleError('Auth - Sign in', error);
|
||||
}
|
||||
|
@@ -51,6 +51,17 @@ async function getEventItemById(eventId: string): Promise<EventItem | null> {
|
||||
return prisma.eventItem.findFirst({ where: { id: eventId } });
|
||||
}
|
||||
|
||||
async function countTotalEvents(): Promise<number> {
|
||||
try {
|
||||
const result = await prisma.eventItem.findMany();
|
||||
console.log({ result });
|
||||
return result.length;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// async function createNewEvent(createForm: CreateEvent) {
|
||||
// return prisma.event.create({ data: createForm });
|
||||
// }
|
||||
@@ -73,4 +84,5 @@ export {
|
||||
// deleteEvent,
|
||||
// createNewEvent,
|
||||
getEventItemById,
|
||||
countTotalEvents,
|
||||
};
|
||||
|
Reference in New Issue
Block a user