How can I get a user from an api key ?

Hey all, I recently stumbled onto better-auth and really like it, especially once I saw it had a plugin for api keys 😍 I managed to get it set up and can create api keys from nuxt (on client side), but I cannot seem to get a user out of them ? I can see the userId is set on the key (since it's created from an authenticated user), but if I try to get a user out of the session by doing this:
import { auth } from "~/utils/auth"

export default defineEventHandler(async(event) => {
const user = await auth.api.getSession({
headers: event.headers
})

console.log(user?.user)
})
import { auth } from "~/utils/auth"

export default defineEventHandler(async(event) => {
const user = await auth.api.getSession({
headers: event.headers
})

console.log(user?.user)
})
But it turns out as undefined: I send the request with a header of x-api-key : <value> where <value> is the value of the generate apiKey.key. Did I misread the docs ? Have I missed something ? How are you all doing this ?
Solution:
Ok, it just seems like I cannot copy/paste correctly into a curl command. Nothing to see here xD
Jump to solution
1 Reply
Solution
Matteo G
Matteo G2w ago
Ok, it just seems like I cannot copy/paste correctly into a curl command. Nothing to see here xD

Did you find this page helpful?