31 lines
811 B
TypeScript
31 lines
811 B
TypeScript
import type { NextFunction, Request, Response } from 'express';
|
|
|
|
import { verifyDeviceToken } from '../utils/device-token';
|
|
|
|
const extractBearerToken = (authorizationHeader?: string): string | null => {
|
|
if (!authorizationHeader || !authorizationHeader.startsWith('Bearer ')) {
|
|
return null;
|
|
}
|
|
|
|
return authorizationHeader.slice('Bearer '.length).trim();
|
|
};
|
|
|
|
export const requireDeviceAuth = (req: Request, res: Response, next: NextFunction): void => {
|
|
const token = extractBearerToken(req.headers.authorization);
|
|
|
|
if (!token) {
|
|
res.status(401).json({ message: 'Missing bearer device token' });
|
|
return;
|
|
}
|
|
|
|
const payload = verifyDeviceToken(token);
|
|
|
|
if (!payload) {
|
|
res.status(401).json({ message: 'Invalid device token' });
|
|
return;
|
|
}
|
|
|
|
req.deviceAuth = payload;
|
|
next();
|
|
};
|