UNPKG

@duongtrungnguyen/next-helper

Version:
95 lines 3.85 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var middleware_exports = {}; __export(middleware_exports, { authMiddleware: () => authMiddleware }); module.exports = __toCommonJS(middleware_exports); var import_server = require("next/server"); var jose = __toESM(require("jose")); var import_configs = require("../configs"); var import_utils = require("../utils"); async function authMiddleware(request) { var _a, _b, _c; const response = import_server.NextResponse.next(); const accessToken = (_a = request.cookies.get(import_configs.libConfig.auth.cookies.accessToken)) == null ? void 0 : _a.value; const refreshToken = (_b = request.cookies.get(import_configs.libConfig.auth.cookies.refreshToken)) == null ? void 0 : _b.value; if (!refreshToken) { return response; } try { if (accessToken) { const decodedToken = jose.decodeJwt(accessToken); const expiresAt = (_c = decodedToken.exp) != null ? _c : 0; const now = Math.floor(Date.now() / 1e3); if (expiresAt - now > 60) { return response; } } const refreshResponse = await fetch( `${import_configs.libConfig.baseUrl}${import_configs.libConfig.auth.globalPrefix}${import_configs.libConfig.auth.endpoints.refresh}`, { method: "POST", headers: { Authorization: (0, import_utils.parseToken)(refreshToken), "Content-Type": "application/json" } } ); if (!refreshResponse.ok) { console.error("Failed to refresh token:", refreshResponse.status); return response; } const data = await refreshResponse.json(); if (!(data == null ? void 0 : data.accessToken) || !(data == null ? void 0 : data.refreshToken)) { console.error("Invalid refresh response:", data); return response; } response.cookies.set(import_configs.libConfig.auth.cookies.accessToken, data.accessToken, { httpOnly: true, secure: process.env.NODE_ENV === "production", path: "/", sameSite: "strict" }); response.cookies.set(import_configs.libConfig.auth.cookies.refreshToken, data.refreshToken, { httpOnly: true, secure: process.env.NODE_ENV === "production", path: "/", sameSite: "strict" }); } catch (error) { console.error("Error refreshing token in middleware:", error); } return response; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { authMiddleware }); //# sourceMappingURL=middleware.js.map