user199135
user199135

Reputation: 17

Devise not accepting JSON Token

I looked at the parameters in rails console only to find out it added users so I did like this

val json = parseToJsonElement("""
        {"name":"$name","password":"$password"}}"""

However, it still rejects it as 401 Unauthorized access on the rails side

I used to be able to login with my JSON token through Android for devise until I decided to add custom views

How to fix?

In console:

Started POST "/users/sign_in" for 127.0.0.1 at 2025-01-10 22:40:28 +0200
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC /*application='LoginApi'*/
Processing by Devise::SessionsController#create as HTML
Completed 401 Unauthorized in 25ms (ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.0ms)


Processing by Devise::SessionsController#new as HTML
Completed 200 OK in 16ms (Views: 0.2ms | ActiveRecord: 3.3ms (0 queries, 0 cached) | GC: 0.0ms)

Regestration controller:

  def create
    super
    logger.debug "here it is."
    devise_api_token = current_devise_api_token
    if devise_api_token
        render json: { message: "You are logged in as #{devise_api_token.resource_owner_name}"}, status: :ok
    else
        render json: { message: "You are not logged in"}, status: :unauthorised
    end
  end

Application Controller:

class ApplicationController < ActionController::API
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
  end
end

cors.rb:

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

     resource "*",
       headers: :any,
       methods: [:get, :post, :put, :patch, :delete, :options, :head]
   end
 end

tried with and without config.authentication_keys = [:name] in config/devise.rb

Upvotes: -2

Views: 63

Answers (0)

Related Questions