Subi
Subi

Reputation: 23

rails api app with devise gem- always getting 401 error

I have set up a new rails api app and trying to use devise for login, but even with correct email and password getting 401 always

Trying to solve it for a couple of days but no luck

Below are the details

Rails 7.1.3.2
devise (4.9.4)

config/routes.rb

Rails.application.routes.draw do
  devise_for :users, path: '', path_names: {
    sign_in: 'login',
    sign_out: 'logout',
    registration: 'signup'
  },
  controllers: {
    sessions: 'users/sessions',
    registrations: 'users/registrations'
  }
  get "up" => "rails/health#show", as: :rails_health_check

  namespace :api do
    namespace :v1 do
      resources :users
    end
  end
end

app/controllers/users/sessions_controller.rb

class Users::SessionsController < Devise::SessionsController
  respond_to :json
end

app/models/user.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end

The email and password are correct and is available in DB. Even tough in postman I am getting 401

Postman

POST: localhost:3000/login
Body: {
    "email": "[email protected]",
    "password": "123456"
}
---------------------------
response
You need to sign in or sign up before continuing.

Rails Server Logs

Started POST "/login" for ::1 at 2024-05-08 18:42:36 +0530
  ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by Users::SessionsController#create as */*
  Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]", "session"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 4ms (ActiveRecord: 0.0ms | Allocations: 2586)

schema.rb

ActiveRecord::Schema[7.1].define(version: 2024_04_28_060440) do
  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

The below suggestion will not work since I have created api only application (there wont be any views)

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

Added user through rails console

u = User.new(email: '[email protected]')
u.password = '123456'
u.password_confirmation = '123456'
u.save!

Upvotes: 0

Views: 53

Answers (1)

Subi
Subi

Reputation: 23

Figured out what is wrong.

The request body should contain user object like

{ "user": 
  {
    "email": "[email protected]",
    "password": "123456"
  }
}

Simple mistake.

Thanks for trying to figure out the issue.

Upvotes: 0

Related Questions