@duongtrungnguyen/next-helper
Version:
Helper library for Next.js 15
35 lines • 1.08 kB
JavaScript
import React from "react";
import { auth, getCurrentUser } from "./server";
import { redirect } from "next/navigation";
const SignedIn = async ({ children }) => {
const user = await getCurrentUser();
return user ? /* @__PURE__ */ React.createElement(React.Fragment, null, children) : null;
};
const SignedOut = async ({ children }) => {
const user = await getCurrentUser();
return user ? null : /* @__PURE__ */ React.createElement(React.Fragment, null, children);
};
const Protect = async ({
children,
fallback,
condition = (user) => !!user,
redirectTo
}) => {
const { user } = await auth();
const unauthorized = fallback ? /* @__PURE__ */ React.createElement(React.Fragment, null, fallback) : null;
const authorized = /* @__PURE__ */ React.createElement(React.Fragment, null, children);
const isAuthenticated = condition(user);
if (!isAuthenticated) {
if (redirectTo) {
redirect(redirectTo);
}
return unauthorized;
}
return authorized;
};
export {
Protect,
SignedIn,
SignedOut
};
//# sourceMappingURL=server-components.js.map