feat: Gantiin auth API untuk dapatin Profile

This commit is contained in:
Rafi Athallah
2026-06-01 12:08:39 +07:00
parent 1bd74f4b46
commit 5810dcfbb6
5 changed files with 20 additions and 9 deletions
+7 -1
View File
@@ -4,6 +4,12 @@ node_modules/
# Production Build Output # Production Build Output
dist/ dist/
# Uploads
uploads/docs/*
!uploads/docs/.gitkeep
uploads/images/*
!uploads/images/.gitkeep
# Local Environment variables # Local Environment variables
.env .env
.env.local .env.local
@@ -34,4 +40,4 @@ lerna-debug.log*
Thumbs.db Thumbs.db
# Prisma # Prisma
/src/generated/prisma /src/generated/prisma
+3 -1
View File
@@ -8,7 +8,9 @@ import { errorHandler, CustomError } from './middleware/errorHandler';
const app = express(); const app = express();
app.use(helmet()); app.use(helmet({
crossOriginResourcePolicy: { policy: 'cross-origin' }
}));
app.use(cors({ app.use(cors({
origin: true, origin: true,
credentials: true, credentials: true,
+8 -7
View File
@@ -161,11 +161,11 @@ router.post('/login', async (req: Request, res: Response, next: NextFunction) =>
}); });
/** /**
* @route GET /api/auth/profile * @route GET /api/auth/me
* @desc Dapetin profil user yg lagi login * @desc Dapetin info ringkas user yg login buat navbar
* @access Private * @access Private
*/ */
router.get('/profile', protect, async (req: AuthenticatedRequest, res: Response, next: NextFunction) => { router.get('/me', protect, async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
try { try {
const userId = req.user?.id; const userId = req.user?.id;
@@ -178,20 +178,21 @@ router.get('/profile', protect, async (req: AuthenticatedRequest, res: Response,
}); });
if (!user) { if (!user) {
const error: CustomError = new Error('User tidak ditemukan'); const error: CustomError = new Error('User not found');
error.statusCode = 404; error.statusCode = 404;
return next(error); return next(error);
} }
const profile = user.role === 'ADMIN' ? user.admin : user.mahasiswa; const profile = user.role === 'ADMIN' ? user.admin : user.mahasiswa;
const namaDepan = profile?.namaDepan || '';
res.status(200).json({ res.status(200).json({
success: true, status: true,
user: { data: {
id: user.id, id: user.id,
email: user.email, email: user.email,
role: user.role, role: user.role,
profile, nama_depan: namaDepan
}, },
}); });
} catch (error) { } catch (error) {
+1
View File
@@ -0,0 +1 @@
# Keep directory
+1
View File
@@ -0,0 +1 @@
# Keep directory