Skip to content
View Lynquatiq's full-sized avatar

Organizations

@https-support-atlassian

Block or report Lynquatiq

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Please don't include any personal information such as legal names or email addresses. Maximum 100 characters, markdown supported. This note will be visible to only you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
Lynquatiq/README.md
Rails.application.routes.draw do
  post 'authenticate', to: 'authentication#authenticate'
end
class AuthenticationController < ApplicationController
  # POST /authenticate
  def authenticate
    user = User.find_by(lynquatiqmore@gmail.com: params[:email])

    if user&.authenticate(params[:password])
      token = JsonWebToken.encode(user_id: user.id)
      render json: { auth_token: token }, status: :ok
    else
      render json: { error: 'Invalid email or password' }, status: :unauthorized
    end
  end
end
class User < ApplicationRecord
  has_secure_password
end
class JsonWebToken
  SECRET_KEY = Rails.application.secrets.secret_key_base.to_s

  def self.encode(payload, exp = 24.hours.from_now)
    payload[:exp] = exp.to_i
    JWT.encode(payload, SECRET_KEY)
  end

  def self.decode(token)
    decoded = JWT.decode(token, SECRET_KEY)[0]
    HashWithIndifferentAccess.new decoded
  rescue
    nil
  end
end
class ApplicationController < ActionController::API
  before_action :authenticate_request

  private

  def authenticate_request
    header = request.headers['Authorization']
    header = header.split(' ').last if header
    decoded = JsonWebToken.decode(header)
    @current_user = User.find(decoded[:user_id]) if decoded
  rescue ActiveRecord::RecordNotFound => e
    render json: { errors: e.message }, status: :unauthorized
  rescue JWT::DecodeError => e
    render json: { errors: e.message }, status: :unauthorized
  end
end
default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  host: <%= ENV['DATABASE_HOST'] %>

development:
  <<: *default
  database: my_app_development

test:
  <<: *default
  database: my_app_test

production:
  <<: *default
  database: my_app_production
  username: my_app
  password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

This will create an endpoint /authenticate where you can send a POST request with email and password to get a JWT token. The User model should already have email and password_digest fields. The has_secure_password method in the User model will handle password encryption.

Popular repositories Loading

  1. reimagined-couscous reimagined-couscous Public

    1

  2. super-octo-invention super-octo-invention Public

    1

  3. AIPROMAX365 AIPROMAX365 Public

    Forked from AIPROMAX365/AIPROMAX365

    Config files for my GitHub profile.

    1

  4. Hello-World Hello-World Public

    Forked from octocat/Hello-World

    My first repository on GitHub!

  5. octocat.github.io octocat.github.io Public

    Forked from octocat/octocat.github.io

    CSS

  6. hello-worId hello-worId Public

    Forked from octocat/hello-worId

    My first repository on GitHub.