update authenticate with app_token

This commit is contained in:
Thein Lin Kyaw
2020-01-15 11:28:57 +06:30
parent 684b408d0b
commit f89d436aeb
2 changed files with 13 additions and 16 deletions

View File

@@ -66,5 +66,5 @@ class Api::AuthenticateController < Api::ApiController
params.permit(:emp_id, :password, :session_token)
end
end

View File

@@ -38,25 +38,22 @@ class Employee < ApplicationRecord
end
end
return nil
end
def self.authenticate_by_token(session_token)
if (session_token)
user = Employee.find_by_token_session(session_token)
expiry_time = login_expiry_time
if user && !user.session_expiry.nil?
if user && user.session_expiry.utc > DateTime.now.utc
def self.authenticate_by_token(token)
if token
if user = Employee.find_by_token_session(token)
expiry_time = login_expiry_time
if user.session_expiry && user.session_expiry.utc > DateTime.now.utc
#Extend the login time each time authenticatation take place
user.session_expiry = user.session_expiry.utc + expiry_time.minutes
user.save
return true
else
return false
end
elsif user = Employee.find_by_app_token(token)
return true
end
end
return false
end
@@ -92,17 +89,17 @@ class Employee < ApplicationRecord
return expiry_time
end
def generate_app_id
def generate_app_id
# return if self.role != 'application'
self.app_id = SecureRandom.urlsafe_base64(nil, false)
rescue ActiveRecord::RecordNotUnique
retry
rescue ActiveRecord::RecordNotUnique
retry
end
def generate_app_token
# return if self.role != 'application'
self.app_token = SecureRandom.hex(10)
rescue ActiveRecord::RecordNotUnique
retry
rescue ActiveRecord::RecordNotUnique
retry
end
end