Skip to content

Commit 0d77460

Browse files
atinuxlarbish
andcommitted
feat: add support for redirect query parameter
Co-Authored-By: Baptiste Leproux <[email protected]>
1 parent 22ae221 commit 0d77460

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/module/src/runtime/server/routes/admin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { eventHandler, sendRedirect } from 'h3'
1+
import { eventHandler, getQuery, sendRedirect, setCookie } from 'h3'
22

33
export default eventHandler((event) => {
4+
const { redirect } = getQuery(event)
5+
if (redirect) {
6+
setCookie(event, 'studio-redirect', String(redirect), {
7+
httpOnly: true,
8+
})
9+
}
410
// Automatically redirect to the GitHub login page
511
// TODO: once there is more than one auth provider, we need to add a selector for the auth provider
612
return sendRedirect(event, '/__nuxt_studio/auth/github')

src/module/src/runtime/server/routes/auth/github.get.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ export default eventHandler(async (event: H3Event) => {
209209
},
210210
}, session.data))
211211

212+
const redirect = decodeURIComponent(getCookie(event, 'studio-redirect') || '')
213+
deleteCookie(event, 'studio-redirect')
214+
// make sure the redirect is a valid relative path (avoid also // which is not a valid URL)
215+
if (redirect && redirect.startsWith('/') && !redirect.startsWith('//')) {
216+
return sendRedirect(event, redirect)
217+
}
218+
212219
return sendRedirect(event, '/')
213220
})
214221

src/module/src/runtime/utils/activation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function defineStudioActivationPlugin(onStudioActivation: (user: St
2525
// Listen to CMD + . to toggle the studio or redirect to the login page
2626
document.addEventListener('keydown', (event) => {
2727
if (event.metaKey && event.key === '.') {
28-
window.location.href = config.route
28+
window.location.href = config.route + '?redirect=' + encodeURIComponent(window.location.pathname)
2929
}
3030
})
3131
}

0 commit comments

Comments
 (0)