handleSignIn - Initiate OAuth sign inhandleCallback - Process OAuth callbackhandleLogout - Sign out and clear sessionhandleRefresh - Refresh tokensimport {
handleSignIn,
handleCallback,
handleLogout,
handleRefresh,
} from 'authsafe-nextjs/server';async function handleSignIn(request: Request): Promise<Response>;// app/api/auth/signin/route.ts
import { handleSignIn } from 'authsafe-nextjs/server';
export async function GET(request: Request) {
return handleSignIn(request);
}// pages/api/auth/signin.ts
import { handleSignIn } from 'authsafe-nextjs/server';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const request = new Request(`http://localhost${req.url}`, {
method: req.method,
headers: req.headers as any,
});
const response = await handleSignIn(request);
res.writeHead(response.status, {
Location: response.headers.get('Location')!,
});
res.end();
}returnTo - Redirect URL after successful sign in/api/auth/signin?returnTo=/dashboardasync function handleCallback(request: Request): Promise<Response>;// app/api/auth/callback/route.ts
import { handleCallback } from 'authsafe-nextjs/server';
export async function GET(request: Request) {
return handleCallback(request);
}// pages/api/auth/callback.ts
import { handleCallback } from 'authsafe-nextjs/server';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const request = new Request(`http://localhost${req.url}`, {
method: req.method,
headers: req.headers as any,
});
const response = await handleCallback(request);
// Copy cookies and redirect
response.headers.forEach((value, key) => {
res.setHeader(key, value);
});
res.writeHead(response.status);
res.end();
}/api/auth/signin/api/auth/callback?code=...handleCallback exchanges code for tokensasync function handleLogout(request: Request): Promise<Response>;// app/api/auth/logout/route.ts
import { handleLogout } from 'authsafe-nextjs/server';
export async function POST(request: Request) {
return handleLogout(request);
}// pages/api/auth/logout.ts
import { handleLogout } from 'authsafe-nextjs/server';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const request = new Request(`http://localhost${req.url}`, {
method: req.method,
headers: req.headers as any,
});
const response = await handleLogout(request);
response.headers.forEach((value, key) => {
res.setHeader(key, value);
});
res.writeHead(response.status);
res.end();
}returnTo - Redirect URL after logoutawait fetch('/api/auth/logout?returnTo=/', { method: 'POST' });async function handleRefresh(request: Request): Promise<Response>;// app/api/auth/refresh/route.ts
import { handleRefresh } from 'authsafe-nextjs/server';
export async function POST(request: Request) {
return handleRefresh(request);
}// pages/api/auth/refresh.ts
import { handleRefresh } from 'authsafe-nextjs/server';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
const request = new Request(`http://localhost${req.url}`, {
method: req.method,
headers: req.headers as any,
});
const response = await handleRefresh(request);
const data = await response.json();
res.status(response.status).json(data);
}// Client-side (automatic)
const { refreshSession } = useAuth();
await refreshSession();// app/api/auth/signin/route.ts
import { handleSignIn } from 'authsafe-nextjs/server';
export async function GET(request: Request) {
return handleSignIn(request);
}// app/api/auth/callback/route.ts
import { handleCallback } from 'authsafe-nextjs/server';
export async function GET(request: Request) {
return handleCallback(request);
}// app/api/auth/logout/route.ts
import { handleLogout } from 'authsafe-nextjs/server';
export async function POST(request: Request) {
return handleLogout(request);
}// app/api/auth/refresh/route.ts
import { handleRefresh } from 'authsafe-nextjs/server';
export async function POST(request: Request) {
return handleRefresh(request);
}{
"error": "Missing client configuration"
}{
"error": "Invalid authorization code"
}{
"error": "No active session"
}{
"error": "No refresh token available"
}// app/api/auth/callback/route.ts
import { handleCallback } from 'authsafe-nextjs/server';
export async function GET(request: Request) {
try {
return await handleCallback(request);
} catch (error) {
console.error('Auth callback error:', error);
return new Response(JSON.stringify({ error: 'Authentication failed' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}// pages/api/auth/callback.ts
import { handleCallback } from 'authsafe-nextjs/server';
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
try {
const request = new Request(`http://localhost${req.url}`, {
method: req.method,
headers: req.headers as any,
});
const response = await handleCallback(request);
res.writeHead(response.status, {
Location: response.headers.get('Location')!,
});
res.end();
} catch (error) {
console.error('Auth error:', error);
res.status(500).json({ error: 'Authentication failed' });
}
}// app/api/auth/callback/route.ts
import { handleCallback } from 'authsafe-nextjs/server';
import { NextRequest } from 'next/server';
export async function GET(request: NextRequest) {
const response = await handleCallback(request);
// Override redirect based on user role
const url = new URL(response.headers.get('Location')!);
const session = await getAuth();
if (session && hasScope('admin')) {
url.pathname = '/admin/dashboard';
} else {
url.pathname = '/dashboard';
}
return Response.redirect(url);
}// app/api/auth/signin/route.ts
import { handleSignIn } from 'authsafe-nextjs/server';
export async function GET(request: Request) {
const url = new URL(request.url);
const returnTo = url.searchParams.get('returnTo');
console.log(`[Auth] Sign in initiated, returnTo: ${returnTo}`);
return handleSignIn(request);
}