-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add devise auth endpoints to authenticate and revoke JWTs, also add e…
…nvironment variable gem (#28) * controllers and jwt setup for devise * finish up devise setup * lint * add dotenv gem and seed user data * linting fixes * remove require auth from questions * change revocation strategy to self * add .env setup to readme * add comments to session controller * linting sessions controller comments * add to readme how to protect a resource
- Loading branch information
Showing
14 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CORS_ORIGIN= | ||
SEED_USER_EMAIL= | ||
SEED_USER_PASSWORD= | ||
DEVISE_JWT_SECRET_KEY= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ | |
|
||
# Ignore master key for decrypting credentials and more. | ||
/config/master.key | ||
.env.development.local |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,21 @@ SELECT * FROM questions; | |
\d+ questions | ||
``` | ||
|
||
**Environment Variable Setup** | ||
``` | ||
# remove .template from .env.development.local.template and fill in .env.development.local, example values: | ||
CORS_ORIGIN=http://localhost:3000 | ||
[email protected] | ||
SEED_USER_PASSWORD=password | ||
DEVISE_JWT_SECRET_KEY=super_secret_secret_key | ||
``` | ||
|
||
**Authenticating Routes with Devise** | ||
``` | ||
# This project includes Devise for auth, add this line before the resource to require a user to be logged in: | ||
before_action :authenticate_user! | ||
``` | ||
|
||
**Run the dev server** | ||
|
||
``` | ||
|
@@ -95,4 +110,4 @@ If you get an error during dependency installation containing `can't find gem bu | |
gem update --system | ||
bundle install | ||
``` | ||
This worked with Ruby 2.5.1 with `rbenv` on MacOS. | ||
This worked with Ruby 2.5.1 with `rbenv` on MacOS. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::API | ||
respond_to :json | ||
def render_resource(resource) | ||
if resource.errors.empty? | ||
render json: resource | ||
else | ||
validation_error(resource) | ||
end | ||
end | ||
|
||
def validation_error(resource) | ||
render json: { | ||
errors: [ | ||
{ | ||
status: '400', | ||
title: 'Bad Request', | ||
detail: resource.errors, | ||
code: '100' | ||
} | ||
] | ||
}, status: :bad_request | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class SessionsController < Devise::SessionsController | ||
respond_to :json | ||
|
||
private | ||
|
||
# this returns a response with the session info in json format | ||
# including the jti, user, and timestamps | ||
def respond_with(resource, _opts = {}) | ||
render json: resource | ||
end | ||
|
||
# upon logout, this is the handler that will handle what to send | ||
# as response, currently just sets header to 200 | ||
def respond_to_on_destroy | ||
head :ok | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class User < ApplicationRecord | ||
devise :database_authenticatable, :registerable, :validatable | ||
include Devise::JWT::RevocationStrategies::JTIMatcher | ||
devise :database_authenticatable, :validatable, :rememberable, | ||
:jwt_authenticatable, jwt_revocation_strategy: self | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class JtiMatcherRevocation < ActiveRecord::Migration[6.0] | ||
def change | ||
add_column :users, :jti, :string, null: false | ||
add_index :users, :jti, unique: true | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
case Rails.env | ||
when 'development' | ||
User.create(email: ENV['SEED_USER_EMAIL'], password: ENV['SEED_USER_PASSWORD'], jti: SecureRandom.uuid) | ||
end |