Created
January 25, 2016 04:28
-
-
Save bayarja/2140e339726bac2a68c5 to your computer and use it in GitHub Desktop.
activeNode relations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User #< ActiveRecord::Base | |
include Neo4j::ActiveNode | |
property :username | |
property :fullname | |
property :register_no | |
property :active | |
property :comment | |
property :password | |
## | |
# Relationsihps | |
has_one :out, :api_token, type: :api_token, model_class: :ApiToken | |
end | |
class ApiToken #< ActiveRecord::Base | |
include Neo4j::ActiveNode | |
property :user_uuid | |
property :access_token | |
property :is_logged | |
property :expires_at | |
## | |
# Relationships | |
has_one :in, :user, type: :user, model_class: :User | |
before_create :generate_access_token | |
before_create :set_expiration | |
def expired? | |
DateTime.now >= self.expires_at | |
end | |
def generate_access_token | |
begin | |
self.access_token = SecureRandom.hex | |
end while self.class.exists?(access_token: access_token) | |
end | |
def set_expiration | |
self.expires_at = DateTime.now + 1.day | |
end | |
end | |
def authenticate! | |
binding.pry | |
api_token = ApiToken.find_by(access_token: env["HTTP_AUTH_TOKEN"]) | |
if api_token && !api_token.expired? | |
api_token.set_expiration | |
api_token.save! | |
@current_user = api_token.user | |
success!(@current_user) | |
else | |
fail! | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment