@intrasoft/auth-next
Version:
A robust and customizable authentication package for Next.js app (router) applications, providing JWT-based authentication and token management
177 lines (165 loc) • 5.37 kB
JavaScript
;
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);
// src/index.ts
var index_exports = {};
__export(index_exports, {
actions: () => actions_exports,
auth: () => auth,
client: () => client_exports,
server: () => server_exports
});
module.exports = __toCommonJS(index_exports);
// src/config.ts
function validateConfig(config) {
if (config.accessCookie) {
throw new Error("Missing or invalid accessLifetime in configuration");
}
}
var CACHED_SERVER_CONFIG = void 0;
function getConfig() {
if (CACHED_SERVER_CONFIG) return CACHED_SERVER_CONFIG;
const config = {
signInUrl: process.env.INTRASOFT_SIGNIN_URL,
accessCookie: process.env.INTRASOFT_ACCESS_TOKEN_LIFETIME,
refreshCookie: process.env.INTRASOFT_REFRESH_TOKEN_LIFETIME
};
validateConfig(config);
CACHED_SERVER_CONFIG = config;
return config;
}
// src/auth.ts
var import_headers = require("next/headers");
var import_navigation = require("next/navigation");
// src/tokens.ts
var import_auth_core = require("@intrasoft/auth-core");
async function decryptAuthToken(access) {
return await (0, import_auth_core.decodeJwt)(access);
}
async function revalidatedTokens(refresh) {
return await (0, import_auth_core.refreshToken)(refresh);
}
// src/auth.ts
async function auth() {
var _a, _b;
const config = getConfig();
const _c = await (0, import_headers.cookies)();
const tokens = {
access: (_a = _c.get(config.accessCookie)) == null ? void 0 : _a.value,
refresh: (_b = _c.get(config.accessCookie)) == null ? void 0 : _b.value
};
if (!isAuthenticated(tokens)) {
return (0, import_navigation.redirect)(config.signInUrl);
}
await refetchTokens(tokens);
const { payload: user } = await decryptAuthToken(tokens.access);
if (!user) {
return (0, import_navigation.redirect)(config.signInUrl);
}
return user;
}
function isAuthenticated(tokens) {
return Boolean(tokens.access || tokens.refresh);
}
async function refetchTokens(tokens) {
if (!tokens.refresh) {
return;
}
return await revalidatedTokens(tokens.refresh);
}
// src/client/index.ts
var client_exports = {};
__export(client_exports, {
useAuth: () => useAuth
});
// src/client/useAuth.ts
var import_react = __toESM(require("react"));
// src/actions.ts
var actions_exports = {};
__export(actions_exports, {
getCurrentUser: () => getCurrentUser,
loginUser: () => loginUser,
logoutUser: () => logoutUser
});
async function getCurrentUser() {
return await auth();
}
async function loginUser(payload) {
}
async function logoutUser() {
}
// src/client/useAuth.ts
function useAuth() {
const [user, setUser] = import_react.default.useState(null);
const getUser = import_react.default.useCallback(async () => {
try {
const response = await getCurrentUser();
setUser(response);
} catch (error) {
setUser(null);
}
}, []);
import_react.default.useEffect(() => {
document.addEventListener("DOMContentLoaded", getUser);
document.addEventListener("visibilitychange", getUser);
return () => {
document.removeEventListener("DOMContentLoaded", getUser);
document.removeEventListener("visibilitychange", getUser);
};
}, []);
return { user };
}
// src/server/index.ts
var server_exports = {};
__export(server_exports, {
SignIn: () => SignIn,
SignOut: () => SignOut
});
// src/server/SignIn.tsx
var import_navigation2 = require("next/navigation");
async function SignIn(props) {
const user = await auth();
const config = getConfig();
if (!user) (0, import_navigation2.redirect)(props.redirectUrl || config.signInUrl);
return props.children;
}
// src/server/SignOut.tsx
var import_navigation3 = require("next/navigation");
async function SignOut(props) {
const user = await auth();
if (user) (0, import_navigation3.redirect)(props.redirectUrl || "/");
return props.children;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
actions,
auth,
client,
server
});
//# sourceMappingURL=index.js.map