feat(devices): add registration, heartbeat, linking, and device tokens
This commit is contained in:
30
Backend/middleware/device-auth.ts
Normal file
30
Backend/middleware/device-auth.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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();
|
||||
};
|
||||
Reference in New Issue
Block a user