1

I used to use adonuxt which worked nice but it's depreciated. So I am now trying to use nuxt.js separate with adonis backend. I am trying to make login system. These two projects run on two separate port in localhost. http://localhost:3000/ nuxt project and http://localhost:3333/ adonis project.

How can I get logged in user information in nuxtServerInit?

I am trying like this

async nuxtServerInit({ commit }, { $axios }) {
 console.log('I am running as nuxt server init')

 try {
   // get the initial data
   let { data } = await axios.get('http://localhost:3333/myuser')
   commit('loginUser', data)
 } catch (e) {
     console.log('nuxt server error ', e.response)
  }
},

and in my adonis.js I am trying

async getUser({request, response, params, auth}){
  try {
    const user = await auth.getUser()
    return user
  } catch (error) {
      return 'not logged in'
  }
}

The result is always not logged in....

Any idea how to implement.....

UPDATE

I am using a very simple login system

async user({request, response, params, auth}){
   try {
      const user = await auth.loginViaId(34)
      return user
   } catch (error) {
      return error
   }
}

I got a very interesting result now. So if I login using my adonis project then the logged in user information becomes available in nuxt project but I cannot get the logged in user if I login using nuxt axios

UPDATE 2

So, session login works, but adonis cannot find user or loggedin user when axios is used.

UPDATE 3

we can get logged in user only using nuxtServerInit.. We can get the logged in user using axios.. :(

crbast
  • 2,192
  • 1
  • 11
  • 21
Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95
  • Can you share your login controller code? – crbast Jul 25 '20 at 13:22
  • Please check the question, I updated. – Hkm Sadek Jul 25 '20 at 13:40
  • Can you add `credentials: true` to axios config (https://axios.nuxtjs.org/options.html#credentials) – crbast Jul 25 '20 at 15:16
  • I get `Access to XMLHttpRequest at 'http://localhost:3333/myuser' from origin 'http://localhost:8000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.` error – Hkm Sadek Jul 25 '20 at 15:20
  • I have cors enabled already but still this – Hkm Sadek Jul 25 '20 at 15:21
  • okay try my answer – crbast Jul 25 '20 at 15:21

1 Answers1

1

Configure adonis

config/session.js

  cookie: {
    httpOnly: true,
    path: '/',
    sameSite: 'strict',
    domain: Env.get('COOKIE_DOMAIN', null),
  },

config/cors.js

  /*
  |--------------------------------------------------------------------------
  | Credentials
  |--------------------------------------------------------------------------
  |
  | Define Access-Control-Allow-Credentials header. It should always be a
  | boolean.
  |
  */
  credentials: true, // Enable credentials cookies

The Access-Control-Allow-Credentials response header tells browsers whether to expose the response to frontend JavaScript code when the request's credentials mode (Request.credentials) is include.

More informations about Access-Control-Allow-Credentials header : developer.mozilla.org

Configure Nuxt

Enable axios credentials (https://axios.nuxtjs.org/options.html#credentials) :

nuxt.config.js

  axios: {
    credentials: true,
  },
crbast
  • 2,192
  • 1
  • 11
  • 21