authservice-nextjs
Version:
Next.js SDK for Auth Service - Server and client-side authentication with App Router support
127 lines • 4.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextAuthAppRouter = void 0;
exports.createAppRouterAuth = createAppRouterAuth;
const headers_1 = require("next/headers");
const navigation_1 = require("next/navigation");
const react_1 = require("react");
const authservice_node_1 = require("authservice-node");
class NextAuthAppRouter {
constructor(config) {
this.getCurrentUser = (0, react_1.cache)(async () => {
const cookieStore = (0, headers_1.cookies)();
const token = cookieStore.get(this.config.cookieName);
if (!token?.value) {
return null;
}
try {
const permissions = await this.client.getUserPermissions(token.value);
return {
id: permissions.userId,
permissions: permissions.permissions.map((p) => `${p.resource}:${p.action}`),
roles: permissions.roles,
};
}
catch {
return null;
}
});
this.client = new authservice_node_1.AuthServiceClient(config);
this.config = {
...config,
cookieName: config.cookieName || 'auth-token',
cookieDomain: config.cookieDomain || '',
cookieSecure: config.cookieSecure ?? process.env.NODE_ENV === 'production',
cookieHttpOnly: config.cookieHttpOnly ?? true,
cookieSameSite: config.cookieSameSite || 'lax',
cookiePath: config.cookiePath || '/',
loginUrl: config.loginUrl || '/login',
authServiceUrl: config.authServiceUrl,
appId: config.appId,
appSecret: config.appSecret,
unauthorizedUrl: config.unauthorizedUrl || '/unauthorized',
redirectOnError: config.redirectOnError ?? true,
sessionCookieName: config.sessionCookieName || 'auth-session',
};
}
async requireAuth() {
const user = await this.getCurrentUser();
if (!user) {
(0, navigation_1.redirect)(this.config.loginUrl);
}
return user;
}
async requirePermission(permission) {
const cookieStore = (0, headers_1.cookies)();
const token = cookieStore.get(this.config.cookieName);
if (!token?.value) {
(0, navigation_1.redirect)(this.config.loginUrl);
}
const result = await this.client.checkPermission({
userToken: token.value,
permission,
});
if (!result.allowed) {
(0, navigation_1.redirect)(this.config.unauthorizedUrl);
}
return this.getCurrentUser();
}
async requireAnyPermission(permissions) {
const cookieStore = (0, headers_1.cookies)();
const token = cookieStore.get(this.config.cookieName);
if (!token?.value) {
(0, navigation_1.redirect)(this.config.loginUrl);
}
const hasPermission = await this.client.hasAnyPermission(token.value, permissions);
if (!hasPermission) {
(0, navigation_1.redirect)(this.config.unauthorizedUrl);
}
return this.getCurrentUser();
}
async requireAllPermissions(permissions) {
const cookieStore = (0, headers_1.cookies)();
const token = cookieStore.get(this.config.cookieName);
if (!token?.value) {
(0, navigation_1.redirect)(this.config.loginUrl);
}
const hasAllPermissions = await this.client.hasAllPermissions(token.value, permissions);
if (!hasAllPermissions) {
(0, navigation_1.redirect)(this.config.unauthorizedUrl);
}
return this.getCurrentUser();
}
async hasPermission(permission) {
const cookieStore = (0, headers_1.cookies)();
const token = cookieStore.get(this.config.cookieName);
if (!token?.value) {
return false;
}
try {
const result = await this.client.checkPermission({
userToken: token.value,
permission,
});
return result.allowed;
}
catch {
return false;
}
}
withAuth(action) {
return (async (...args) => {
const user = await this.requireAuth();
return action(user, ...args);
});
}
withPermission(permission, action) {
return (async (...args) => {
const user = await this.requirePermission(permission);
return action(user, ...args);
});
}
}
exports.NextAuthAppRouter = NextAuthAppRouter;
function createAppRouterAuth(config) {
return new NextAuthAppRouter(config);
}
//# sourceMappingURL=server.js.map