Skip to content

Commit

Permalink
Add feathersClient argument on initAuth to set JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
NickBolles committed Nov 14, 2018
1 parent ba71770 commit 61fd4c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const authDefaults = {
}

export const initAuth = function initAuth (options) {
const { commit, req, moduleName, cookieName } = Object.assign({}, authDefaults, options)
const { commit, req, moduleName, cookieName, feathersClient } = Object.assign({}, authDefaults, options)

if (typeof commit !== 'function') {
throw new Error('You must pass the `commit` function in the `initAuth` function options.')
Expand All @@ -55,6 +55,9 @@ export const initAuth = function initAuth (options) {
if (payload) {
commit(`${moduleName}/setAccessToken`, accessToken)
commit(`${moduleName}/setPayload`, payload)
if (feathersClient) {
return feathersClient.passport.setJWT(accessToken).then(() => payload)
}
}
return Promise.resolve(payload)
}
Expand Down
9 changes: 6 additions & 3 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Vue.use(Vuex)
const { service, auth } = feathersNuxt(feathersClient)

describe('Utils', function () {
it('properly populates auth', function (done) {
it('properly populates auth', function () {
const store = new Vuex.Store({
plugins: [
service('todos'),
Expand All @@ -23,7 +23,7 @@ describe('Utils', function () {
cookie: 'feathers-jwt=' + accessToken
}
}
initAuth({
return initAuth({
commit: store.commit,
req,
moduleName: 'auth',
Expand All @@ -32,7 +32,10 @@ describe('Utils', function () {
.then(payload => {
assert(store.state.auth.accessToken === accessToken, 'the token was in place')
assert(store.state.auth.payload, 'the payload was set')
done()
return feathersClient.passport.getJWT()
})
.then((token) => {
assert.isDefined(token, 'the feathers client storage was set')
})
})
})

0 comments on commit 61fd4c7

Please sign in to comment.