module Card::Auth::Token

methods for setting current account

Constants

SECRET_KEY

Public Class Methods

decode(token) click to toggle source
# File lib/card/auth/token.rb, line 25
def decode token
  decoded = JWT.decode(token, SECRET_KEY)[0]
  HashWithIndifferentAccess.new decoded
rescue StandardError => e
  e.message
end
encode(user_id, extra_payload={}) click to toggle source
# File lib/card/auth/token.rb, line 10
def encode user_id, extra_payload={}
  payload = { user_id: user_id, exp: expiration }.merge(extra_payload)

  JWT.encode payload, SECRET_KEY
end
expiration() click to toggle source
# File lib/card/auth/token.rb, line 32
def expiration
  Card.config.token_expiry.from_now.to_i
end
validate!(token) click to toggle source

returns Hash if valid, String error message if not

# File lib/card/auth/token.rb, line 18
def validate! token
  payload = decode token
  raise Card::Error::PermissionDenied, payload if payload.is_a? String

  payload
end

Public Instance Methods

signin_with_token(token) click to toggle source

set the current user based on token

# File lib/card/auth/token.rb, line 38
def signin_with_token token
  payload = Token.validate! token
  signin payload[:anonymous] ? Card::AnonymousID : payload[:user_id]
end