feat: extend auth endpoint to support both User and PartyUser models with fallback retrieval logic

This commit is contained in:
louiscklaw
2025-06-18 12:43:03 +08:00
parent 99fafda624
commit f950617372
4 changed files with 51 additions and 11 deletions

View File

@@ -1,4 +1,13 @@
import type { User } from '@prisma/client';
// src/app/api/auth/me/route.ts
//
// PURPOSE:
// - T.B.A.
//
// RULES:
// - T.B.A.
//
import type { PartyUser, User } from '@prisma/client';
import type { NextRequest } from 'next/server';
import { headers } from 'next/headers';
@@ -11,9 +20,11 @@ import { getUserById } from 'src/app/services/user.service';
import { createAccessLog } from 'src/app/services/access-log.service';
import { flattenNextjsRequest } from '../sign-in/flattenNextjsRequest';
import { getPartyUserById } from 'src/app/services/party-user.service';
// ----------------------------------------------------------------------
// NOTE: keep this comment to let prisma running on nextjs
// export const runtime = 'edge';
/**
@@ -43,12 +54,19 @@ export async function GET(req: NextRequest) {
const accessToken = `${authorization}`.split(' ')[1];
const data = await verify(accessToken, JWT_SECRET);
console.log(data.userId);
console.log({ data });
if (data.userId) {
// TODO: remove me
// const currentUser = _users.find((user) => user.id === data.userId);
const currentUser: User | null = await getUserById(data.userId);
const { userId } = data;
let currentUser: User | PartyUser | null = null;
currentUser = await getPartyUserById(userId);
if (!currentUser) {
currentUser = await getUserById(userId);
}
if (!currentUser) {
createAccessLog('', USER_TOKEN_CHECK_FAILED, debug);

View File

@@ -1,11 +1,26 @@
###
# username and password ok
GET http://localhost:7272/api/auth/me
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbWJnbnUyengwMDBjaHEzaGZ3dmtjejlvIiwiaWF0IjoxNzQ4OTY0ODkyLCJleHAiOjE3NTAxNzQ0OTJ9.lo04laCxtm0IVeYaETEV3hXKyDmXPEn7SyWtY2VR4dI
GET http://localhost:7272/api/auth/me
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbWMwdWo4aXgwMDBqM2Y1eWhxc29xMW9wIiwiaWF0IjoxNzUwMjE5NTYyLCJleHAiOjE3NTE0MjkxNjJ9.8gKM2oMquccM_HDEfBAgtapCGf3M1eIp6SZ_knx7d1g
###
# username and password ok
POST http://localhost:7272/api/auth/sign-in
content-type: application/json
{
"email": "demo@minimals.cc",
"password": "@2Minimal"
}
###
# There is no user corresponding to the email address.
POST http://localhost:7272/api/auth/sign-in
content-type: application/json
@@ -15,7 +30,9 @@ content-type: application/json
}
###
# Wrong password
POST http://localhost:7272/api/auth/sign-in
content-type: application/json

View File

@@ -21,8 +21,8 @@ POST http://localhost:7272/api/party-user-auth/sign-in
content-type: application/json
{
"email": "demo@minimals.cc",
"password": "@2Minimal"
"email": "party_user0@prisma.io",
"password": "Aa12345678"
}
###

View File

@@ -2,14 +2,19 @@
###
GET http://localhost:7272/api/auth/me
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJjbWMwdWo4bGwwMDByM2Y1eXhob3JuMW1hIiwiaWF0IjoxNzUwMjE5NTgwLCJleHAiOjE3NTE0MjkxODB9.7BtuIKEvwDcHc5j9JYX0Eb1uB37kFH1Ksx4MTDTtEWQ
###
# username and password ok
POST http://localhost:7272/api/party-user-auth/sign-in
content-type: application/json
{
"email": "demo@minimals.cc",
"password": "@2Minimal"
"email": "party_user0@prisma.io",
"password": "Aa12345678"
}
###