@nuxtjs/auth
Advanced tools
Comparing version 3.2.1 to 3.3.0
@@ -5,2 +5,14 @@ # Change Log | ||
<a name="3.3.0"></a> | ||
# [3.3.0](https://github.com/nuxt-community/auth-module/compare/v3.2.1...v3.3.0) (2017-12-28) | ||
### Features | ||
* add fetchUser option ([#27](https://github.com/nuxt-community/auth-module/issues/27)) ([1b8856c](https://github.com/nuxt-community/auth-module/commit/1b8856c)) | ||
* allow customizing http method for user endpoint ([#28](https://github.com/nuxt-community/auth-module/issues/28)) ([994152b](https://github.com/nuxt-community/auth-module/commit/994152b)) | ||
* more compability for nuxt@next ([d50be11](https://github.com/nuxt-community/auth-module/commit/d50be11)) | ||
<a name="3.2.1"></a> | ||
@@ -7,0 +19,0 @@ ## [3.2.1](https://github.com/nuxt-community/auth-module/compare/v3.2.0...v3.2.1) (2017-12-20) |
@@ -10,3 +10,5 @@ const { resolve } = require('path') | ||
propertyName: 'user', | ||
resetOnFail: true | ||
resetOnFail: true, | ||
enabled: true, | ||
method: 'GET' | ||
}, | ||
@@ -13,0 +15,0 @@ login: { |
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 | ||
@@ -16,12 +11,3 @@ store.registerModule('auth', authStore, { | ||
// 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 }) | ||
} |
@@ -11,3 +11,3 @@ <% if (options.token.enabled && options.token.cookie) { %> | ||
<% if (options.token.enabled) { %>token: null,<% } %> | ||
user: null | ||
<% if (options.user.enabled) { %>user: null<% } %> | ||
}), | ||
@@ -22,2 +22,3 @@ | ||
mutations: { | ||
<% if (options.user.enabled) { %> | ||
// SET_USER | ||
@@ -27,2 +28,3 @@ SET_USER (state, user) { | ||
}, | ||
<% } %> | ||
@@ -45,3 +47,3 @@ <% if (options.token.enabled) { %> | ||
// Set Authorization token for all axios requests | ||
this.$axios.setToken(token, '<%= options.token.type %>'); | ||
(this.$axios || this.app.$axios).setToken(token, '<%= options.token.type %>') | ||
@@ -79,3 +81,3 @@ <% if (options.token.localStorage) { %> | ||
} | ||
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)) | ||
} | ||
@@ -101,3 +103,3 @@ <% } %> | ||
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 || '') || {} | ||
@@ -116,8 +118,13 @@ token = cookies['<%= options.token.cookieName %>'] | ||
reset ({ dispatch, commit }) { | ||
commit('SET_USER', null) | ||
<% if (options.user.enabled) { %>commit('SET_USER', null)<% } %> | ||
<% if (options.token.enabled) { %>dispatch('updateToken', null)<% } %> | ||
}, | ||
<% 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) { %> | ||
@@ -135,3 +142,3 @@ // Fetch and update latest token | ||
try { | ||
const data = await this.$axios.$get(endpoint) | ||
const data = await (this.$axios || this.app.$axios).$<%= options.user.method.toLowerCase() %>(endpoint) | ||
commit('SET_USER', data<%= options.user.propertyName ? ('[\'' + options.user.propertyName + '\']') : '' %>) | ||
@@ -146,2 +153,3 @@ } catch (e) { | ||
}, | ||
<% } %> | ||
@@ -151,3 +159,3 @@ // Login | ||
// Send credentials to API | ||
let data = await this.$axios.$post(endpoint, fields) | ||
let data = await (this.$axios || this.app.$axios).$post(endpoint, fields) | ||
@@ -159,3 +167,5 @@ <% if (options.token.enabled) { %> | ||
// Fetch authenticated user | ||
<% if (options.user.enabled) { %> | ||
await dispatch('fetch') | ||
<% } %> | ||
}, | ||
@@ -167,3 +177,3 @@ | ||
try { | ||
await this.$axios.$<%= options.logout.method.toLowerCase() %>(endpoint) | ||
await (this.$axios || this.app.$axios).$<%= options.logout.method.toLowerCase() %>(endpoint) | ||
} catch (e) { | ||
@@ -170,0 +180,0 @@ // eslint-disable-next-line no-console |
{ | ||
"name": "@nuxtjs/auth", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "Authentication module for Nuxt.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -31,3 +31,5 @@ # Auth Module | ||
propertyName: 'user', | ||
resetOnFail: true | ||
resetOnFail: true, | ||
enabled: true, | ||
method: 'GET', | ||
}, | ||
@@ -63,2 +65,3 @@ login: { | ||
* **resetOnFail** - Automatically invalidate all tokens if user fetch fails. (Default is `true`) | ||
* **method** - Set the request to POST or GET. | ||
@@ -97,12 +100,12 @@ #### login | ||
}) | ||
// ... code ... | ||
store.dispatch('auth/logout') // run logout | ||
// ... code ... | ||
store.state.auth.token // get access token | ||
// ... code ... | ||
store.state.auth.user // get user data | ||
// ... code ... | ||
@@ -109,0 +112,0 @@ store.getters['auth/loggedIn'] // get login status (true or false) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17338
129
228
1