Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f972f79
commit 5e21a4b
Showing
32 changed files
with
84 additions
and
370 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
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,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 |
This file was deleted.
Oops, something went wrong.
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,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 |
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 was deleted.
Oops, something went wrong.
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,3 @@ | ||
json.user do |json| | ||
json.partial! 'users/user', user: current_user | ||
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,3 @@ | ||
json.user do |json| | ||
json.partial! 'users/user', user: current_user | ||
end |
This file was deleted.
Oops, something went wrong.
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,2 +1,3 @@ | ||
json.extract! user, :id, :created_at, :updated_at | ||
json.url user_url(user, format: :json) | ||
json.token user.generate_jwt |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.