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
Showing
13 changed files
with
478 additions
and
1 deletion.
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
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,12 @@ | ||
class Api::V1::AuthenticationController < ApiController | ||
skip_before_action :authenticate_token! | ||
|
||
def create | ||
user = User.find_by(email: params[:user][:email]) | ||
if user.valid_password? params[:user][:password] | ||
render json: { token: JsonWebToken.encode(sub: user.id) } | ||
else | ||
render json: { errors: ["Invalid email or password"] } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
class ApiController < ApplicationController | ||
skip_before_action :verify_authenticity_token | ||
|
||
before_action :set_default_format | ||
before_action :authenticate_token! | ||
|
||
private | ||
|
||
def set_default_format | ||
request.format = :json | ||
end | ||
|
||
def authenticate_token! | ||
payload = JsonWebToken.decode(auth_token) | ||
@current_user = User.find(payload["sub"]) | ||
rescue JWT::ExpiredSignature | ||
render json: {errors: ["Auth token has expired"]}, status: :unauthorized | ||
rescue JWT::DecodeError | ||
render json: {errors: ["Invalid auth token"]}, status: :unauthorized | ||
end | ||
|
||
def auth_token | ||
@auth_token ||= request.headers.fetch("Authorization", "").split(" ").last | ||
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,10 @@ | ||
class JsonWebToken | ||
def self.encode(payload) | ||
expiration = 30.minutes.from_now.to_i | ||
JWT.encode payload.merge(exp: expiration), Rails.application.secrets.secret_key_base | ||
end | ||
|
||
def self.decode(token) | ||
JWT.decode(token, Rails.application.secrets.secret_key_base).first | ||
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,6 @@ | ||
class User < ApplicationRecord | ||
# Include default devise modules. Others available are: | ||
# :confirmable, :lockable, :timeoutable and :omniauthable | ||
devise :database_authenticatable, :registerable, | ||
:recoverable, :rememberable, :trackable, :validatable | ||
end |
Oops, something went wrong.