2014-06-21 66 views
-1

我正在爲Rails 4中的Web應用程序的API執行登錄函數。 想法是能夠從發送JSON的Android應用程序登錄一個用戶名和密碼摘要。從JSON API中獲取參數POST請求在Rails 4中應用

這是我api_controller:

class ApiController < ApplicationController 
#respond_to :json 
before_filter :restrict_access, except: :login 
protect_from_forgery except: [:login, :new_ticket] 

##========================================================================== 
# /api/login/:username/:password_digest 
##========================================================================== 
def login 
    @user = User.find_by_username(login_params[:username]) 

    if not @user 
     user_not_found = 'No existe este usuario' 
     render json: {error: true, message: user_not_found}, status: 404 
     return 
    elsif not @user.authenticate(login_params[:password_digest]) 
     wrong_password = 'Contraseña incorrecta' 
     render json: {error: true, message: wrong_password}, status: 400 
    else 
     access_token = @user.api_key.access_token 
     render json: {access_token: access_token, authenticated: true} 
    end 
end 

##========================================================================== 
# /api/users/:access_token/ 
##========================================================================== 

def users 
    @users = User.all 
    render json: (@users.map { |u| u.as_json(except: :password) }).to_json 
end 

private 
def restrict_access 
    api_key = ApiKey.find_by_access_token(params[:access_token]) 
    head :unauthorized unless api_key 
end 

def login_params 
    params.permit(:username, :password_digest) 
end 


end 

我正在用的用戶名POST請求和password_digest,但我發現{}中login_params。爲什麼?謝謝!

編輯:這是我的路由的定義

scope 'api' do 
    post '/login', to: 'api#login' 
    get '/users/:access_token', to: 'api#users' 
end 

回答

0

沒關係,參數使用curl,但不與「高級客戶休息」鉻插件時正在

相關問題