-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Guardian.Plug.EnsureAuthenticated not working #194
Comments
I could make it work adding the realm to the plug in the router.ex
Without explicitly declaring the realm, it just didn't work at all. |
Hi @qgadrian. I usually use Bearer as the realm. If you don't declare a realm on your verify header plug it will assume
With the relam
It's up to the client to use the correct form that the server is expecting. |
I want to create a helper method "current_user" and be able to use it in all templates, much like in Devise in Rails. There's a function "guardianplugensureauthenticated" Could anyone give me a pointer how to utilize it for that? There's no base controller in Phoenix where I can define current_user by calling "guardianplugensureauthenticated" |
@hassox Using the realm name it works fine. I'll check why without declaring a real it just don't work (maybe there is a default real name?). @GildedHonour you can use at your controller methods the current user and the claims for an authenticated session. Check the readme, there you can see that you can use this
|
@qgadrian that would work. But I also want to do that on a module level. Suppose, I want to restrict access to the whole controller UserDashboard for non authenticated users. It can be done via router.ex file? Also I want to use "current_user" helper function in the main layout for which I'll need to create it somehow in a SharedView or LayoutView. How can I get access to user and claims from a View? |
@GildedHonour You can use the plug I don´t know anything about views yet, I just started learning Elixir/Phoenix 5 days ago. You can ask at Stackoverflow that. |
@qgadrian I usually set the realm to "Bearer" like so: How are you testing your controllers and sending requests? Are you sending valid JWT tokens through the "authorization" header? @GildedHonour you can create a
That way the helper functions are imported in all views. In that helper module you can define helper functions like so:
Where in a view you'd access the resources like so |
Hi, I was playing around with Guardian but can't make the plug Guardian.Plug.EnsureAuthenticated work within an api request.
Authentication seems to work fine, but maybe it's that the problem and not the plug itself. Every time I try to do a request to a controller using the plug, the error handler method unauthenticated is called.
router.ex
config.exs
authentication controller
authenticated ensured controller
I'm sending the request with the Authorization request header with the jwt token. A log line in unauthenticated method shows the following:
The test project can be found here
Thank you
The text was updated successfully, but these errors were encountered: