GithubHelp home page GithubHelp logo

aleksandraaleksandrovna / rest-api-authentication-jwt-ruby Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielarturoalejoalvarez/rest-api-authentication-jwt-ruby

0.0 0.0 0.0 32 KB

Software of Development with Ruby, Rails, PostgreSQL,JWT,etc (REST-API)

Ruby 99.30% HTML 0.70%

rest-api-authentication-jwt-ruby's Introduction

REST API Authentication Rails-JWT V1.0

Description

This repository is a Software of Application with Rails,JWT,PostgreSQL,etc

Installation

Using Ruby 2.7, Rails 6.0 preferably.

Software

Ruby Version Manager RVM 1.29.10-next

Database

Using PostgreSQL

Apps

Client Rest: Postman, Insomnia, Talend API Tester, etc

Plugins

You can use the gems JWT, Carrierwave, Bcrypt, Rack-Cors, etc

Usage

$ git clone https://github.com/DanielArturoAlejoAlvarez/rest-api-authentication-jwt-ruby[NAME APP]

$ bundle install

$ rails db:migrate

$ rails s

Follow the following steps and you're good to go! Important:

alt text

Coding

Routes

...
namespace :api do
  namespace :v1 do
    resources :users, param: :_username

    namespace :auth do
      post '/login', to: 'authentication#login'
    end
  end
end

get '/*a', to: 'application#not_found'
...

Middlewares

...
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete]
  end
end
...

Controllers

...
def login
  @user = User.find_by_email(params[:email])
  if @user&.authenticate(params[:password])
    token = JsonWebToken.encode({user_id: @user.id})
    time = Time.now + 24.hours.to_i
    render json: {
      token: token,
      exp: time.strftime("%m-%d-%Y %H:%M"),
      username: @user.username
    }, status: :ok
  else
    render json: { error: 'unauthorized' }, status: :unauthorized
  end
end
...

Models

...
has_secure_password
mount_uploader :avatar, AvatarUploader

validates :email, presence: true, uniqueness: true
validates :email,
          format: { with: URI::MailTo::EMAIL_REGEXP }

validates :username, presence: true, uniqueness: true

validates :password,
          length: { minimum: 6 },
          if: ->{ new_record? || !password.nil? }
...

Libraries

...
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, 'HS256')
  end

  def self.decode token
    decoded = JWT.decode(token, SECRET_KEY, true, {algorithm: 'HS256'})[0]
    HashWithIndifferentAccess.new decoded
  end
end
...

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DanielArturoAlejoAlvarez/rest-api-authentication-jwt-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.


rest-api-authentication-jwt-ruby's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.