Skip to content

Instantly share code, notes, and snippets.

@scho
Created August 2, 2012 07:00

Revisions

  1. scho revised this gist Aug 2, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions sessions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    class SessionsController < Devise::SessionsController

    # Of course, skip authorization for this class, since we DO authorization here
    # (it's cancan stuff, so if you don't use cancan, remove this line)
    skip_authorize_resource

    # The action for login
  2. scho revised this gist Aug 2, 2012. No changes.
  3. scho created this gist Aug 2, 2012.
    90 changes: 90 additions & 0 deletions login.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,90 @@
    # The Login component
    # Lets user type in their credentials, so they can authenticate themselves
    class Sessions::Login < Netzke::Base

    # Set the EXT JS class
    js_base_class 'Ext.Window'

    # Configure the component
    #
    # @return [Hash]
    def configuration
    super.merge(
    :title => "Login",
    :layout => 'fit',
    :hidden => false,
    :width => 350,
    :y => 100,
    :auto_height => true,
    :closable => false,
    :resizable => false,
    :items => [{
    :xtype => :form,
    :frame => true,
    :buttons => [:create_user_session.action],
    :url => Netzke::Core.controller.new_user_session_path(:format => :json),
    :default_type => :textfield,
    :defaults => {
    :anchor => '100%',
    :allowBlank => false,
    :enable_key_events => true,
    :listeners => {
    :special_key => <<-JS.l
    function(field, e)
    {
    // if user presses ENTER => call onSignIn()
    if(e.getKey() == Ext.EventObject.ENTER)
    {
    field.up('window').onCreateUserSession();
    }
    }
    JS
    }
    },
    :items => [{
    :name => 'user[login]',
    :field_label => User.human_attribute_name(:login)
    },{
    :name => 'user[password]',
    :field_label => User.human_attribute_name(:password),
    :input_type => :password
    }]
    }]
    )
    end

    # Sign in button
    action :create_user_session, :form_bind => true, :icon => '/images/icons/actions/create_user_session.png'

    # On sign in
    # Success: Redirect to the path that the server returns
    # Failuer: Show alert
    js_method :on_create_user_session, <<-JS
    function() {
    var form = this.query('form')[0].getForm();
    if (form.isValid()) {
    form.submit({success: function(form, action) {
    window.location = action.result.redirect_to;
    },
    failure: function(form, action) {
    Ext.Msg.alert(action.result.errors.message, action.result.errors.reason);
    }});
    }
    }
    JS

    # Submit form on enter and set focus on login
    js_method :init_component, <<-JS
    function() {
    // Call parent
    this.callParent();
    // Get the login textfield
    var textfield = this.query('textfield')[0];
    // set focus on login textfield
    textfield.on('afterrender', function(){this.focus();})
    }
    JS

    end
    44 changes: 44 additions & 0 deletions sessions_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # This conroller handles signing in and signing out
    # See the devise gem for more information
    class SessionsController < Devise::SessionsController

    # Of course, skip authorization for this class, since we DO authorization here
    skip_authorize_resource

    # The action for login
    # If format is html => render login form
    # If format is json => Say that something went wrong
    def new
    respond_to do |format|
    format.html { super }
    format.json {render :json => {:success => false, :errors => {:message => I18n.t('session.sign_in.failed.message'),
    :reason => I18n.t('session.sign_in.failed.wrong_password')}}}
    end
    end

    # Try to authorize a user
    # If it fails => redirect to #new
    def create
    respond_to do |format|
    format.html { super }
    format.json {
    warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
    render :status => 200, :json => {:success => true, :redirect_to => session[:user_return_to] || after_sign_in_path_for(:user)}
    }
    end
    end

    # Define the default path after signing in
    #
    # @param [Object, Symbol] resource_or_scope
    def after_sign_in_path_for(resource_or_scope)
    home_path
    end

    # Define the default path after signing out
    #
    # @param [Object, Symbol] resource_or_scope
    def after_sign_out_path_for(resource_or_scope)
    new_user_session_path
    end
    end