Skip to content

Commit

Permalink
'working' WIP on API user auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dracco1993 committed Jan 7, 2020
1 parent f972f79 commit 5e21a4b
Show file tree
Hide file tree
Showing 32 changed files with 84 additions and 370 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -18,6 +18,7 @@ gem 'jbuilder', '~> 2.7'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'devise'
# gem 'jwt'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
Expand Down
38 changes: 38 additions & 0 deletions app/controllers/application_controller.rb
@@ -1,2 +1,40 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
skip_before_action :verify_authenticity_token

respond_to :json

before_action :authenticate_user

private

def authenticate_user!(options = {})
head :unauthorized unless signed_in?
end

def current_user
@current_user ||= super || User.find(@current_user_id)
end

def signed_in?
@current_user_id.present?
end

def authenticate_user
if request.headers['Authorization'].present?
authenticate_or_request_with_http_token do |token|
begin
jwt_payload = JWT.decode(token, Rails.application.secrets.secret_key_base).first

@current_user_id = jwt_payload['id']
rescue JWT::ExpiredSignature, JWT::VerificationError, JWT::DecodeError
head :unauthorized
end
end
end
end
end
7 changes: 0 additions & 7 deletions app/controllers/dashboards_controller.rb

This file was deleted.

13 changes: 13 additions & 0 deletions app/controllers/sessions_controller.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class SessionsController < Devise::SessionsController
def create
user = User.find_by_email(sign_in_params[:email])

if user && user.valid_password?(sign_in_params[:password])
@current_user = user
else
render json: { errors: { 'email or password' => ['is invalid'] } }, status: :unprocessable_entity
end
end
end
12 changes: 2 additions & 10 deletions app/controllers/users_controller.rb
@@ -1,5 +1,5 @@
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!

# GET /users
Expand Down Expand Up @@ -29,10 +29,8 @@ def create

respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
Expand All @@ -43,10 +41,8 @@ def create
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
Expand All @@ -57,7 +53,6 @@ def update
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
Expand All @@ -71,11 +66,8 @@ def set_user
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(
:username,
:email,
:password,
:salt,
:encrypted_password
:password
)
end
end
14 changes: 14 additions & 0 deletions app/models/user.rb
Expand Up @@ -3,4 +3,18 @@
class User < ApplicationRecord
devise :database_authenticatable, :lockable,
:recoverable, :rememberable, :validatable, :trackable

validates :email,
presence: true,
uniqueness: true,
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }

def generate_jwt
JWT.encode({
id: id,
exp: 7.days.from_now.to_i
},
Rails.application.secrets.secret_key_base
)
end
end
13 changes: 0 additions & 13 deletions app/views/dashboards/show.html.erb

This file was deleted.

3 changes: 3 additions & 0 deletions app/views/devise/registrations/create.json.jbuilder
@@ -0,0 +1,3 @@
json.user do |json|
json.partial! 'users/user', user: current_user
end
3 changes: 3 additions & 0 deletions app/views/devise/sessions/create.json.jbuilder
@@ -0,0 +1,3 @@
json.user do |json|
json.partial! 'users/user', user: current_user
end
29 changes: 0 additions & 29 deletions app/views/users/_form.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/users/_user.json.jbuilder
@@ -1,2 +1,3 @@
json.extract! user, :id, :created_at, :updated_at
json.url user_url(user, format: :json)
json.token user.generate_jwt
16 changes: 0 additions & 16 deletions app/views/users/confirmations/new.html.erb

This file was deleted.

6 changes: 0 additions & 6 deletions app/views/users/edit.html.erb

This file was deleted.

31 changes: 0 additions & 31 deletions app/views/users/index.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/users/mailer/confirmation_instructions.html.erb

This file was deleted.

7 changes: 0 additions & 7 deletions app/views/users/mailer/email_changed.html.erb

This file was deleted.

3 changes: 0 additions & 3 deletions app/views/users/mailer/password_change.html.erb

This file was deleted.

8 changes: 0 additions & 8 deletions app/views/users/mailer/reset_password_instructions.html.erb

This file was deleted.

7 changes: 0 additions & 7 deletions app/views/users/mailer/unlock_instructions.html.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/users/new.html.erb

This file was deleted.

25 changes: 0 additions & 25 deletions app/views/users/passwords/edit.html.erb

This file was deleted.

16 changes: 0 additions & 16 deletions app/views/users/passwords/new.html.erb

This file was deleted.

43 changes: 0 additions & 43 deletions app/views/users/registrations/edit.html.erb

This file was deleted.

0 comments on commit 5e21a4b

Please sign in to comment.