Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4636

Ruby on Rails 8 API not allowing mobile phone connection

$
0
0

My ruby on rails 8 API works with postman, but I get 401 unauthorized access when trying to connect through android studio avd. I tried to stick to this link https://husseinelgammal.hashnode.dev/implementing-an-authentication-in-a-ruby-on-rails-api-only-project-using-devise-and-jwt#heading-enabling-cors as much as possible and modified config/devise.rb with config.authentication_keys = [:name]

MainActivity.kt

var baseURL = "http://10.0.2.2:3000/"var connectURL = baseURL+"users/login"var rrr: List<Any> = mutableListOf()//MainClass body    fun connectButton(view: View) {        if (binding.loginNameField.text.isNotEmpty() && binding.loginPasswordField.text.isNotEmpty()) {            Thread{                rrr = fuelPost(connectURL)            }.start()        }    }    private fun fuelPost(inputURL: String,                                 name: String = binding.loginNameField.text.toString(),                                 password: String = binding.loginPasswordField.text.toString()): List<Any> {        val json = parseToJsonElement("""{"user":{"name":"$name","password":"$password"}}""")        val (request, response, result) = Fuel.post(inputURL).body(json.toString()).response()        Log.i("request", request.toString())        Log.i("response", response.toString())        Log.i("result", result.toString())        return listOf(request, response, result)    }}

From Rails console:

Started POST "/users/login" for 127.0.0.1 at 2025-01-15 15:18:05 +0200Processing by Users::SessionsController#create as JSONCompleted 401 Unauthorized in 1ms (ActiveRecord: 0.0ms (0 queries, 0 cached) | GC: 0.0ms)

Android Studio console

--> POST http://10.0.2.2:3000/users/loginBody : {"user":{"name":"MUSTDOS","password":"fakepass"}}...Body : {"error":"You need to sign in or sign up before continuing."}.............

sessions_controller.rb

class Users::SessionsController < Devise::SessionsController    include RackSessionsFix  private  def respond_with(current_user, _opts = {})  logger.debug "current user: #{current_user.inspect}"    render "users/sessions/session_success", status: :ok  end  def respond_to_on_destroy    if current_user      render "users/sessions/session_destroy", status: :ok    else      render "users/sessions/session_destroy_errors", status: :unauthorized    end  endend

Viewing all articles
Browse latest Browse all 4636

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>