Skip to content

Instantly share code, notes, and snippets.

Rails.application.routes.draw do
get :index, to: 'home#index'
end
class HomeController < ApplicationController
before_action :authenticate_user
def index
render json: current_user || 'The user isn\'t authenticated'.
end
end
class ApplicationController < ActionController::API
include Knock::Authenticable
end
...
# 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
module Auth0
class Signin
def self.perform(email, password)
ctx = OpenSSL::SSL::SSLContext.new
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
HTTP.headers(accept: 'application/json')
.post(
"https://#{ENV['AUTH0_DOMAIN']}/oauth/token",
ssl_context: ctx,
module Auth0
class Signup
def self.perform(email, password)
HTTP.headers(accept: 'application/json')
.post(
"https://#{ENV['AUTH0_DOMAIN']}/dbconnections/signup",
form: {
client_id: ENV['AUTH0_CLIENT_ID'],
email: email,
password: password,
Knock.setup do |config|
if Rails.env.test?
config.token_secret_signature_key = -> { Rails.application.credentials.read }
else
config.token_audience = -> { ENV['AUTH0_AUDIENCE'] }
# Auth0 uses RS256.
config.token_signature_algorithm = 'RS256'
# API secret from Auth0.
...
# Handle JWT with auth0
gem 'knock', '~> 2.0'
# To perform http requests.
gem 'http'
...
...
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
# Manage env variables.
gem 'dotenv-rails'
end
class User < ApplicationRecord
validates_presence_of :auth0_uid, :email
end