[RUBY] [Rails] Return login result in JSON format (for beginners)

Introduction

I wasn't sure what API communication was doing at first. This time, so that people with the same problems can have an image as much as possible. I will introduce an implementation that returns the response result after execution in JSON without creating a UI. The subject is sign-up and login, which Rails beginners probably learned first.

code

routes.rb


Rails.application.routes.draw do
  #In namespace/api/v1/Create a hierarchy of
  namespace :api, format: :json do
    namespace :v1 do
      post :sign_up,  to: 'users#sign_up'
      post :login,   to: 'users#login'
    end
  end
end

users_controller.rb


class Api::V1::UsersController < ApplicationController ##Match class name to hierarchy
  def sign_up
    user = User.new(user_params)
    if user.save
      render status: "200", json: { result: user }  #Return user information in json format when saving is successful
    else
      render status: "400", json: { result: "Email address or password is not valid" }
    end
  end

  def login
    user = User.find_by(email: params[:email])
    if user && user.authenticate(params[:password])
      render status: "200", json: { result: user }  #Return user information in json format when login is successful
    else
      render status: "400", json: { result: "Email address or password do not match" }
    end
  end

  private

    def user_params
      params.permit(:email, :password)
    end
end

Try it with Postman

For those who have never used it

After installing (https://www.postman.com/), please try as shown in the image. After setting, click the "Send" button to execute! スクリーンショット 2020-12-20 21.45.33.png スクリーンショット 2020-12-20 21.47.01.png

Sign-up success example

スクリーンショット 2020-12-20 21.34.48.png

Login successful example

スクリーンショット 2020-12-20 21.35.49.png

Login failure example

スクリーンショット 2020-12-20 21.36.25.png

When you do not want to display all the response contents

You can easily limit it using as_json. Reference URL https://qiita.com/tbaba/items/ea44025b056f708ab0b4

in conclusion

Once I knew it, it was surprisingly easy. But beginners take time to know this. .. .. I hope it helps someone.

Recommended Posts

[Rails] Return login result in JSON format (for beginners)
How to implement login request processing (Rails / for beginners)
(For beginners) [Rails] Install Devise
[rails] Login screen implementation in devise
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
Implement simple login function in Rails
In the login function, processing that prevents others from editing or deleting posts (Rails / for beginners)
[Rails] Function restrictions in devise (login / logout)
[Ruby on Rails] About bundler (for beginners)
Output Spring Boot log in json format
Try using JSON format API in Java
must not return in the for statement
Rails [For beginners] Implementation of comment function
Explanation of Ruby on rails for beginners ①
Beginners create portfolio in Ruby on Rails
[For beginners] How to debug in Eclipse
[For rails beginners] Specify transition destination after logging in to multiple Devise models
Validation settings for Ruby on Rails login function
[Rails 6.0] "Easy login" implementation procedure required for portfolio
[For beginners] Procedure for creating a controller using rails
Use pagy for pagination in your Rails app.
Remedy for command not found in rails s
[Rails] Display popular posts in ranking format [Easy]