Skip to content

Instantly share code, notes, and snippets.

@alameenkhader
Created October 9, 2015 10:41
Show Gist options
  • Save alameenkhader/b5497667ae0c7d12fd2f to your computer and use it in GitHub Desktop.
Save alameenkhader/b5497667ae0c7d12fd2f to your computer and use it in GitHub Desktop.
Devise Ajax Sign in with Angular JS

app/controllers/sessions_controller.rb

#
# SessionsController - Controller for user session
#
# @author alameen
#
class SessionsController < Devise::SessionsController
  def create
    resource = warden.authenticate!(scope: resource_name,
                                    recall: "#{controller_path}#failure")

    sign_in(resource_name, resource)
    @message = I18n.t('devise.sessions.signed_in')
    respond_to do |format|
      format.html
      format.json
    end
  end

  def failure
    @message = I18n.t('devise.failure.invalid', authentication_keys: 'email')
    @success = false
    respond_to do |format|
      format.html { redirect_to new_user_session_path(resource) }
      format.json
    end
  end
end

config/initializers/devise.rb

config.http_authenticatable_on_xhr = false
onfig.navigational_formats = ['*/*', :html, :json]

config/routes.rb

devise_for :users, controllers: { sessions: 'sessions' }

app/views/sessions/create.json.jbuilder

json.success true
json.message @message
json.redirect_path <redirect-path-here>

app/views/sessions/failure.json.jbuilder

json.success @success
json.message @message

Sign in view

<div class="sign-in padding15" ng-controller="SessionsCtrl">
  <div class="bottom20">
    <span class="text-primary"><b>DASHBOARD ACCESS</b></span>
  </div>
  <p class="text-danger">{{message}}</p>
  <form name="signInForm" ng-submit="signIn()">
    <div class="form-group">
      <input type="email" class="form-control" placeholder="E-MAIL" ng-model="user.email" required id="userEmail"/>
    </div>
    <div class="form-group bottom5">
      <input type="password" class="form-control" placeholder="password" ng-model="user.password" required id="userPassword"/>
    </div>
    <div class="actions bottom50">
      <%= link_to 'Forgot your password?', new_user_password_path, class: 'text-muted pull-right right0' %>
    </div>
    <div class="actions">
      <input type="submit" class="btn btn-lg btn-primary btn-center" value="ENTER" ng-disabled="signingIn">
    </div>
  </form>
  <div class="row top10">
    <img ng-if="signingIn" src="<%= asset_path('loading.gif') %>" alt="LOADING" class="loading-icon tiny" />
  </div>
</div>

app/assets/javascripts/controllers/SessionsCtrl.js

/**
 * Sessions Controller
 *
 * @author alameen
 */
'use strict';

angular.module('CFW').controller('SessionsCtrl', function($scope, $http, $filter) {
  $scope.user = {};

  $scope.signIn = function() {
    var data = { user: $scope.user };
    $scope.signingIn = true;
    $http.post('/users/sign_in.json', data)
      .success(function(data){
        if(data.success) {
          window.location = data.redirect_path;
        }
        $scope.message = data.message;
        $scope.signingIn = false;
      });
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment