socialforge/app/api/mobile/apis/auth.rb

36 lines
957 B
Ruby
Raw Normal View History

2014-11-27 19:43:04 +08:00
module Mobile
2014-12-09 16:36:43 +08:00
module Entities
class Auth < Grape::Entity
expose :token
expose :user, using: User
end
end
module Apis
class Auth < Grape::API
resource :auth do
desc "Creates and returns access_token if valid login"
params do
requires :login, type: String, desc: 'Username or email'
requires :password, type: String, desc: 'Password'
end
post :login do
user,last_logon = ::User.try_to_login(params[:login], params[:password])
if user
::ApiKey.delete_all(user_id: user.id)
key = ::ApiKey.create!(user_id: user.id)
2014-12-09 16:57:08 +08:00
api_user = ::UsersService.new.show_user({id:user.id})
present :data, {token: key.access_token, user: api_user}, using: Entities::Auth
2014-12-09 16:36:43 +08:00
present :status, 0
else
{status: 1, message: 'Unauthorized.'}
end
2014-11-27 19:43:04 +08:00
end
end
end
end
end