This file contains hidden or 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
<code data-gist-id="ec8cc541c45705a34988c68225627ecb" data-gist-hide-line-numbers="true"></code> | |
class UsersController < ApplicationController | |
def show | |
@user = User.find(params[:id]) | |
end | |
def new | |
@user = User.new | |
end |
This file contains hidden or 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
# lib/json_web_token.rb | |
class JsonWebToken | |
HMAC_SECRET = Rails.application.secrets.secret_key_base | |
def self.encode(payload, exp = 24.hours.from_now) | |
payload[:exp] = exp.to_i | |
JWT.encode(payload, HMAC_SECRET) | |
end |
This file contains hidden or 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
# config/application.rb | |
module ApiApp | |
class Application < Rails::Application | |
#..... | |
config.eager_load_paths << Rails.root.join('lib') | |
#..... | |
end | |
end |
This file contains hidden or 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
# define a command class | |
class AuthenticateUser | |
# put SimpleCommand before the class' ancestors chain | |
prepend SimpleCommand | |
include ActiveModel::Validations | |
# optional, initialize the command with some arguments | |
def initialize(email, password) | |
@email = email | |
@password = password |
This file contains hidden or 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
# config/locales/en.yml | |
en: | |
activemodel: | |
errors: | |
models: | |
authenticate_user: | |
failure: Wrong email or password |
This file contains hidden or 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
# app/services/authenticate_user.rb | |
class AuthenticateUser | |
def initialize(email, password) | |
@email = email | |
@password = password | |
end | |
def call | |
JsonWebToken.encode(user_id: user.id) if user |
This file contains hidden or 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
# app/controllers/authentication_controller.rb | |
class AuthenticationController < ApplicationController | |
skip_before_action :authenticate_request | |
def create | |
command = AuthenticateUser.call(params[:email], params[:password]) | |
if command.success? | |
render json: { auth_token: command.result } |
This file contains hidden or 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
#config/routes.rb | |
namespace :api do | |
namespace :v1 do | |
post 'signin', to: 'authentication#create' | |
end | |
end |
This file contains hidden or 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
require 'jwt' | |
payload = { data: 'test' } | |
hmac_secret = 'my$ecretK3y' | |
token = JWT.encode(payload, hmac_secret, 'HS256') | |
#eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidGVzdCJ9.pNIWIL34Jo13LViZAJACzK6Yf0qnvT_BuwOxiMCPE-Y | |
puts token |
OlderNewer