amrnn90 / breeze-nuxt

An application / authentication starter kit frontend in Nuxt3 for Laravel Breeze.
MIT License
198 stars 33 forks source link

request to http://localhost:9000/api/user failed, reason: read ECONNRESET () #5

Closed matthiastjong closed 1 year ago

matthiastjong commented 1 year ago

I am using Docker w/ Laravel Sail to power the back-end. However, I cannot seem to make a request towards the API using this codebase.

This is the error:

500
request to http://localhost:9000/api/user failed, reason: read ECONNRESET ()

(I used port 9000 for the back-end because I already have another environment that exists on port 8000)

If I visit http://localhost:9000/api/user myself then I can see that the environment is running OK (I get redirected to /login). I have also tried the solutions mentioned at #3 but unfortunately the same error keeps getting thrown.

matthiastjong commented 1 year ago

So this seems to be working when I use php artisan serve instead of running it through Docker. However, we really rely on Docker in our workflow. Is this also possible to achieve with docker?

JaavierR commented 1 year ago

I managed to make it work with docker, using 127.0.0.1 instead of localhost on both the front and the back with different ports. But when I log in it returns the error The route dashboard could not be found. or it tells me that I am not authenticated if I ask for the user right after logging in.

JaavierR commented 1 year ago

@matthiastjong I finally make it work with Laravel Sail, but in order to work you need to access the app via http://127.0.0.1 URL instead the localhost.

The following parameters are required:

# .env in the laravel project

FRONTEND_URL=http://127.0.0.1:3000
SESSION_DOMAIN=.127.0.0.1
SANCTUM_STATEFUL_DOMAINS=127.0.0.1:3000

SESSION_DRIVER=cookie
// .env in the nuxt project

NUXT_PUBLIC_BACKEND_URL=http://127.0.0.1
NUXT_PUBLIC_FRONTEND_URL=http://127.0.0.1:3000
// nuxt.config.ts
import { defineNuxtConfig } from 'nuxt/config'

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  modules: ['@nuxtjs/tailwindcss'],
  runtimeConfig: {
    public: {
      backendUrl: process.env.NUXT_PUBLIC_BACKEND_URL,
      frontendUrl: process.env.NUXT_PUBLIC_FRONTEND_URL
    }
  },
  imports: {
    dirs: ['./utils']
  }
})

And finally when you run the app in dev mode you need to adjust the package.json command to this:

{
...,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev --host 127.0.0.1",
    "generate": "nuxt generate",
    "preview": "nuxt preview"
  },
...
}

I hope I have been clear and helpful

amrnn90 commented 1 year ago

Thanks for sharing what worked out for you @JaavierR ! FYI there is no need to change the runtimeConfig inside nuxt.config.ts as the values there are treated as the defaults and will get overridden if they are defined in your environment variables as long as they adhere to the naming convention NUXT_PUBLIC_ETC.

amrnn90 commented 1 year ago

Do the settings mentioned above fix your issues with docker @matthiastjong?

matthiastjong commented 1 year ago

Hi @amrnn90 , we have checked out your solution and it works! Amazing stuff.. After this we can authenticate our local API 😄 Thank you!