Get the current user in ActionCable

panoscodes profile image Panos Dalitsouris ・1 min read

An easy way to get the current user using devise-jwt

When you “login”, devise-jwt returns an Authorization header with a token

Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6Miwic3ViIjoiMiIsInNjcCI6InVzZXIiLCJhdWQiOm51bGwsImlhdCI6MTU3MDM4OTcyNiwiZXhwIjoxNTcwMzkzMzI2LCJqdGkiOiI1NGZmY2YyOC1iNGFhLTQ3NDMtYTU5My1iOWJkYmIzNDRiOTIifQ.hcBKHd9n4r4s9-pHyshDyVm9zHnjF33SXXTQyGZljEY

You probably already know that.

According to the docs if you are using cookies you can do the following to get the current user

verified_user = User.find_by(id: cookies.encrypted[:user_id])

In case you use devise-jwt you could use following inside your connection.rb to find_verified_user 🎊


markdown guide