You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trustieforge/app/api/mobile/apis/auth.rb

36 lines
943 B

module Mobile
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)
api_user = UserService.new.show_user(user.id)
present :data, {token: key, user: api_user}, using: Mobile:Entities::Auth
present :status, 0
else
{status: 1, message: 'Unauthorized.'}
end
end
end
end
end
end