Accessing current_user devise method from rails controller - ruby-on-rails

I'm using devise in a rails app I'm building. I'd like to get the ID of the user that is currently signed in, with current_user.id, in my controller, without passing it as a parameter from my view. Is there any way I can do this? Thanks

If you have installed Devise properly and have not broken something by overriding the Devise controllers, you should be able to access current_user.id from the controller. This method will only work if a user is signed in which you can test with the user_signed_in? method. Finally, this assumes that the resource name Devise is using is indeed user. That is the default, but it is possible to configure Devise to work with different resource names.

Related

How to programmatically sign in a user through Devise in Rails

I need to have a custom mechanism for signing in using Devise with Rails 4. So I found the sign_in method in Devise's test helpers section of their documentation:
sign_in #user # sign_in(resource)
But is that the proper way to sign someone in from the web? In particular, will it do all the things Devise does when a user signs in, like recording the date/time stamps, IP addresses, sign in counts, etc? Or is this just for testing purposes?
Devise offers a bunch of helpers, two of which are these:
sign_in(resource_or_scope, *args)
sign_in_and_redirect(resource_or_scope, *args)
You can use these from any controller.
If using sign_in already works for you but leaves the user on a blank page, check your logfile to see if there is a redirect going on, and where it redirects to. Or just make the redirect explicit by using the second of the helpers above.
References:
https://stackoverflow.com/a/8123646/2034097
It is the proper and standard way to programatically sign a user in. Looking at the devise login code sessions#create you can see they use this method as well.
Long story short: Yes, sign_in #user does all the things that devise would normally do when a user signs in. It could be useful, for example, allowing an Administrator to sign in as one of their users.
How To: Sign in as another user if you are an admin

Logout users with devise gem rails

In admin section, I'm showing a list of currently logged in users.
Now admin can select one or more user/users and destroy their session(logout them).
I'm not able to figure where to start from,please help me.
You can use the sign_out method in the controller action by passing in the user object:
# Make sure only admins can do this
def sign_out_user
#user = User.find(params[:id])
sign_out #user
end
More info here:
http://rubydoc.info/github/plataformatec/devise/master/Devise/TestHelpers%3asign_out
Considering users is the collection of your required users,
for user in users
sign_out user
end
It should solve your issue.
Hope it helps :)
The sign_out method provided by Devise won't help. I know the documentation says that it will logout the "resource" you requested, but if you dig into the gems themselves (devise and warden) you'll find that when you give it an object, like a user, it merely figures out what scope (ie, :user) that object belongs to, and it logs out that entire scope.
A scope in Devise is a namespace for logins. You might have a Customer model that requires logins, but also a Vendor model that also requires logins, and you'd use different scopes for those. Most applications only use a single scope, tied to the User model.
You're probably using :cookie_store for your session storage, which is the Rails default. In this case, it isn't possible to log out any single user except yourself. Devise stores your login info in the session, which is stored in a cookie, and not in your database. Their browser has the credentials, so you can't directly remove that.

How can I include devise usernames on rails logs (using graylog2)

Is there a way to include the logged in user in rails logs? I am using devise and graylog2, the latter of which I assume is inconsequential to the answer.
Devise provides a current_user helper in the controller which can be used to access the currently signed-in user.
Assuming the user model has a name attribute, you can do the following:
::Rails.logger.info "The currently logged-in user is #{current_user.name}"

Current_user method using devise

I've just started to use devise today and I want to know if I have to create a current user method in my application controller or is it already created through devise ?
Devise creates that helper method for you, and others too. Check
https://github.com/heartcombo/devise#controller-filters-and-helpers

Devise will not let me register a user while logged in

I'm using devise for the user system but I have one problem. I'd like for a logged-in user to register new user. It's a question of security. However, a logged-in user can not currently register a new user.
I dont know how fix this.
Thanks by help!
I'm pretty sure the easist way would be just to turn off the devise config option :registerable, this will get rid of the sign_up paths and links.
Then just build your own user controller actions and views to interact directly with your User model.
The default devise registrations controller wants to auto create a new session for the newly created user which is why it won't let logged in users create another user.
Hope this helps.
The Devise documentation explains how to do that kind of thing.
You don't need to over-ride anything in devise. Just treat it like any other namespaced resource.

Resources