From cb0a371e10165a8eb2762e75ac46067033d4a3de Mon Sep 17 00:00:00 2001 From: Mohamed El Mahallawy Date: Tue, 10 Jan 2017 11:41:29 -0800 Subject: [PATCH] token auth --- .../api/v1/bookmarks_controller.rb | 1 + app/controllers/application_controller.rb | 27 ------------------- config/environments/development.rb | 2 ++ 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/app/controllers/api/v1/bookmarks_controller.rb b/app/controllers/api/v1/bookmarks_controller.rb index d7c1849..8488f4f 100644 --- a/app/controllers/api/v1/bookmarks_controller.rb +++ b/app/controllers/api/v1/bookmarks_controller.rb @@ -1,4 +1,5 @@ class Api::V1::BookmarksController < ApplicationController + before_action :authenticate_user! before_action :set_bookmark, only: [:show, :edit, :update, :destroy] # GET /bookmarks diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 12569af..4ed4bf3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f71970..86df291 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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