Kasheftin / nuxt-auth-from-scratch

48 stars 19 forks source link

nuxtServerInit resolving true before api.auth.me() promise returned #5

Closed toddheslin closed 6 years ago

toddheslin commented 6 years ago

Hey @Kasheftin thanks for this guide, it is absolutely awesome.

I've found a bug (maybe it's not?) where full page reloads within admin are causing this line: https://github.com/Kasheftin/nuxt-auth-from-scratch/blob/c322de98f3b0dc4e44784e577c64649bd260b187/frontend/middleware/auth.js#L2 to evaluate to false.

The problem seems to be that this line: https://github.com/Kasheftin/nuxt-auth-from-scratch/blob/c322de98f3b0dc4e44784e577c64649bd260b187/frontend/store/index.js#L22 is returning before the api.auth.me() promise is resolved here: https://github.com/Kasheftin/nuxt-auth-from-scratch/blob/c322de98f3b0dc4e44784e577c64649bd260b187/frontend/store/auth.js#L22

Consequence is that commit('set_user', response.data.result) is never committed and const userIsLoggedIn = !!store.state.auth.user becomes false.

What are your thoughts?

Kasheftin commented 6 years ago

Hi, my first suggestion is that the auth.me request does not return the successful result when requested from server side. resolve(true) should not mean anything here since it's just a mark to nuxt to resolve nuxtServerInit and continue execution. It's simple for you to check - add some console.log debug to #22 and #26 rows of https://github.com/Kasheftin/nuxt-auth-from-scratch/blob/c322de98f3b0dc4e44784e577c64649bd260b187/frontend/store/auth.js and check what case you have. I suppose your case is the second one. Then it should throw an error, and the resolve(false) should be evaluated in nuxtServerInit (but the argument of resolve of nuxtServerInit does not matter anything).

toddheslin commented 6 years ago

Thanks for the reply @Kasheftin

Okay a few things:

1) the /auth/me route is working fine when the request comes from client-side requests but failing on server-side requests.

2) I logged as you suggested and found this in the error block:

Error: connect ECONNREFUSED 127.0.0.1:80
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 80,

This leads me to think that this isn't working on hard page refresh:

import { baseURL } from '~/config'
axios.defaults.baseURL = baseURL

And then I found it! I missed adding the init plugin to nuxt.config.js plugins: ['~/plugins/vuetify.js', '~/api/init.js'],

Appreciate your help @Kasheftin