diff --git a/lib/templates/auth.plugin.js b/lib/templates/auth.plugin.js index 3c73feff2..946ef3020 100644 --- a/lib/templates/auth.plugin.js +++ b/lib/templates/auth.plugin.js @@ -1,26 +1,12 @@ import './auth.middleware' import authStore from './auth.store' -export default async function (ctx, inject) { - const { store } = ctx - - // Inject $ctx - inject('ctx', ctx) - +export default async function ({ store, req, res }, inject) { // Register auth store module store.registerModule('auth', authStore, { preserveState: !!store.state.auth, }) // Fetch initial state - try { - await store.dispatch('auth/fetch') - } catch (e) { - // Not authorized - // Check axios module is correctly registered - if (!ctx.$axios) { - /* eslint-disable no-console */ - console.error('[@nuxtjs/auth]', 'Please make sure @nuxtjs/axios is added after this module!') - } - } + await store.dispatch('auth/fetch', { req, res }) } diff --git a/lib/templates/auth.store.js b/lib/templates/auth.store.js index a1e7d1941..f62937b12 100644 --- a/lib/templates/auth.store.js +++ b/lib/templates/auth.store.js @@ -41,7 +41,7 @@ export default { commit('SET_TOKEN', token) // Set Authorization token for all axios requests - this.$axios.setToken(token, '<%= options.token.type %>'); + (this.$axios || this.app.$axios).setToken(token, '<%= options.token.type %>') <% if (options.token.localStorage) { %> // Update localStorage @@ -74,7 +74,7 @@ export default { expires = date.setDate(date.getDate() - 1) params.expires = new Date(expires) } - this.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('<%= options.token.cookieName %>', token, params)) + this.$res.setHeader('Set-Cookie', Cookie.serialize('<%= options.token.cookieName %>', token, params)) } <% } %> }, @@ -95,7 +95,7 @@ export default { <% if (options.token.cookie) { %> // Try to extract token from cookies if (!token) { - const cookieStr = process.browser ? document.cookie : this.$ctx.req.headers.cookie + const cookieStr = process.browser ? document.cookie : this.$req.headers.cookie const cookies = Cookie.parse(cookieStr || '') || {} token = cookies['<%= options.token.cookieName %>'] } @@ -115,7 +115,11 @@ export default { <% if (options.user.enabled) { %> // Fetch - async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>' } = {}) { + async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>', req, res } = {}) { + // Backward compability for nuxt RCs which this.app.$ctx.req is not available + this.$req = req + this.$res = res + <% if (options.token.enabled) { %> // Fetch and update latest token dispatch('fetchToken') @@ -128,7 +132,7 @@ export default { // Try to get user profile try { - const data = await this.$axios.$<%= options.user.method.toLowerCase() %>(endpoint) + const data = await (this.$axios || this.app.$axios).$<%= options.user.method.toLowerCase() %>(endpoint) commit('SET_USER', data<%= options.user.propertyName ? ('[\'' + options.user.propertyName + '\']') : '' %>) } catch (e) { console.error(e) @@ -143,7 +147,7 @@ export default { // Login async login ({ dispatch }, { fields, endpoint = '<%= options.login.endpoint %>' } = {}) { // Send credentials to API - let data = await this.$axios.$post(endpoint, fields) + let data = await (this.$axios || this.app.$axios).$post(endpoint, fields) <% if (options.token.enabled) { %> dispatch('updateToken', data['<%= options.token.name %>']) @@ -159,7 +163,7 @@ export default { async logout ({ dispatch, state }, { endpoint = '<%= options.logout.endpoint %>' } = {}) { // Server side logout try { - await this.$axios.$<%= options.logout.method.toLowerCase() %>(endpoint) + await (this.$axios || this.app.$axios).$<%= options.logout.method.toLowerCase() %>(endpoint) } catch (e) { // eslint-disable-next-line no-console console.error('Error while logging out', e)