Created
          July 11, 2013 12:34 
        
      - 
      
- 
        Save DVG/5975064 to your computer and use it in GitHub Desktop. 
    EmberAuth
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | StripfighterEmber.ApplicationController = Ember.Controller.extend | |
| signOut: -> | |
| StripfighterEmber.Auth.signOut() | |
| StripfighterEmber.Auth.destroySession() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | StripfighterEmber.RegistrationController = Ember.Controller.extend | |
| email: null | |
| password: null | |
| passwordConfirmation: null | |
| sendRegistration: () -> | |
| self = @ | |
| $.post('/users', | |
| user: | |
| email: @email, | |
| password: @password, | |
| password_confirmation: @passwordConfirmation) | |
| .done (response) -> | |
| # sign in | |
| ) | |
| .fail (response) -> | |
| # display errors | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class RegistrationsController < Devise::RegistrationsController | |
| respond_to :json | |
| def create | |
| user = User.new(params[:user]) | |
| if user.save | |
| user.ensure_authentication_token! | |
| user.remember_me! | |
| data = { | |
| user_id: user.id, | |
| auth_token: user.authentication_token, | |
| remember_token: remember_token(user) | |
| } | |
| render json: data, status: 201 | |
| return | |
| else | |
| warden.custom_failure! | |
| render :json=> {errors: user.errors}, :status=>422 | |
| end | |
| end | |
| protected | |
| def remember_token(resource) | |
| data = resource_class.serialize_into_cookie(resource) | |
| "#{data.first.first}-#{data.last}" | |
| end | |
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class SessionsController < Devise::SessionsController | |
| def create | |
| unless (params[:email] && params[:password]) || (params[:remember_token]) | |
| return missing_params | |
| end | |
| build_resource | |
| resource = if params[:remember_token] | |
| resource_from_remember_token | |
| else | |
| resource_from_credentials | |
| end | |
| return invalid_credentials unless resource | |
| resource.ensure_authentication_token! | |
| data = { | |
| user_id: resource.id, | |
| auth_token: resource.authentication_token, | |
| } | |
| if params[:remember] | |
| resource.remember_me! | |
| data[:remember_token] = remember_token(resource) | |
| end | |
| render json: data, status: 201 | |
| end | |
| def destroy | |
| return missing_params unless params[:auth_token] | |
| resource = resource_class.find_by_authentication_token(params[:auth_token]) | |
| return invalid_credentials unless resource | |
| resource.reset_authentication_token! | |
| render json: {user_id: resource.id}, status: 200 | |
| end | |
| protected | |
| def missing_params | |
| warden.custom_failure! | |
| return render json: {}, status: 400 | |
| end | |
| def invalid_credentials | |
| warden.custom_failure! | |
| render json: {}, status: 401 | |
| end | |
| def remember_token(resource) | |
| data = resource_class.serialize_into_cookie(resource) | |
| "#{data.first.first}-#{data.last}" | |
| end | |
| def resource_from_remember_token | |
| token = params[:remember_token] | |
| id, identifier = token.split('-') | |
| resource_class.serialize_from_cookie(id, identifier) | |
| end | |
| def resource_from_credentials | |
| data = { email: params[:email] } | |
| if res = resource_class.find_for_database_authentication(data) | |
| if res.valid_password?(params[:password]) | |
| res | |
| end | |
| end | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment