el-noppo / devise-ajax

Rails 4 app using devise with validation and submit through AJAX
6 stars 7 forks source link

Login with ajax #1

Open DennisKo opened 9 years ago

DennisKo commented 9 years ago

I am struggling to implement the sessions_controller with this method. How you would you do this?

el-noppo commented 9 years ago

Hi Dennis, this is how I have implemented it:

coding: utf-8

class SessionsController < Devise::SessionsController def create resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") sign_in_and_redirect(resource_name, resource) end

def sign_in_and_redirect(resource_or_scope, resource=nil) scope = Devise::Mapping.find_scope!(resource_or_scope) resource ||= resource_or_scope sign_in(scope, resource) unless warden.user(scope) == resource flash[:notice] = "Loggade in." render :template => "shared/ujs/devise_success_login.js.erb" end

def failure render :template => "shared/ujs/devise_errors.js.erb" end end