Skip to content

"state mismatch" error if nuxt-auth-state already exists #461

@tomlewis0

Description

@tomlewis0

Overview
The handleState logic falls over in the case that the nuxt-auth-state cookie is already present. This results in a "state mismatch" error and is unrecoverable without instructing users to erase cookies.

Detail

handleState attempts to fetch the cookie. If its successful, it erases and returns it. If it fails, it generates and stores it.

export async function handleState(event: H3Event) {
let state = getCookie(event, 'nuxt-auth-state')
if (state) {
deleteCookie(event, 'nuxt-auth-state')
return state
}
state = encodeBase64Url(getRandomBytes(8))
setCookie(event, 'nuxt-auth-state', state)
return state
}

The issue is that we call this method twice during oauth. Once before the external redirect and again when the user returns and the code and state query params are available. Example.

The happy path is where nuxt-auth-state is undefined, it is generated, stored and sent along with the redirect to the oauth provider. On return, its returned and erased.

The failure path is where nuxt-auth-state is defined as an arbitrary value, it is returned and erased. On return, it is undefined and a new value is generated resulting in a mismatch.

Proposal
WIP - feedback welcomed

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions