Skip to content

Commit

Permalink
token auth
Browse files Browse the repository at this point in the history
  • Loading branch information
mmahalwy committed Jan 10, 2017
1 parent fb64435 commit cb0a371
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 27 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/v1/bookmarks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Api::V1::BookmarksController < ApplicationController
before_action :authenticate_user!
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]

# GET /bookmarks
Expand Down
27 changes: 0 additions & 27 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
class ApplicationController < ActionController::Base
include DeviseTokenAuth::Concerns::SetUserByToken

before_action :authenticate
before_action :set_default_response_format

helper_method :current_user

def current_user # rubocop:disable Style/TrivialAccessors
@current_user
end

protected

def authenticate
decoded_token = JWT.decode(authentication_token, Rails.application.secrets.secret_key_base).first
@current_user ||= User.find(decoded_token['id'])

rescue JWT::DecodeError
render json: { error: 'unauthorized' }, status: :unauthorized
end

def authentication_token
%r{Bearer (.*)}.match(request.headers['Authorization']).try(:captures).try(:first)
end

def set_default_response_format
request.format = :json
end
end
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker

OmniAuth.config.full_host = "http://localhost:3030"
end

0 comments on commit cb0a371

Please sign in to comment.