Skip to content

Commit 1058170

Browse files
committed
Properly instantiate user returned with auth response
This adds a serverAlias and debug prop to the auth module
1 parent 2f79fab commit 1058170

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/auth-module/auth-module.actions.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ eslint
33
@typescript-eslint/explicit-function-return-type: 0,
44
@typescript-eslint/no-explicit-any: 0
55
*/
6+
import { models } from '../index'
7+
68
export default function makeAuthActions(feathersClient) {
79
return {
810
authenticate(store, data) {
@@ -34,9 +36,19 @@ export default function makeAuthActions(feathersClient) {
3436
.then(payload => {
3537
commit('setPayload', payload)
3638

39+
let user = response[state.responseEntityField]
40+
3741
// If a user was returned in the authenticate response, use that user.
38-
if (response[state.responseEntityField]) {
39-
commit('setUser', response[state.responseEntityField])
42+
if (user) {
43+
if (state.serverAlias && state.userService) {
44+
const Model = Object.keys(models[state.serverAlias])
45+
.map(modelName => models[state.serverAlias][modelName])
46+
.find(model => model.servicePath === state.userService)
47+
if (Model) {
48+
user = new Model(user)
49+
}
50+
}
51+
commit('setUser', user)
4052
// Populate the user if the userService was provided
4153
} else if (
4254
state.userService &&

src/auth-module/auth-module.state.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ eslint
33
@typescript-eslint/explicit-function-return-type: 0,
44
@typescript-eslint/no-explicit-any: 0
55
*/
6-
export default function setupAuthState({ userService }) {
6+
export default function setupAuthState({ userService, serverAlias }) {
77
const state = {
88
accessToken: null, // The JWT
99
payload: null, // The JWT payload
@@ -15,7 +15,8 @@ export default function setupAuthState({ userService }) {
1515

1616
errorOnAuthenticate: null,
1717
errorOnLogout: null,
18-
user: null
18+
user: null,
19+
serverAlias
1920
}
2021
// If a userService string was passed, add a user attribute
2122
if (userService) {

src/auth-module/auth-module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import setupActions from './auth-module.actions'
1111
const defaults = {
1212
namespace: 'auth',
1313
userService: '', // Set this to automatically populate the user (using an additional request) on login success.
14+
serverAlias: null,
15+
debug: false,
1416
state: {}, // for custom state
1517
getters: {}, // for custom getters
1618
mutations: {}, // for custom mutations
@@ -30,6 +32,11 @@ export default function authPluginInit(feathersClient) {
3032
'You must register the @feathersjs/authentication-client plugin before using the feathers-vuex auth module'
3133
)
3234
}
35+
if (options.debug && options.userService && !options.serverAlias) {
36+
console.warn(
37+
'A userService was provided, but no serverAlias was provided. To make sure the user record is an instance of the User model, a serverAlias must be provided.'
38+
)
39+
}
3340

3441
const defaultState = setupState(options)
3542
const defaultGetters = setupGetters()

0 commit comments

Comments
 (0)