Fix auth imports and add proper admin access control - create auth-utils.js with verifyToken function
This commit is contained in:
parent
49cfa31253
commit
8d7eb67a97
6 changed files with 66 additions and 5 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { NextResponse } from 'next/server';
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { verifyToken } from '../setup-auth.js';
|
||||
import { verifyToken, isAdmin } from '../auth-utils.js';
|
||||
|
||||
// Rate limiting for external APIs
|
||||
const rateLimiters = {
|
||||
|
|
@ -291,6 +291,10 @@ export async function GET(request) {
|
|||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!isAdmin(user)) {
|
||||
return NextResponse.json({ error: 'Admin access required' }, { status: 403 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const action = searchParams.get('action');
|
||||
|
||||
|
|
@ -353,6 +357,10 @@ export async function POST(request) {
|
|||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!isAdmin(user)) {
|
||||
return NextResponse.json({ error: 'Admin access required' }, { status: 403 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const action = searchParams.get('action');
|
||||
|
||||
|
|
|
|||
53
api/auth-utils.js
Normal file
53
api/auth-utils.js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import jwt from 'jsonwebtoken';
|
||||
import { sql } from '@vercel/postgres';
|
||||
|
||||
export async function verifyToken(token) {
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const jwtSecret = process.env.JWT_SECRET || 'fallback-secret-change-in-production';
|
||||
const decoded = jwt.verify(token, jwtSecret);
|
||||
|
||||
// Get user from database
|
||||
const result = await sql.query(`
|
||||
SELECT
|
||||
up.user_id,
|
||||
up.username,
|
||||
up.email,
|
||||
up.first_name,
|
||||
up.last_name,
|
||||
up.avatar_url,
|
||||
up.roles,
|
||||
up.created_at,
|
||||
up.updated_at
|
||||
FROM user_preferences up
|
||||
WHERE up.user_id = $1
|
||||
`, [decoded.userId]);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const user = result.rows[0];
|
||||
return {
|
||||
id: user.user_id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
firstName: user.first_name,
|
||||
lastName: user.last_name,
|
||||
avatarUrl: user.avatar_url,
|
||||
roles: user.roles || [],
|
||||
createdAt: user.created_at,
|
||||
updatedAt: user.updated_at
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Token verification failed:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function isAdmin(user) {
|
||||
return user && user.roles && user.roles.includes('admin');
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextResponse } from 'next/server';
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { verifyToken } from '../setup-auth.js';
|
||||
import { verifyToken } from '../auth-utils.js';
|
||||
|
||||
// GET /api/cards - Search cards from database
|
||||
export async function GET(request) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextResponse } from 'next/server';
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { verifyToken } from '../setup-auth.js';
|
||||
import { verifyToken } from '../auth-utils.js';
|
||||
|
||||
// GET /api/collections - Get user collections
|
||||
export async function GET(request) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextResponse } from 'next/server';
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { verifyToken } from './setup-auth.js';
|
||||
import { verifyToken } from './auth-utils.js';
|
||||
|
||||
// GET /api/user-cards - Get user's cards with optional filters
|
||||
export async function GET(request) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextResponse } from 'next/server';
|
||||
import { sql } from '@vercel/postgres';
|
||||
import { verifyToken } from '../setup-auth.js';
|
||||
import { verifyToken } from '../auth-utils.js';
|
||||
|
||||
// GET /api/user-cards/[id] - Get single user card
|
||||
export async function GET(request, { params }) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue