Skip to content

Commit

Permalink
added authentication router
Browse files Browse the repository at this point in the history
  • Loading branch information
reactionic127 committed Aug 19, 2021
1 parent 073fe59 commit 0766e85
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/token_authenticatable.rb
Expand Up @@ -6,7 +6,7 @@ module TokenAuthenticatable

attr_reader :current_user

rescue_from UnauthorizedException, with: ->{ render(json: { errors "Unauthorized"}, status: 403)}
rescue_from UnauthorizedException, with: ->{ render(json: { error: "Unauthorized"}, status: 403)}
end

private
Expand Down
13 changes: 5 additions & 8 deletions config/routes.rb
@@ -1,10 +1,7 @@
Rails.application.routes.draw do
get 'users/index'
get 'users/new'
get 'users/create'
get 'users/show'
get 'users/edit'
get 'users/update'
get 'users/destroy'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
resources :users do
end

post "register" => "users#register"
post "login" => "authentication#login"
end
15 changes: 15 additions & 0 deletions lib/jwt_services.rb
@@ -0,0 +1,15 @@
class JwtService
class << self
def encode(payload, exp = 24.hours.from_now)
payload[:exp] = exp.to_i
JWT.encode(payload, Rails.application.secrets.secret_key_base)
end

def decode(token)
body = JWT.decode(token, Rails.application.secrets.secret_key_base)[0]
HashWithIndifferentAccess.new(body)
rescue JWT::DecodeError, JWT::ExpiredSignature
return nil
end
end
end

0 comments on commit 0766e85

Please sign in to comment.