Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] When using https and the user is authenticated when refresh it gets log out #275

Closed
ezekel opened this issue Dec 30, 2024 · 19 comments
Assignees
Labels
question Further information is requested

Comments

@ezekel
Copy link

ezekel commented Dec 30, 2024

Hi @manchenkoff,

I noticed an issue when using HTTPS with both Laravel and Nuxt, and setting ssr: true in my nuxt.config. After authenticating, if I refresh the Nuxt app, it redirects me to the login page. Additionally, if I try to directly access the URL https://larabells.local/dashboard in the browser, it also redirects me to the login page.

Note:

Disabling the ssr works fine

I am using frankenphp laravell 11 and nuxt3 and caddy and docker

.env

NUXT_PUBLIC_SANCTUM_BASE_URL=https://api.larabells.local

nuxt.config.ts

// https://nuxt.com/docs/api/configuration/nuxt-config

export default defineNuxtConfig({

   compatibilityDate: '2024-04-03',
   devtools: {enabled: true},
   ssr: true,
   devServer: {
       host: 'larabells.local',
       port: 4000,
       //https:true

   },
    // vite: {
    //     server: {
    //        hmr: {
    //             protocol: 'wss',
    //             clientPort: 443,
    //         },
    //    },
    // },
   modules: [
       "@nuxtjs/tailwindcss",
       "shadcn-nuxt",
       '@nuxtjs/tailwindcss',
       '@nuxtjs/color-mode',
       "nuxt-auth-sanctum"
   ],
   colorMode: {
       classSuffix: ''
   },
   shadcn: {
       /**
        * Prefix for all the imported component
        */
       prefix: '',
       /**
        * Directory that the component lives in.
        * @default "./components/ui"
        */
       componentDir: './components/ui'
   },
   sanctum: {
       baseUrl:  process.env.NUXT_PUBLIC_API_URL, // Laravel API
       mode: 'cookie',

       /*endpoints: {
           user: '/api/user',
       },*/
       redirect: {
           onGuestOnly: '/dashboard',
           onLogin:'/dashboard',
           onLogout: '/login',
       },
   },
})

login.vue

<script setup lang="ts">
definePageMeta({
 /*layout: 'minimal',*/
 middleware: ['sanctum:guest'],
})


</script>

<template>
 <AuthLoginForm />
</template>

<style scoped>

</style>

dashboard.vue

<script setup lang="ts">
import { Button } from '~/components/ui/button'


definePageMeta({
  layout: 'auth',
  middleware: ['sanctum:auth','sanctum-verified'],
})

  <Button type="submit" class="mt-5" @click="books">
    Books
  </Button>
  

see my video

nuxt-auth-1.mp4

Thank you in advance

@ezekel ezekel added the bug Something isn't working label Dec 30, 2024
@manchenkoff
Copy link
Owner

Hey @ezekel, do you have different URL for Laravel which is accessible by your Nuxt server app?

Usually it happens if SSR and CSR call different URLs and thus cookies are not available. For more details, you can also change the sanctum module log level to 5 (you can find it in the module docs) and attach output here, it should show why your user gets unauthenticated on page refresh.

@ezekel
Copy link
Author

ezekel commented Dec 30, 2024

Hi @manchenkoff ,

can you show me where to edit the sanctum module to level 5 ?

Usually it happens if SSR and CSR call different URLs and thus cookies are not available.

but why if disable ssr it will works fine ? if I refresh it will not redirect to login page if I disable the ssr.

Thank you in advance.

@ezekel ezekel closed this as completed Dec 30, 2024
@ezekel ezekel reopened this Dec 30, 2024
@manchenkoff
Copy link
Owner

can you show me where to edit the sanctum module to level 5 ?

Please, check this link - https://manchenkoff.gitbook.io/nuxt-auth-sanctum/advanced/logging

but why if disable ssr it will works fine ? if I refresh it will not redirect to login page if I disable the ssr.

The difference between SSR and CSR is that your Laravel API is being called from different hosts, which can cause CORS issues and may not be supported by the Laravel app if the SSR host is not included in stateful domain list.

You can check more details here - https://manchenkoff.gitbook.io/nuxt-auth-sanctum/usage/cookie-authentication

@ezekel
Copy link
Author

ezekel commented Dec 31, 2024

Hi @manchenkoff

I don't know if I get the correct using logLevel

nuxt.config.ts

sanctum: {
        baseUrl:  process.env.NUXT_PUBLIC_API_URL, // Laravel API
        mode: 'cookie',

        endpoints: {
            user: '/api/user',
        },
        redirect: {
            onGuestOnly: '/dashboard',
            onLogin:'/dashboard',
            onLogout: '/login',
        },
        logLevel: 5,
    },
here is the log of my docker node when I refresh the page
  [nuxt-auth-sanctum:ssr] ⚙ Fetching user identity on plugin initialization
2024-12-31T07:30:03.998732700Z [nuxt-auth-sanctum:ssr] ⚙ [request] added default headers [ 'Accept' ]
2024-12-31T07:30:03.998751200Z [nuxt-auth-sanctum:ssr] ⚙ [request] added client headers to server request [ 'Referer', 'Origin', 'Cookie', 'User-Agent' ]
2024-12-31T07:30:03.998753800Z [nuxt-auth-sanctum:ssr] → Request headers for "/api/user" { accept: 'application/json',
2024-12-31T07:30:03.998755800Z   cookie:
2024-12-31T07:30:03.998830700Z    'XSRF-TOKEN=eyJpdiI6IjUxNFBGNjQ5NktZdUVsS05xR3BtR0E9PSIsInZhbHVlIjoiYVhZTVRNV0JaWEY5eWtKRDNqUklnSXBCUzJwaHp6U1gwSjRISFZnS1BwR3FqWEZmUHNHTkVvL2F3T1V5VmZXNXY3a3k5eUR1QTU3SjJQQ3NROWlIdlBrdFpMdUdLNWlPUkJmWmFkMEZrOTBXNjEwY2UrY2hHK09wWlM1MzE5V1ciLCJtYWMiOiIyZGMxNzE3ZGFiODE3ZWEwN2VlYzM2YWU5NWE4YjI4YzJlZmFjYzBhZjk3YzgyNWUwMjVkNWIyZjE1ZWIwNWUxIiwidGFnIjoiIn0%3D; laravel_session=eyJpdiI6Ii8vUFBzWHFjeGcrYWtqMnFyRWJuVlE9PSIsInZhbHVlIjoiUVFhTFJMYVA5UTkvQ2NCdDBNenhpakVBSzRWTWJoYUZDVm1XZ1ovdEN2cWxNL0d0VWVQZzRRb0dCR05hM2hWSE9kbHJkWmlFSUhWUzFZR2tlOVhpQi9ldTZnNitaQ0xmV2pIWEVMTHZIVEUyc05iWHJVZVZyRTJaZjVoYTFkRDciLCJtYWMiOiIyNzlkMzY3YmQzOGM1ZDU5MjczYTExOGMyZTRhMjVmZjVkYWM4ZjkyMGM5M2ZjOWJkMWNmNDA5MDlmMjY4ZWVmIiwidGFnIjoiIn0%3D; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6IjE0bkdIQjVIVlBtSXY2dTZIVTdlT2c9PSIsInZhbHVlIjoibGVBd2kyWldlYXJyeml3Rlhja2h4MFBlNDU1dmdUN2hOcFBUTUhYcFRRaUkxWGRhNFU3NXVWTmhQc0pCUkEydERtM0d3U29iMWZKRVg0cGNOTWF4TzlMbE8wcERQRnR3UFpYSCt4bGtselVZay9Id0ptczhsZmlZZlR3TFE4U1d3c0FGd09JYUU3ampWMElFUXJQdkJRLzJMbmI4TUs0U3RYVjhIQnBLWnBaRFF6SWQ5Z211aXhabnR3NFZnMms4Y2FxYzZ0dzJmaU9JMjVwNkdrK2tycVNxVDl6a2RSeEhVY2Ntc0RiQjhucz0iLCJtYWMiOiI5ZWRmMWJjNWU2NTA0YTE0MWFhNDA0Y2VhN2UyYzlhYmM3ZWYxMGQzN2E5ZTE0NmZjZTc5MjAyNDk1MmVjODBjIiwidGFnIjoiIn0%3D',
2024-12-31T07:30:03.998852400Z   origin: 'https://larabells.local',
2024-12-31T07:30:03.998855700Z   referer: 'https://larabells.local',
2024-12-31T07:30:03.998858000Z   'user-agent':
2024-12-31T07:30:03.998860500Z    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0' }
2024-12-31T07:30:03.998863200Z     at FancyReporter.formatLogObj (node_modules/consola/dist/shared/consola.DC9Xh_QO.mjs:602:20)
2024-12-31T07:30:03.998865600Z     at FancyReporter.log (node_modules/consola/dist/shared/consola.Cs5FdtHD.mjs:61:23)
2024-12-31T07:30:03.998868200Z     at Consola._log (node_modules/consola/dist/core.mjs:483:16)
2024-12-31T07:30:03.998871400Z     at resolveLog (node_modules/consola/dist/core.mjs:451:14)
2024-12-31T07:30:03.998873900Z     at Consola._logFn (node_modules/consola/dist/core.mjs:479:5)
2024-12-31T07:30:03.998876500Z     at Consola.trace (node_modules/consola/dist/core.mjs:408:19)
2024-12-31T07:30:03.998879000Z     at onRequest (node_modules/nuxt-auth-sanctum/dist/runtime/httpFactory.js:59:14)
2024-12-31T07:30:03.998881600Z     at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
2024-12-31T07:30:03.998884200Z     at async callHooks (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:134:7)
2024-12-31T07:30:03.998886600Z     at async $fetchRaw2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:207:7)
2024-12-31T07:30:04.000504200Z 
2024-12-31T07:30:04.000547100Z [nuxt-auth-sanctum:ssr]  ERROR  Unable to load user identity from API [GET] "https://api.larabells.local/api/user": <no response> fetch failed
2024-12-31T07:30:04.000553300Z 
2024-12-31T07:30:04.000577500Z     at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
2024-12-31T07:30:04.000580400Z     at async $fetch2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:316:15)
2024-12-31T07:30:04.000582800Z     at async initialIdentityLoad (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:42:20)
2024-12-31T07:30:04.000585200Z     at async setup (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:70:119)
2024-12-31T07:30:04.000588200Z     at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
2024-12-31T07:30:04.000590600Z     at async applyPlugin (node_modules/nuxt/dist/app/nuxt.js:142:25)
2024-12-31T07:30:04.000593100Z     at async executePlugin (node_modules/nuxt/dist/app/nuxt.js:179:9)
2024-12-31T07:30:04.000619200Z     at async applyPlugins (node_modules/nuxt/dist/app/nuxt.js:193:5)
2024-12-31T07:30:04.000622300Z     at async createNuxtAppServer (node_modules/nuxt/dist/app/entry.js:15:7)
2024-12-31T07:30:04.000625500Z     at async default (node_modules/@nuxt/vite-builder/dist/runtime/vite-node.mjs:34:18)
2024-12-31T07:30:04.000641200Z 
2024-12-31T07:30:04.000642900Z   [cause]: fetch failed
2024-12-31T07:30:04.000644700Z 
2024-12-31T07:30:04.000646400Z       at node:internal/deps/undici/undici:13185:13
2024-12-31T07:30:04.000648100Z       at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
2024-12-31T07:30:04.000649900Z       at async $fetchRaw2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:258:26)
2024-12-31T07:30:04.000651900Z       at async $fetch2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:316:15)
2024-12-31T07:30:04.000653700Z       at async initialIdentityLoad (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:42:20)
2024-12-31T07:30:04.000655500Z       at async setup (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:70:119)
2024-12-31T07:30:04.000657200Z       at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
2024-12-31T07:30:04.000659000Z       at async applyPlugin (node_modules/nuxt/dist/app/nuxt.js:142:25)
2024-12-31T07:30:04.000660900Z       at async executePlugin (node_modules/nuxt/dist/app/nuxt.js:179:9)
2024-12-31T07:30:04.000662600Z       at async applyPlugins (node_modules/nuxt/dist/app/nuxt.js:193:5)
2024-12-31T07:30:04.000664300Z 
2024-12-31T07:30:04.000666000Z     [cause]: connect ECONNREFUSED 172.18.0.4:443
2024-12-31T07:30:04.000667700Z 
2024-12-31T07:30:04.000669300Z         at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1611:16)
2024-12-31T07:30:04.000671000Z         at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)
2024-12-31T07:30:04.000672800Z 
2024-12-31T07:30:04.045345200Z [nuxt-auth-sanctum:ssr] ⚙ Fetching user identity on plugin initialization
2024-12-31T07:30:04.047588000Z [nuxt-auth-sanctum:ssr] ⚙ [request] added default headers [ 'Accept' ]
2024-12-31T07:30:04.047629900Z [nuxt-auth-sanctum:ssr] ⚙ [request] added client headers to server request [ 'Referer', 'Origin', 'Cookie', 'User-Agent' ]
2024-12-31T07:30:04.047633200Z [nuxt-auth-sanctum:ssr] → Request headers for "/api/user" { accept: 'application/json',
2024-12-31T07:30:04.047645100Z   cookie:
2024-12-31T07:30:04.047649000Z    'XSRF-TOKEN=eyJpdiI6IjUxNFBGNjQ5NktZdUVsS05xR3BtR0E9PSIsInZhbHVlIjoiYVhZTVRNV0JaWEY5eWtKRDNqUklnSXBCUzJwaHp6U1gwSjRISFZnS1BwR3FqWEZmUHNHTkVvL2F3T1V5VmZXNXY3a3k5eUR1QTU3SjJQQ3NROWlIdlBrdFpMdUdLNWlPUkJmWmFkMEZrOTBXNjEwY2UrY2hHK09wWlM1MzE5V1ciLCJtYWMiOiIyZGMxNzE3ZGFiODE3ZWEwN2VlYzM2YWU5NWE4YjI4YzJlZmFjYzBhZjk3YzgyNWUwMjVkNWIyZjE1ZWIwNWUxIiwidGFnIjoiIn0%3D; laravel_session=eyJpdiI6Ii8vUFBzWHFjeGcrYWtqMnFyRWJuVlE9PSIsInZhbHVlIjoiUVFhTFJMYVA5UTkvQ2NCdDBNenhpakVBSzRWTWJoYUZDVm1XZ1ovdEN2cWxNL0d0VWVQZzRRb0dCR05hM2hWSE9kbHJkWmlFSUhWUzFZR2tlOVhpQi9ldTZnNitaQ0xmV2pIWEVMTHZIVEUyc05iWHJVZVZyRTJaZjVoYTFkRDciLCJtYWMiOiIyNzlkMzY3YmQzOGM1ZDU5MjczYTExOGMyZTRhMjVmZjVkYWM4ZjkyMGM5M2ZjOWJkMWNmNDA5MDlmMjY4ZWVmIiwidGFnIjoiIn0%3D; remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d=eyJpdiI6IjE0bkdIQjVIVlBtSXY2dTZIVTdlT2c9PSIsInZhbHVlIjoibGVBd2kyWldlYXJyeml3Rlhja2h4MFBlNDU1dmdUN2hOcFBUTUhYcFRRaUkxWGRhNFU3NXVWTmhQc0pCUkEydERtM0d3U29iMWZKRVg0cGNOTWF4TzlMbE8wcERQRnR3UFpYSCt4bGtselVZay9Id0ptczhsZmlZZlR3TFE4U1d3c0FGd09JYUU3ampWMElFUXJQdkJRLzJMbmI4TUs0U3RYVjhIQnBLWnBaRFF6SWQ5Z211aXhabnR3NFZnMms4Y2FxYzZ0dzJmaU9JMjVwNkdrK2tycVNxVDl6a2RSeEhVY2Ntc0RiQjhucz0iLCJtYWMiOiI5ZWRmMWJjNWU2NTA0YTE0MWFhNDA0Y2VhN2UyYzlhYmM3ZWYxMGQzN2E5ZTE0NmZjZTc5MjAyNDk1MmVjODBjIiwidGFnIjoiIn0%3D',
2024-12-31T07:30:04.047659100Z   origin: 'https://larabells.local',
2024-12-31T07:30:04.047661000Z   referer: 'https://larabells.local',
2024-12-31T07:30:04.047662800Z   'user-agent':
2024-12-31T07:30:04.047664600Z    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0' }
2024-12-31T07:30:04.047666400Z     at FancyReporter.formatLogObj (node_modules/consola/dist/shared/consola.DC9Xh_QO.mjs:602:20)
2024-12-31T07:30:04.047668100Z     at FancyReporter.log (node_modules/consola/dist/shared/consola.Cs5FdtHD.mjs:61:23)
2024-12-31T07:30:04.047669900Z     at Consola._log (node_modules/consola/dist/core.mjs:483:16)
2024-12-31T07:30:04.047672700Z     at resolveLog (node_modules/consola/dist/core.mjs:451:14)
2024-12-31T07:30:04.047674400Z     at Consola._logFn (node_modules/consola/dist/core.mjs:479:5)
2024-12-31T07:30:04.047676200Z     at Consola.trace (node_modules/consola/dist/core.mjs:408:19)
2024-12-31T07:30:04.047677900Z     at onRequest (node_modules/nuxt-auth-sanctum/dist/runtime/httpFactory.js:59:14)
2024-12-31T07:30:04.047679700Z     at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
2024-12-31T07:30:04.047681500Z     at async callHooks (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:134:7)
2024-12-31T07:30:04.047683200Z     at async $fetchRaw2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:207:7)
2024-12-31T07:30:04.048850500Z 
2024-12-31T07:30:04.048885100Z [nuxt-auth-sanctum:ssr]  ERROR  Unable to load user identity from API [GET] "https://api.larabells.local/api/user": <no response> fetch failed
2024-12-31T07:30:04.048890100Z 
2024-12-31T07:30:04.048891900Z     at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
2024-12-31T07:30:04.048893700Z     at async $fetch2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:316:15)
2024-12-31T07:30:04.048895500Z     at async initialIdentityLoad (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:42:20)
2024-12-31T07:30:04.048897400Z     at async setup (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:70:119)
2024-12-31T07:30:04.048899100Z     at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
2024-12-31T07:30:04.048900800Z     at async applyPlugin (node_modules/nuxt/dist/app/nuxt.js:142:25)
2024-12-31T07:30:04.048902600Z     at async executePlugin (node_modules/nuxt/dist/app/nuxt.js:179:9)
2024-12-31T07:30:04.048904300Z     at async applyPlugins (node_modules/nuxt/dist/app/nuxt.js:193:5)
2024-12-31T07:30:04.048913000Z     at async createNuxtAppServer (node_modules/nuxt/dist/app/entry.js:15:7)
2024-12-31T07:30:04.048914900Z     at async default (node_modules/@nuxt/vite-builder/dist/runtime/vite-node.mjs:34:18)
2024-12-31T07:30:04.048916700Z 
2024-12-31T07:30:04.048918400Z   [cause]: fetch failed
2024-12-31T07:30:04.048920100Z 
2024-12-31T07:30:04.048921700Z       at node:internal/deps/undici/undici:13185:13
2024-12-31T07:30:04.048923400Z       at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
2024-12-31T07:30:04.048925200Z       at async $fetchRaw2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:258:26)
2024-12-31T07:30:04.048926900Z       at async $fetch2 (node_modules/ofetch/dist/shared/ofetch.03887fc3.mjs:316:15)
2024-12-31T07:30:04.048928700Z       at async initialIdentityLoad (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:42:20)
2024-12-31T07:30:04.048930500Z       at async setup (node_modules/nuxt-auth-sanctum/dist/runtime/plugin.js:70:119)
2024-12-31T07:30:04.048932300Z       at async Object.callAsync (node_modules/unctx/dist/index.mjs:72:16)
2024-12-31T07:30:04.048934000Z       at async applyPlugin (node_modules/nuxt/dist/app/nuxt.js:142:25)
2024-12-31T07:30:04.048935800Z       at async executePlugin (node_modules/nuxt/dist/app/nuxt.js:179:9)
2024-12-31T07:30:04.048937500Z       at async applyPlugins (node_modules/nuxt/dist/app/nuxt.js:193:5)
2024-12-31T07:30:04.048939200Z 
2024-12-31T07:30:04.048940900Z     [cause]: connect ECONNREFUSED 172.18.0.4:443
2024-12-31T07:30:04.048942600Z 
2024-12-31T07:30:04.048944200Z         at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1611:16)
2024-12-31T07:30:04.048946000Z         at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)
2024-12-31T07:30:04.048947800Z 

    APP_URL=https://api.larabells.local
FRONTEND_URL=https://larabells.local
SANCTUM_STATEFUL_DOMAINS=larabells.local
SESSION_DOMAIN=.larabells.local

I tried also to include the api to the stateful domains but no luck

SANCTUM_STATEFUL_DOMAINS=larabells.local,api.larabells.local

I also have middleware

middleware/sanctum_verified.ts

export default defineNuxtRouteMiddleware(() => {
  const sanctumConfig = useSanctumConfig()

  const { isAuthenticated, user } = useSanctumAuth()

  if (!isAuthenticated.value) {
    if (sanctumConfig.redirect.onAuthOnly === false) {
      return createError({
        statusCode: 409,
        message: 'You must verify your email to access this page',
        fatal: true,
      })
    }

    return navigateTo(sanctumConfig.redirect.onAuthOnly)
  }

  if (user.value?.email_verified_at !== null) {
    console.log("verified")
    return
  }

  return navigateTo('/verify-email')
})

@ezekel
Copy link
Author

ezekel commented Dec 31, 2024

I saw this it looks like same problem withe me ssr enable

#209

but I tried to put this to sanctum module it doesnt work

client: {
            initialRequest: true,
        }
    sanctum: {
        baseUrl:  process.env.NUXT_PUBLIC_API_URL, // Laravel API
        mode: 'cookie',

        endpoints: {
            user: '/api/user',
        },
        redirect: {
            onGuestOnly: '/dashboard',
            onLogin:'/dashboard',
            onLogout: '/login',
        },
        logLevel: 5,
        client: {
            initialRequest: true,
        }
    },

@manchenkoff
Copy link
Owner

2024-12-31T07:30:04.048885100Z [nuxt-auth-sanctum:ssr] ERROR Unable to load user identity from API [GET] "https://api.larabells.local/api/user": fetch failed

@ezekel, in your case, this is a problem. Your Laravel API is not reachable from the Nuxt SSR image, probably because your Docker container is named differently. It has nothing to do with the initialRequest config. If you have Laravel and Nuxt apps under the same network, you should name your Laravel container by the domain name as well, in your case - api.larabells.local should be fine.

I tried also to include the api to the stateful domains but no luck SANCTUM_STATEFUL_DOMAINS=larabells.local,api.larabells.local

This is unnecessary because api.* domain is not for frontend callers.

@manchenkoff manchenkoff added question Further information is requested and removed bug Something isn't working labels Dec 31, 2024
@ezekel
Copy link
Author

ezekel commented Jan 2, 2025

@manchenkoff

This is my Docker Compose setup. Actually, my container name is already set by its domain name, as it has been configured this way previously. However, I still have no luck when I enable ssr: true

       caddy:
        image: caddy:latest
        container_name: caddy-breeze
        restart: unless-stopped
        ports:
            - "80:80"
            - "443:443"
            - "443:443/udp" # HTTP/3
        volumes:
            - caddy_data:/data
            - caddy_config:/config
            - ./Caddyfile:/etc/caddy/Caddyfile
        networks:
            - mynetwork
            
            
    php2:
        container_name: api.larabells.local
        networks:
            - mynetwork
            
            
       node:
        container_name: larabells.local  
        networks:
            - mynetwork
        depends_on:
            - php2    
            
            
      networks:
     mynetwork:
        name: my-network
        external: true          

Running docker network ls

8db0bb10da80 my-network bridge local

@manchenkoff
Copy link
Owner

manchenkoff commented Jan 2, 2025

@ezekel I would recommend trying to ping the Laravel container from your Nuxt container to determine the problem (by opening a terminal session, for example). All of them should be in the same network and be available by container name as a host.

@ezekel
Copy link
Author

ezekel commented Jan 2, 2025

@manchenkoff

I exec my node container and ping my laravel

/app # ping api.larabells.local
PING api.larabells.local (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.044 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.049 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.048 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.050 ms
64 bytes from 172.18.0.3: seq=4 ttl=64 time=0.048 ms
64 bytes from 172.18.0.3: seq=5 ttl=64 time=0.047 ms
64 bytes from 172.18.0.3: seq=6 ttl=64 time=0.050 ms
64 bytes from 172.18.0.3: seq=7 ttl=64 time=0.073 ms
64 bytes from 172.18.0.3: seq=8 ttl=64 time=0.049 ms
64 bytes from 172.18.0.3: seq=9 ttl=64 time=0.088 ms
64 bytes from 172.18.0.3: seq=10 ttl=64 time=0.050 ms
64 bytes from 172.18.0.3: seq=11 ttl=64 time=0.049 ms
64 bytes from 172.18.0.3: seq=12 ttl=64 time=0.087 ms
64 bytes from 172.18.0.3: seq=13 ttl=64 time=0.049 ms
64 bytes from 172.18.0.3: seq=14 ttl=64 time=0.049 ms
64 bytes from 172.18.0.3: seq=15 ttl=64 time=0.052 ms
64 bytes from 172.18.0.3: seq=16 ttl=64 time=0.051 ms
64 bytes from 172.18.0.3: seq=17 ttl=64 time=0.052 ms
64 bytes from 172.18.0.3: seq=18 ttl=64 time=0.053 ms
^C
--- api.larabells.local ping statistics ---
19 packets transmitted, 19 packets received, 0% packet loss
round-trip min/avg/max = 0.044/0.054/0.088 ms

@manchenkoff
Copy link
Owner

I exec my node container and ping my laravel

I see, then the problem is different. Is it also available from Nuxt as https connection? You can try curl https://api.larabells.local or some existing endpoint in your API

@ezekel
Copy link
Author

ezekel commented Jan 2, 2025

I can see my api is loading via https

image

@manchenkoff
Copy link
Owner

I can see my api is loading via https

not from the browser, I meant from your Nuxt container since you have problem with SSR only

@ezekel
Copy link
Author

ezekel commented Jan 2, 2025

I can see my api is loading via https

not from the browser, I meant from your Nuxt container since you have problem with SSR only

/app # curl -v https://api.larabells.local
* Host api.larabells.local:443 was resolved.
* IPv6: (none)
* IPv4: 172.18.0.3
*   Trying 172.18.0.3:443...
* connect to 172.18.0.3 port 443 from 172.18.0.5 port 37176 failed: Connection refused
* Failed to connect to api.larabells.local port 443 after 1 ms: Could not connect to server
* closing connection #0
curl: (7) Failed to connect to api.larabells.local port 443 after 1 ms: Could not connect to server
/app # 

@manchenkoff
Copy link
Owner

@ezekel now you have the reason why SSR doesn't work, apparently your container is not available under 443 port, I am not familiar with the containers you use, but I would imagine that you have some Caddy proxy for HTTPS, which is not Laravel instance. In that case you should use api.larabells.local as a name for the proxy instead and use a different value for Laravel container.

@ezekel
Copy link
Author

ezekel commented Jan 2, 2025

@manchenkoff

yes I use proxy and this is my caddfile

api.larabells.local {
	reverse_proxy api.larabells.local:8000
}
larabells.local {
    reverse_proxy larabells.local:4000
}

@ezekel
Copy link
Author

ezekel commented Jan 2, 2025

I will be back I will try to fix the proxy

@manchenkoff
Copy link
Owner

So, in your case this is how Nuxt works:

  • In browser: CSR -> https://api.larabells.local -> localhost:443 -> caddy-breeze:443 -> api.larabells.local:8000
  • In Nuxt container: SSR -> https://api.larabells.local -> api.larabells.local:443 (no container serving)

@ezekel ezekel closed this as completed Jan 3, 2025
@ezekel
Copy link
Author

ezekel commented Jan 3, 2025

@manchenkoff

Actually I was not able to fix this in my local machine https with ssr enable, my question if I deploy this to the server using docker does my problem will fix since it is already live server not in local?

@ezekel ezekel reopened this Jan 3, 2025
@manchenkoff
Copy link
Owner

@manchenkoff

Actually I was not able to fix this in my local machine https with ssr enable, my question if I deploy this to the server using docker does my problem will fix since it is already live server not in local?

It depends on how you manage the network, if you have the same setup, it will be the same problem. However, if you work with a direct domain name instead of a host, it should be fine.

@ezekel ezekel closed this as completed Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants