mirror of
https://github.com/vercel/commerce.git
synced 2025-05-12 20:57:51 +00:00
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import type { NextRequest } from 'next/server';
|
|
import { isLoggedIn, getOrigin, authorize, logout } from 'lib/shopify/auth';
|
|
|
|
// This function can be marked `async` if using `await` inside
|
|
export async function middleware(request: NextRequest) {
|
|
/****
|
|
Authorize Middleware to get access tokens
|
|
*****/
|
|
if (request.nextUrl.pathname.startsWith('/authorize')) {
|
|
console.log('Running Initial Authorization Middleware');
|
|
const origin = getOrigin(request);
|
|
console.log('origin', origin);
|
|
return await authorize(request, origin);
|
|
}
|
|
/****
|
|
END OF Authorize Middleware to get access tokens
|
|
*****/
|
|
|
|
/****
|
|
LOGOUT -
|
|
*****/
|
|
if (request.nextUrl.pathname.startsWith('/logout')) {
|
|
console.log('Running Logout middleware');
|
|
const origin = getOrigin(request);
|
|
return await logout(request, origin);
|
|
}
|
|
/****
|
|
END OF LOGOUT
|
|
*****/
|
|
/****
|
|
Account
|
|
*****/
|
|
|
|
if (request.nextUrl.pathname.startsWith('/account')) {
|
|
console.log('Running Account middleware');
|
|
//const newHeaders = new Headers(request.headers)
|
|
const origin = getOrigin(request);
|
|
//console.log ("origin", origin)
|
|
return await isLoggedIn(request, origin);
|
|
}
|
|
|
|
/****
|
|
END OF Account
|
|
*****/
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/authorize', '/logout', '/account/:path*']
|
|
};
|