Permalink
Browse files

feat: improve compability for nuxt 1.0.0

  • Loading branch information...
pi0 committed Dec 29, 2017
1 parent 1a9a76e commit 7740decd6f561a6266e0922168132d50c97c4e13
Showing with 18 additions and 15 deletions.
  1. +11 −4 lib/templates/auth.plugin.js
  2. +7 −11 lib/templates/auth.store.js
@@ -1,12 +1,19 @@
import './auth.middleware'
import authStore from './auth.store'
export default async function ({ store, req, res }, inject) {
export default async function (context, inject) {
// Register auth store module
store.registerModule('auth', authStore, {
preserveState: !!store.state.auth,
context.store.registerModule('auth', authStore, {
preserveState: Boolean(context.store.state.auth),
})
// Backward compability for Nuxt <= RC.11
if (!context.store.app) {
context.store.app = {
context
}
}
// Fetch initial state
await store.dispatch('auth/fetch', { req, res })
await context.store.dispatch('auth/fetch')
}
@@ -41,7 +41,7 @@ export default {
commit('SET_TOKEN', token)
// Set Authorization token for all axios requests
(this.$axios || this.app.$axios).setToken(token, '<%= options.token.type %>')
this.$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.$res.setHeader('Set-Cookie', Cookie.serialize('<%= options.token.cookieName %>', token, params))
this.app.context.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.$req.headers.cookie
const cookieStr = process.browser ? document.cookie : this.app.context.req.headers.cookie
const cookies = Cookie.parse(cookieStr || '') || {}
token = cookies['<%= options.token.cookieName %>']
}
@@ -115,11 +115,7 @@ export default {
<% if (options.user.enabled) { %>
// Fetch
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
async fetch ({ getters, state, commit, dispatch }, { endpoint = '<%= options.user.endpoint %>' } = {}) {
<% if (options.token.enabled) { %>
// Fetch and update latest token
await dispatch('fetchToken')
@@ -132,7 +128,7 @@ export default {
// Try to get user profile
try {
const data = await (this.$axios || this.app.$axios).$<%= options.user.method.toLowerCase() %>(endpoint)
const data = await this.$axios.$<%= options.user.method.toLowerCase() %>(endpoint)
commit('SET_USER', data<%= options.user.propertyName ? ('[\'' + options.user.propertyName + '\']') : '' %>)
} catch (e) {
console.error(e)
@@ -147,7 +143,7 @@ export default {
// Login
async login ({ dispatch }, { fields, endpoint = '<%= options.login.endpoint %>' } = {}) {
// Send credentials to API
let data = await (this.$axios || this.app.$axios).$post(endpoint, fields)
let data = await this.$axios.$post(endpoint, fields)
<% if (options.token.enabled) { %>
await dispatch('updateToken', data['<%= options.token.name %>'])
@@ -163,7 +159,7 @@ export default {
async logout ({ dispatch, state }, { endpoint = '<%= options.logout.endpoint %>' } = {}) {
// Server side logout
try {
await (this.$axios || this.app.$axios).$<%= options.logout.method.toLowerCase() %>(endpoint)
await this.$axios.$<%= options.logout.method.toLowerCase() %>(endpoint)
} catch (e) {
// eslint-disable-next-line no-console
console.error('Error while logging out', e)

0 comments on commit 7740dec

Please sign in to comment.