adonisjs / transmit

A Server-Sent-Event module for AdonisJS
MIT License
71 stars 5 forks source link

No acces to authenticated user when trying to authorize a channel #11

Closed kilobyte2007 closed 7 months ago

kilobyte2007 commented 7 months ago

Package version

0.4.2

Describe the bug

I am trying to make a channel private like in the README example but it looks like the auth.user is always undefined within the callback function.

This is my code:

import type { HttpContext } from '@adonisjs/core/http'
import transmit from '@adonisjs/transmit/services/main'

transmit.authorizeChannel<{ id: string }>('users/:id', async (ctx: HttpContext, { id }) => {
  console.log('id', id)
  console.log('user', ctx.auth.user)
  return ctx.auth.user?.id === +id
})

And this is the log output:

id 29
user undefined

It's probably worth noting that I am logged in and other requests are working fine.

Seems to me like there's a configuration that I've missed but I am pretty sure I have configured everything like this in the docs. Is there something I am missing?

Thanks a lot in advance.

Here are my package versions:

"@adonisjs/auth": "^9.1.1",
"@adonisjs/core": "^6.3.1",
"@adonisjs/lucid": "^20.2.0",
"@adonisjs/mail": "^9.2.0",
"@adonisjs/redis": "^8.0.1",
"@adonisjs/session": "^7.1.1",
"@adonisjs/shield": "^8.1.0",
"@adonisjs/static": "^1.1.1",
"@adonisjs/transmit": "^0.4.2",
"@adonisjs/transmit-client": "^0.1.6"

Reproduction repo

No response

RomainLanz commented 7 months ago

Hey @kilobyte2007! 👋🏻

If you are not using the silent auth middleware, you will have first to authenticate the user:

transmit.authorizeChannel<{ id: string }>('users/:id', async (ctx: HttpContext, { id }) => {
  await auth.check()

  console.log('id', id)
  console.log('user', ctx.auth.user)
  return ctx.auth.user?.id === +id
})
kilobyte2007 commented 7 months ago

Wow! Didn't know about the silent auth middleware. Works perfectly now. Thanks so much!

RomainLanz commented 7 months ago

Nice!