Skip to content

Commit

Permalink
feat: improve compability for nuxt 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Dec 29, 2017
1 parent 1a9a76e commit 7740dec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
15 changes: 11 additions & 4 deletions lib/templates/auth.plugin.js
Original file line number Diff line number Diff line change
@@ -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')
}
18 changes: 7 additions & 11 deletions lib/templates/auth.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
<% } %>
},
Expand All @@ -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 %>']
}
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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 %>'])
Expand All @@ -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)
Expand Down

0 comments on commit 7740dec

Please sign in to comment.