-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incomplete implemention using Next.js middleware
- Loading branch information
1 parent
b5a6e4f
commit 6a113a6
Showing
10 changed files
with
90 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type Config = { | ||
type: 'space-plugin' | 'tool-plugin'; | ||
oauth: boolean; | ||
}; | ||
|
||
const config: Config = { | ||
type: 'space-plugin', | ||
oauth: true, | ||
}; | ||
|
||
export default config; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { NextResponse } from 'next/server'; | ||
import type { NextRequest } from 'next/server'; | ||
import appConfig from '@/config'; | ||
import { initOauthFlowUrl, authParams } from '@/auth'; | ||
import { APP_BRIDGE_TOKEN_HEADER_KEY } from '@/const'; | ||
import { verifyAppBridgeToken } from '@/utils/server'; | ||
|
||
// Limit the middleware to paths starting with `/api/` | ||
export const config = { | ||
// matcher: '/api/:function*', | ||
}; | ||
|
||
const SKIP_AUTH_FOR = ['/api/_app_bridge', '/api/_oauth']; | ||
|
||
export async function middleware(request: NextRequest) { | ||
// handle 401 error after the initial oauth flow | ||
if ( | ||
request.nextUrl.pathname === '/401' && | ||
request.headers.get('Referer') === 'https://app.storyblok.com/' | ||
) { | ||
return NextResponse.redirect( | ||
new URL( | ||
appConfig.type === 'tool-plugin' | ||
? 'https://app.storyblok.com/oauth/tool_redirect' | ||
: 'https://app.storyblok.com/oauth/app_redirect', | ||
), | ||
); | ||
} | ||
|
||
// verify App Bridge token for all API routes | ||
const pathname = request.nextUrl.pathname; | ||
if (!pathname.startsWith('/api/')) { | ||
return NextResponse.next(); | ||
} | ||
if (SKIP_AUTH_FOR.includes(pathname)) { | ||
return NextResponse.next(); | ||
} | ||
if ( | ||
[initOauthFlowUrl, `${authParams.endpointPrefix}/callback`].includes( | ||
pathname, | ||
) | ||
) { | ||
return NextResponse.next(); | ||
} | ||
const token = request.headers.get(APP_BRIDGE_TOKEN_HEADER_KEY); | ||
const result = await verifyAppBridgeToken(token || ''); | ||
if (result.ok) { | ||
return NextResponse.next(); | ||
} else { | ||
return new NextResponse(null, { | ||
status: 401, | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters