This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| # Find our User by id in test environment, and auth0_uid in other environments. | |
| Knock::AuthToken.class_eval do | |
| def entity_for(entity_class) | |
| key_to_find = Rails.env.test? ? :id : :auth0_uid | |
| entity_class.find_by(key_to_find => @payload['sub']) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::API | |
| include Knock::Authenticable | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HomeController < ApplicationController | |
| before_action :authenticate_user | |
| def index | |
| render json: current_user || 'The user isn\'t authenticated'. | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.routes.draw do | |
| get :index, to: 'home#index' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| Knock::Authenticable.module_eval do | |
| def define_current_entity_getter(entity_class, getter_name) | |
| unless self.respond_to?(getter_name) | |
| memoization_var_name = "@_#{getter_name}" | |
| self.class.send(:define_method, getter_name) do | |
| unless instance_variable_defined?(memoization_var_name) | |
| current = Knock::AuthToken.new(token: token).entity_for(entity_class) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.config.middleware.use OmniAuth::Builder do | |
| provider( | |
| :auth0, | |
| ENV['AUTH0_CLIENT_ID'], | |
| ENV['AUTH0_CLIENT_SECRET'], | |
| ENV['AUTH0_DOMAIN'], | |
| callback_path: '/auth/auth0/callback', | |
| authorize_params: { | |
| scope: 'openid email' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.routes.draw do | |
| namespace :admin do | |
| resources :users do | |
| post :login_as_user, on: :member | |
| end | |
| root to: 'users#index' | |
| end | |
| get 'auth/auth0', to: 'auth0#authentication', as: :authentication |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateUsers < ActiveRecord::Migration[5.2] | |
| def change | |
| create_table :users do |t| | |
| t.string :email, null: false, index: true, unique: true | |
| t.boolean :admin, index: true, default: false | |
| t.timestamps | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Auth0Controller < ApplicationController | |
| ... | |
| def callback | |
| user = find_or_create_user | |
| session[:user_id] = user.id | |
| if user.admin? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Auth0Controller < ApplicationController | |
| ... | |
| def failure | |
| end | |
| def logout | |
| if admin_logged_as_another_user? | |
| accessed_user = session[:user_id] |