-
-
Save traumverloren/eb7c27e1e2780240c13f to your computer and use it in GitHub Desktop.
| ######################### | |
| # config/initializers/doorkeeper.rb | |
| ######################### | |
| Doorkeeper.configure do | |
| # Change the ORM that doorkeeper will use. | |
| # Currently supported options are :active_record, :mongoid2, :mongoid3, :mongo_mapper | |
| orm :active_record | |
| # This block will be called to check whether the resource owner is authenticated or not. | |
| resource_owner_authenticator do | |
| User.find_by_id(session[:user_id]) || redirect_to(new_session_url(return_to: request.fullpath)) | |
| end | |
| ######################### | |
| # sessions_controller.rb | |
| ######################### | |
| class SessionsController < ApplicationController | |
| def new | |
| session[:return_to] = params[:return_to] | |
| end | |
| def create | |
| user = User.find_by_email(params[:email]) | |
| if user && user.authenticate(params[:password]) | |
| session[:user_id] = user.id | |
| redirect_to session[:return_to] || root_url, notice: "Logged in!" | |
| session.delete(:return_to) | |
| else | |
| flash.now.alert = "Invalid password or email" | |
| render "new" | |
| end | |
| end | |
| end |
@jiggneshhgohel this really helped a lot! Thank you for posting this with such clarity! :-)
Now, Where I'm stuck at is how would I delete the user session when the user clicks logout! We don't have a logout URL with the doorkeeper.
The user should be logged out and when the user logs in again (I'm using identity.launchWebAuthFlow) He should be prompted to enter login details again. Right now, resource_owner_authenticator always has current_user present
resource_owner_authenticator do |routes|
if current_user
current_user
else
session[:user_return_to] = request.fullpath
redirect_to(new_user_session_url)
end
end
When I make API calls with the token, the current_user is nil (i.e. warden.authenticated?(:user) is false) so warden.logout does not work when I try to delete the session through API calls.
This SO question discusses that thing to an extent and here the user is logged out right before returning the token. I want to do this (warden.logout) on users sign_out action. Can you help me do this?
In a nutshell, I want to logout the user session which we created during resource_owner_authenticator block when the user clicks logout. (I'm using implicit grant flow for a chrome extension to our API, which is the OAuth provider with Doorkeeper). Any help would be highly appreciated. Thanks!
With the version 4.2.0 of Devise I was using there following a convention set by Devise for the session variable the need to do any modifications in the
SessionsControllercan be avoided. Following that convention I updated my config to following and it worked without any issues:Hope that helps.
Thanks.