Core authentication functions in @warpy-auth-sdk/core.
@warpy-auth-sdk/core provides several core functions for authentication. These are the main functions you'll use in your application.
Main authentication function that handles OAuth, email, and MCP agent login flows
authenticate(config: AuthConfig, request?: Request, payload?: MCPLoginPayload): Promise<AuthenticateResult>configAuthConfigrequiredAuthentication configuration with provider and secret
requestRequestHTTP request object (for OAuth/email flows)
payloadMCPLoginPayloadMCP agent login payload (for agent authentication)
Promise<AuthenticateResult>import { authenticate, google } from '@warpy-auth-sdk/core';
// OAuth authentication
const result = await authenticate(
{
secret: process.env.AUTH_SECRET!,
provider: google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
}),
},
request
);
if (result.session) {
// User is authenticated
console.log('User:', result.session.user);
} else if (result.redirectUrl) {
// Redirect to OAuth provider
return Response.redirect(result.redirectUrl);
}Retrieve the current session from request cookies
getSession(request: Request, secret: string): Promise<Session | null>requestRequestrequiredHTTP request with cookies
secretstringrequiredJWT signing secret
Promise<Session | null>import { getSession } from '@warpy-auth-sdk/core';
export async function GET(request: Request) {
const session = await getSession(request, process.env.AUTH_SECRET!);
if (!session) {
return Response.json({ error: 'Not authenticated' }, { status: 401 });
}
return Response.json({ user: session.user });
}Sign out the user and clear session cookies
signOut(request: Request, config: AuthConfig): Promise<void>requestRequestrequiredHTTP request
configAuthConfigrequiredAuthentication configuration
Promise<void>import { signOut } from '@warpy-auth-sdk/core';
export async function POST(request: Request) {
await signOut(request, {
secret: process.env.AUTH_SECRET!,
provider: google({ /* ... */ }),
});
return Response.json({ success: true });
}Verify MCP agent token from Authorization header
verifyAgentToken(request: Request, secret: string): Promise<Session | null>requestRequestrequiredHTTP request with Authorization header
secretstringrequiredJWT signing secret
Promise<Session | null>import { verifyAgentToken } from '@warpy-auth-sdk/core';
export async function GET(request: Request) {
const session = await verifyAgentToken(request, process.env.AUTH_SECRET!);
if (!session || !session.scopes?.includes('read')) {
return Response.json({ error: 'Unauthorized' }, { status: 401 });
}
return Response.json({ data: 'Protected data' });
}Core types used throughout @warpy-auth-sdk/core:
interface AuthConfig {
secret: string;
provider: Provider;
adapter?: Adapter;
callbacks?: {
user?: (user: User, context: { provider: string }) => Promise<User>;
jwt?: (token: JWT) => JWT;
session?: (session: Session) => Session;
};
}
interface Session {
user: {
id: string;
email: string;
name?: string;
picture?: string;
};
expires: Date;
token?: string;
type?: 'standard' | 'mcp-agent';
scopes?: string[];
agentId?: string;
}
interface AuthenticateResult {
session?: Session;
error?: string;
redirectUrl?: string;
}All core functions can throw errors. Handle them appropriately:
try {
const session = await getSession(request, process.env.AUTH_SECRET!);
// Handle session
} catch (error) {
console.error('Authentication error:', error);
return Response.json({ error: 'Authentication failed' }, { status: 500 });
}