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

25 lines
722 B
Ruby
Raw Normal View History

2014-11-27 19:43:04 +08:00
module Mobile
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-03 17:28:19 +08:00
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
{status: 0, data: data}
2014-11-27 19:43:04 +08:00
else
2014-12-03 17:28:19 +08:00
{status: 1, message: 'Unauthorized.'}
2014-11-27 19:43:04 +08:00
end
end
end
end
end