Next.js SDK Reference
This page documents all the exports from the @myauth/next package.
auth()
A server-side function to get the current session data including user and token.
import { auth } from "@myauth/next";
const session = await auth();
// Returns { user: User | null, token: string | null } | nullAuthProvider
A React context provider that manages authentication state across your app.
<AuthProvider
initialSession={session}
clientId={process.env.NEXT_PUBLIC_CLIENT_ID!}
>
{children}
</AuthProvider>useAuth()
A React hook that provides access to the current authentication state.
const { user, token, loading, login, logout } = useAuth();withAuthMiddleware()
A middleware function to protect routes that require authentication.
import { withAuthMiddleware } from "@myauth/next";
export default withAuthMiddleware(process.env.NEXT_PUBLIC_CLIENT_ID!);AuthenticateWithRedirectCallback
A React component that handles the authentication callback after redirect.
<AuthenticateWithRedirectCallback />createAuthCallbackHandler()
Creates an API route handler for exchanging authorization codes for tokens.
export const POST = createAuthCallbackHandler({
clientId: process.env.NEXT_PUBLIC_CLIENT_ID!,
clientSecret: process.env.NEXT_PUBLIC_CLIENT_SECRET!,
});getSessionToken()
Retrieves the current session token from storage.
import { getSessionToken } from "@myauth/next";
const token = getSessionToken();Types
Exported TypeScript types for type safety.
interface User {
email: string;
}
interface AuthState {
user: User | null;
token: string | null;
loading: boolean;
logout: () => Promise<void>;
login: () => Promise<void>;
}
interface Session {
user: User | null;
token: string | null;
}