Skip to content

Instantly share code, notes, and snippets.

@scho
Created August 2, 2012 07:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scho/3234643 to your computer and use it in GitHub Desktop.
Save scho/3234643 to your computer and use it in GitHub Desktop.
Authentication with Rails 3.2.7 using devise 2.0.4, netzke-core 0.7.6 and Ext JS 4.0.2
# 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
# 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
# (it's cancan stuff, so if you don't use cancan, remove this line)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment