[Ruby on Rails] Implement login function by add_token_to_users with API

Introduction

Creating a web service with API. To implement a user login function. Since it was the first time to create an API login function, I implemented it referring to the following page.

Implementation of login API for token authentication in Rails --Qiita

environment

MacBookAir ruby 2.6.3 Rails 6.0.3.2

Think about routing

config/routes.rb


Rails.application.routes.draw do
  namespace 'api' do
    namespace 'v1' do
      post '/login', to: 'sessions#create'
    end
  end
end

Add migration

$rails g migration Add_token_To_Users token:token

The following is created

db/migrate/20200911133819_add_token_to_users.rb


class AddTokenToUsers < ActiveRecord::Migration[6.0]
  def change
    add_column :users, :token, :string
    add_index :users, :token, unique: true
  end
end

model

class User < ApplicationRecord
    has_secure_token
end

controller

app/controllers/api/v1/users_controller.rb


module Api
    module V1
class SessionsController < ApplicationController
  def create
    user = User.find_by(email: session_params[:email])

    if user&.authenticate(session_params[:password])
        session[:user_id] = user.id
        return render json: { token: user.token}
    else
        return render json: { status: 401, message: "certification failed" } 
    end

   end

  private

  def session_params
    params.require(:session).permit(:email, :password)
  end
end
end
end

Try logging in with POSTMAN

It looks like it's done.

スクリーンショット 2020-09-19 16.51.37.png

At the end

I have to implement the logout function from now on.

Recommended Posts

[Ruby on Rails] Implement login function by add_token_to_users with API
Validation settings for Ruby on Rails login function
Implementation of Ruby on Rails login function (Session)
Login function implementation with rails
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Introduced paging function
Implement login function in Rails simply by name and password (1)
Implement login function in Rails simply by name and password (2)
Implement simple login function in Rails
[Ruby on Rails] CSV output function
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
Implement login function simply with name and password in Rails (3)
I want to add a browsing function with ruby on rails
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Follow function implementation: Bidirectional
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
Try to implement login function with Spring-Boot
Make a login function with Rails anyway
(2021) Ruby on Rails administrator (admin) login creation
[Ruby on Rails] Model test with RSpec
[Ruby on rails] Implementation of like function
[Ruby on Rails] Logical deletion (withdrawal function)
Introducing Rspec with Ruby on Rails x Docker
Publish the app made with ruby on rails
Ruby on Rails Email automatic sending function implementation
[Rails] Procedure for linking databases with Ruby On Rails
Implement search function with Rakuten Books (DVD) API
Determine the current page with Ruby on Rails
[Ruby on Rails] Upload multiple images with refile
[Ruby on Rails] Post editing function (update, delete)
I made a portfolio with Ruby On Rails
How to implement image posting function using Active Storage in Ruby on Rails
[Rails] Implement search function
Ruby on Rails basics
Ruby On Rails Association
[Ruby on Rails] Asynchronous communication of posting function, ajax
Run Ruby on Rails RSpec tests with GitHub Actions
[Ruby on Rails] Posting score ranking function (whole display)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Ruby on Rails] Post image preview function in refile
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[Ruby on Rails] Search function (model, method selection formula)
Created RSS / Atom format sitemap with Ruby on Rails
[Ruby on Rails] How to implement tagging / incremental search function for posts (without gem)
Implement application function in Rails
I tried installing Ruby on Rails related plugin with vim-plug
Ruby on rails learning record -2020.10.03
[Rails] Implement User search function
Introduced graph function with rails
Ruby on Rails Email automatic sending function setting (using gmail)
Ruby on rails learning record -2020.10.04
Implement follow function in Rails
[Ruby on Rails] Bookmark (favorite registration, like) function: One direction
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05