Skip to content

Commit

Permalink
feat: more compability for nuxt@next
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya Parsa committed Dec 28, 2017
1 parent 994152b commit d50be11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
18 changes: 2 additions & 16 deletions lib/templates/auth.plugin.js
Original file line number Diff line number Diff line change
@@ -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 })
}
18 changes: 11 additions & 7 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.setToken(token, '<%= options.token.type %>');
(this.$axios || this.app.$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.$ctx.res.setHeader('Set-Cookie', Cookie.serialize('<%= options.token.cookieName %>', token, params))
this.$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.$ctx.req.headers.cookie
const cookieStr = process.browser ? document.cookie : this.$req.headers.cookie
const cookies = Cookie.parse(cookieStr || '') || {}
token = cookies['<%= options.token.cookieName %>']
}
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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 %>'])
Expand All @@ -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)
Expand Down

0 comments on commit d50be11

Please sign in to comment.