UNPKG

authservice-nextjs

Version:

Next.js SDK for Auth Service - Server and client-side authentication with App Router support

178 lines 7.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NextAuthMiddleware = void 0; const authservice_node_1 = require("authservice-node"); class NextAuthMiddleware { constructor(auth) { this.auth = auth; } withAuth(handler) { return async (req, res) => { try { const user = await this.auth.getUserFromRequest(req); if (!user) { return res.status(401).json({ error: 'Unauthorized', message: 'Authentication required', }); } req.user = user; return handler(req, res); } catch (error) { console.error('Authentication error:', error); return res.status(500).json({ error: 'Internal Server Error', message: 'Authentication service unavailable', }); } }; } withPermission(permission, options) { return (handler) => { return async (req, res) => { try { const token = this.auth.getTokenFromRequest(req); if (!token) { return res.status(401).json({ error: 'Unauthorized', message: 'Authentication required', }); } const client = this.auth.getClient(); const result = await client.checkPermission({ userToken: token, permission, context: { ip: req.socket.remoteAddress, userAgent: req.headers['user-agent'], path: req.url, method: req.method, }, }); if (!result.allowed) { if (options?.onError) { await options.onError(new authservice_node_1.PermissionError(`Missing required permission: ${permission}`, [permission])); } return res.status(403).json({ error: 'Forbidden', message: `Missing required permission: ${permission}`, required: permission, }); } if (!req.user) { req.user = await this.auth.getUserFromRequest(req); } return handler(req, res); } catch (error) { if (options?.onError) { await options.onError(error); } if (error && typeof error === 'object' && 'statusCode' in error) { return res.status(error.statusCode || 500).json({ error: error.code, message: error.message, }); } console.error('Permission check error:', error); return res.status(500).json({ error: 'Internal Server Error', message: 'Permission service unavailable', }); } }; }; } withAnyPermission(permissions, options) { return (handler) => { return async (req, res) => { try { const token = this.auth.getTokenFromRequest(req); if (!token) { return res.status(401).json({ error: 'Unauthorized', message: 'Authentication required', }); } const client = this.auth.getClient(); const hasPermission = await client.hasAnyPermission(token, permissions); if (!hasPermission) { if (options?.onError) { await options.onError(new authservice_node_1.PermissionError(`Missing required permissions. Need any of: ${permissions.join(', ')}`, permissions)); } return res.status(403).json({ error: 'Forbidden', message: `Missing required permissions. Need any of: ${permissions.join(', ')}`, required: permissions, requireAny: true, }); } if (!req.user) { req.user = await this.auth.getUserFromRequest(req); } return handler(req, res); } catch (error) { if (options?.onError) { await options.onError(error); } console.error('Permission check error:', error); return res.status(500).json({ error: 'Internal Server Error', message: 'Permission service unavailable', }); } }; }; } withAllPermissions(permissions, options) { return (handler) => { return async (req, res) => { try { const token = this.auth.getTokenFromRequest(req); if (!token) { return res.status(401).json({ error: 'Unauthorized', message: 'Authentication required', }); } const client = this.auth.getClient(); const hasAllPermissions = await client.hasAllPermissions(token, permissions); if (!hasAllPermissions) { if (options?.onError) { await options.onError(new authservice_node_1.PermissionError(`Missing required permissions. Need all of: ${permissions.join(', ')}`, permissions)); } return res.status(403).json({ error: 'Forbidden', message: `Missing required permissions. Need all of: ${permissions.join(', ')}`, required: permissions, requireAll: true, }); } if (!req.user) { req.user = await this.auth.getUserFromRequest(req); } return handler(req, res); } catch (error) { if (options?.onError) { await options.onError(error); } console.error('Permission check error:', error); return res.status(500).json({ error: 'Internal Server Error', message: 'Permission service unavailable', }); } }; }; } compose(...middlewares) { return (handler) => { return middlewares.reduceRight((acc, middleware) => middleware(acc), handler); }; } } exports.NextAuthMiddleware = NextAuthMiddleware; //# sourceMappingURL=middleware.js.map