export const AuthMiddlewareImpl = Layer.succeed(
AuthMiddleware,
AuthMiddleware.of(({ headers }) =>
Effect.gen(function* () {
const authHeader = headers.authorization
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return yield* new UnauthorizedError()
}
const token = authHeader.substring(7).trim()
const device = yield* validateDeviceToken(token).pipe(Effect.mapError(() => new UnauthorizedError()))
return {
id: device.id,
locationId: device.locationId,
type: device.type,
name: device.name,
deviceInfo: device.deviceInfo,
connectedAt: device.connectedAt,
lastSeen: device.lastSeen,
}
}),
),
)
export const AuthMiddlewareImpl = Layer.succeed(
AuthMiddleware,
AuthMiddleware.of(({ headers }) =>
Effect.gen(function* () {
const authHeader = headers.authorization
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return yield* new UnauthorizedError()
}
const token = authHeader.substring(7).trim()
const device = yield* validateDeviceToken(token).pipe(Effect.mapError(() => new UnauthorizedError()))
return {
id: device.id,
locationId: device.locationId,
type: device.type,
name: device.name,
deviceInfo: device.deviceInfo,
connectedAt: device.connectedAt,
lastSeen: device.lastSeen,
}
}),
),
)