Answers for "rails g sessions controller in rails"

0

rails g sessions controller in rails

Rails.application.routes.draw do
  resources :sessions, only: [:create]
end
Posted by: Guest on October-30-2020
0

rails g sessions controller in rails

class SessionsController < ApplicationController
  def create
    user = User
            .find_by(email: params["user"]["email"])
            .try(:authenticate, params["user"]["password"])

    if user
      session[:user_id] = user.id
      render json: {
        status: :created,
        logged_in: true,
        user: user
      }
    else
      render json: { status: 401 }
    end
  end
end
Posted by: Guest on October-30-2020

Browse Popular Code Answers by Language