@archlast/sdk-next
v0.1.1
Published
Archlast SDK for Next.js with SSR support
Readme
@archlast/sdk-next
Archlast SDK for Next.js with SSR (Server-Side Rendering) support.
Installation
npm install @archlast/sdk-nextFeatures
- SSR Session Reading: Get session data in server components and route handlers
- Middleware Helpers: Cookie-based authentication checks for protected routes
- Better-Auth Wrapped: No need to install
better-authdirectly - App Router Support: Built for Next.js 15+ App Router
- Type-Safe: Full TypeScript support
Usage
Server Components
import { getServerSession } from "@archlast/sdk-next";
export default async function Profile() {
const session = await getServerSession();
if (!session) {
return <div>Please sign in</div>;
}
return <div>Welcome, {session.user.name}!</div>;
}Route Handlers
import { getServerSession } from "@archlast/sdk-next";
import { NextResponse } from "next/server";
export async function GET() {
const session = await getServerSession();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
return NextResponse.json({ user: session.user });
}Middleware
// middleware.ts
import { requireAuth } from "@archlast/sdk-next";
import { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
// Protect all routes except /login and /api/auth
if (
!request.nextUrl.pathname.startsWith("/login") &&
!request.nextUrl.pathname.startsWith("/api/auth")
) {
const authResult = requireAuth(request, "/login");
if (authResult) return authResult;
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico).*)"],
};Client Components
"use client";
import { useAuth } from "@archlast/sdk-next/react";
export default function Profile() {
const { session, isLoading } = useAuth();
if (isLoading) return <div>Loading...</div>;
if (!session) return <div>Please sign in</div>;
return <div>Welcome, {session.user.name}!</div>;
}Using Better-Auth Directly
"use client";
import { authClient } from "@archlast/sdk-next/auth";
export default function LoginForm() {
const handleSubmit = async () => {
await authClient.signIn.email({
email: "[email protected]",
password: "password",
});
};
return <button onClick={handleSubmit}>Sign In</button>;
}Environment Variables
# Optional: Set Better-Auth server URL
# Defaults to http://localhost:4000/api/auth
NEXT_PUBLIC_BETTER_AUTH_URL=http://localhost:4000
# Or use Archlast URL
NEXT_PUBLIC_ARCHLAST_HTTP_URL=http://localhost:4000API
Server Functions
getServerSession()- Get session in server components/route handlersgetCookieHeader()- Get cookies as string for forwardinggetSessionHeaders()- Get headers for authenticated requests
Middleware
createMiddlewareClient(request)- Create middleware clientrequireAuth(request, redirectTo?)- Helper to protect routes
Auth
authClient- Better-Auth client for sign in/outuseSession()- Hook for session statecreateAuthClient(options)- Create custom auth client
Examples
Protected Server Component
import { getServerSession } from "@archlast/sdk-next";
import { redirect } from "next/navigation";
export default async function Dashboard() {
const session = await getServerSession();
if (!session) {
redirect("/login");
}
return (
<div>
<h1>Dashboard</h1>
<p>Welcome, {session.user.name}!</p>
</div>
);
}API Route with Session
import { getServerSession } from "@archlast/sdk-next";
import { NextResponse } from "next/server";
export async function POST(request: Request) {
const session = await getServerSession();
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
// Process request...
return NextResponse.json({ success: true });
}License
MIT
