Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/components/Header/AuthModal.client.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@ import { useAtproto } from '~/composables/atproto/useAtproto'
import { authRedirect } from '~/utils/atproto/helpers'

const handleInput = shallowRef('')

const route = useRoute()
const { user, logout } = useAtproto()

async function handleBlueskySignIn() {
await authRedirect('https://bsky.social')
await navigateTo(
{
path: '/api/auth/atproto',
query: { handle: 'https://bsky.social', returnTo: route.fullPath },
},
{ external: true },
)
}

async function handleCreateAccount() {
await authRedirect('https://npmx.social', true)
await navigateTo(
{
path: '/api/auth/atproto',
query: { handle: 'https://npmx.social', create: 'true', returnTo: route.fullPath },
},
{ external: true },
)
}

async function handleLogin() {
Expand Down
18 changes: 17 additions & 1 deletion server/api/auth/atproto.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export default defineEventHandler(async event => {
}

const query = getQuery(event)
const rawReturnTo = query.returnTo?.toString() || '/'
// Validate returnTo is a safe relative path (prevent open redirect)
const isRelativePath =
rawReturnTo.startsWith('/') && !rawReturnTo.startsWith('//') && !rawReturnTo.includes(':')
const returnTo = isRelativePath ? rawReturnTo : '/'

setCookie(event, 'auth_return_to', returnTo, {
maxAge: 60 * 5,
httpOnly: true,
// secure only if NOT in dev mode
secure: !import.meta.dev,
})

const clientMetadata = getOauthClientMetadata()
const session = await useServerSession(event)
const { stateStore, sessionStore } = useOAuthStorage(session)
Expand Down Expand Up @@ -63,5 +76,8 @@ export default defineEventHandler(async event => {
})
}

return sendRedirect(event, '/')
const returnToURL = getCookie(event, 'auth_return_to') || '/'
deleteCookie(event, 'auth_return_to')

return sendRedirect(event, returnToURL)
})
Loading