login progress

This commit is contained in:
Edin Dazdarevic
2015-03-02 07:49:36 +01:00
parent 7c28876fed
commit 4befef5bf4
8 changed files with 233 additions and 63 deletions

View File

@@ -1,7 +1,27 @@
get '/user/auth' do
# TODO: do something that makes sense here
res = User.find_by(id: 1).try(:authenticate, 'spassword') # => false
res.to_json
post '/user/login' do
request.body.rewind
login_details = JSON.parse(request.body.read)
email = login_details['email']
password = login_details['password']
res = User.find_by(email: email).try(:authenticate, password) # => false
if res
#TODO : encrypt this cookie
response.set_cookie('ribica_auth', :path=> '/', :httponly => true, :value=>res.id, :expires=>Time.now+100.year)
res.to_json(except: 'password_digest')
else
status 401
{:error => "email ili lozinka neispravni!"}.to_json
end
end
get '/user' do
auth = cookies['ribica_auth']
if not auth.nil?
return User.find_by(id: auth).to_json(except: 'password_digest')
end
end
post '/user' do