Skip to content

Commit 678827e

Browse files
Merge pull request #2 from matthewsullivan/feature/add-jwt-expiration
Feature/add JWT expiration
2 parents 9e9f4ca + 59b4996 commit 678827e

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

app/graph/authentication/mutations/login.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def login_user!(credentials)
2323
user = User.find_by(email: credentials[:email])
2424
raise StandardError unless user&.authenticate(credentials[:password])
2525

26-
token = JwtHelper.encode_token({ user_id: user.id })
26+
token = JwtHelper.encode_token({ email: user.email, user_id: user.id })
2727
{ user: user, token: token }
2828
end
2929
end

app/helpers/jwt_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
module JwtHelper
44
class << self
5-
SECRET = ENV['RJPA_SECRET']
5+
SECRET = ENV['JWT_SECRET']
66

77
def encode_token(payload)
8+
payload[:exp] = Time.now.to_i + ENV['JWT_EXPIRATION'].to_i
89
JWT.encode(payload, SECRET)
910
end
1011

@@ -22,7 +23,7 @@ def logged_in_user(token)
2223

2324
def decoded_token(token)
2425
JWT.decode(token, SECRET, true, algorithm: ENV['JWT_ALGORITHM'])
25-
rescue StandardError => e
26+
rescue JWT::ExpiredSignature, StandardError => e
2627
GraphQL::ExecutionError.new(e.message)
2728
nil
2829
end

config/local_env.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
RJPA_SECRET: 'RJPA_SECRET'
2-
JWT_ALGORITHM: 'HS256'
1+
JWT_ALGORITHM: 'HS256'
2+
JWT_EXPIRATION: '3600'
3+
JWT_SECRET: 'RJPA_SECRET'

0 commit comments

Comments
 (0)