Initial commit
This commit is contained in:
commit
b3a51a4115
10336 changed files with 2381973 additions and 0 deletions
10
backend/dist/middleware/auth.d.ts
vendored
Normal file
10
backend/dist/middleware/auth.d.ts
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
export interface AuthRequest extends Request {
|
||||
user?: {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
export declare const authenticate: (req: AuthRequest, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>>>;
|
||||
export declare const optionalAuth: (req: AuthRequest, res: Response, next: NextFunction) => Promise<void>;
|
||||
54
backend/dist/middleware/auth.js
vendored
Normal file
54
backend/dist/middleware/auth.js
vendored
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.optionalAuth = exports.authenticate = void 0;
|
||||
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
||||
const index_js_1 = require("../index.js");
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'treasure-trails-secret-key';
|
||||
const authenticate = async (req, res, next) => {
|
||||
try {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return res.status(401).json({ error: 'No token provided' });
|
||||
}
|
||||
const token = authHeader.split(' ')[1];
|
||||
const decoded = jsonwebtoken_1.default.verify(token, JWT_SECRET);
|
||||
const user = await index_js_1.prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
select: { id: true, email: true, name: true }
|
||||
});
|
||||
if (!user) {
|
||||
return res.status(401).json({ error: 'User not found' });
|
||||
}
|
||||
req.user = user;
|
||||
next();
|
||||
}
|
||||
catch (error) {
|
||||
return res.status(401).json({ error: 'Invalid token' });
|
||||
}
|
||||
};
|
||||
exports.authenticate = authenticate;
|
||||
const optionalAuth = async (req, res, next) => {
|
||||
try {
|
||||
const authHeader = req.headers.authorization;
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
return next();
|
||||
}
|
||||
const token = authHeader.split(' ')[1];
|
||||
const decoded = jsonwebtoken_1.default.verify(token, JWT_SECRET);
|
||||
const user = await index_js_1.prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
select: { id: true, email: true, name: true }
|
||||
});
|
||||
if (user) {
|
||||
req.user = user;
|
||||
}
|
||||
next();
|
||||
}
|
||||
catch {
|
||||
next();
|
||||
}
|
||||
};
|
||||
exports.optionalAuth = optionalAuth;
|
||||
Loading…
Add table
Add a link
Reference in a new issue