Module: Kotek::Auth

Defined in:
lib/kotek/auth.rb,
lib/kotek/auth/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.3.0"

Instance Method Summary collapse

Instance Method Details

#current_userObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/kotek/auth.rb', line 16

def current_user
  begin
    token = request.headers["Authorization"]
    decoded_array = JWT.decode(token, hmac_secret, true, { algorithm: 'HS256' })
    payload = decoded_array.first
    User.find(payload["user_id"])
  rescue
    return nil
  end
end

#hmac_secretObject



12
13
14
# File 'lib/kotek/auth.rb', line 12

def hmac_secret
  ENV["API_SECRET_KEY"]
end

#token(user_id) ⇒ Object



7
8
9
10
# File 'lib/kotek/auth.rb', line 7

def token(user_id)
  payload = { user_id: user_id }
  JWT.encode(payload, hmac_secret, 'HS256')
end