Skip to content
Merged
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
22 changes: 20 additions & 2 deletions src/runtime/server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { subtle, getRandomValues } from 'uncrypto'
import type { OAuthProvider, OnError } from '#auth-utils'
import { createError } from '#imports'

// Determine if we are in development mode
const isDevelopment = process.env.NODE_ENV === 'development'

// OAuth cookie expiration time (10 minutes in seconds)
const OAUTH_COOKIE_MAX_AGE = 60 * 10

export function getOAuthRedirectURL(event: H3Event): string {
const requestURL = getRequestURL(event)

Expand Down Expand Up @@ -187,7 +193,13 @@ export async function handlePkceVerifier(event: H3Event) {
// Create new verifier
if (!query.code) {
const verifier = encodeBase64Url(getRandomBytes())
setCookie(event, 'nuxt-auth-pkce', verifier)
setCookie(event, 'nuxt-auth-pkce', verifier, {
httpOnly: true,
secure: !isDevelopment,
sameSite: 'lax',
maxAge: OAUTH_COOKIE_MAX_AGE,
path: '/',
})

// Get pkce
const encodedPkce = new TextEncoder().encode(verifier)
Expand Down Expand Up @@ -218,6 +230,12 @@ export async function handleState(event: H3Event) {

// If the state is not in the query, generate a new state and set it in the cookie
const state = encodeBase64Url(getRandomBytes(8))
setCookie(event, 'nuxt-auth-state', state)
setCookie(event, 'nuxt-auth-state', state, {
httpOnly: true,
secure: !isDevelopment,
sameSite: 'lax',
maxAge: OAUTH_COOKIE_MAX_AGE,
path: '/',
})
return state
}
Loading