feat: user profile page - save info + change pwd
This commit is contained in:
@@ -9,7 +9,7 @@ const moment = require('moment')
|
||||
const graphHelper = require('../../helpers/graph')
|
||||
const request = require('request-promise')
|
||||
const crypto = require('crypto')
|
||||
const nanoid = require('nanoid/non-secure/generate')
|
||||
const nanoid = require('nanoid/non-secure').customAlphabet('1234567890abcdef', 10)
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
@@ -150,7 +150,7 @@ module.exports = {
|
||||
roles = _.concat(roles, ['write:pages', 'manage:pages', 'read:source', 'read:history', 'write:assets', 'manage:assets'])
|
||||
}
|
||||
return {
|
||||
id: nanoid('1234567890abcdef', 10),
|
||||
id: nanoid(),
|
||||
roles: roles,
|
||||
match: r.exact ? 'EXACT' : 'START',
|
||||
deny: r.deny,
|
||||
|
@@ -1,4 +1,5 @@
|
||||
const graphHelper = require('../../helpers/graph')
|
||||
const _ = require('lodash')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
@@ -35,15 +36,16 @@ module.exports = {
|
||||
if (!usr.isActive) {
|
||||
throw new WIKI.Error.AuthAccountBanned()
|
||||
}
|
||||
const usrGroups = await usr.$relatedQuery('groups')
|
||||
return {
|
||||
...usr,
|
||||
password: '',
|
||||
providerKey: '',
|
||||
tfaSecret: '',
|
||||
lastLoginOn: '1970-01-01',
|
||||
groups: usrGroups.map(g => g.name)
|
||||
}
|
||||
|
||||
const providerInfo = _.find(WIKI.data.authentication, ['key', usr.providerKey])
|
||||
|
||||
usr.providerName = _.get(providerInfo, 'title', 'Unknown')
|
||||
usr.lastLoginAt = usr.lastLoginAt || usr.updatedAt
|
||||
usr.password = ''
|
||||
usr.providerId = ''
|
||||
usr.tfaSecret = ''
|
||||
|
||||
return usr
|
||||
}
|
||||
},
|
||||
UserMutation: {
|
||||
@@ -124,11 +126,88 @@ module.exports = {
|
||||
},
|
||||
resetPassword (obj, args) {
|
||||
return false
|
||||
},
|
||||
async updateProfile (obj, args, context) {
|
||||
try {
|
||||
if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
|
||||
throw new WIKI.Error.AuthRequired()
|
||||
}
|
||||
const usr = await WIKI.models.users.query().findById(context.req.user.id)
|
||||
if (!usr.isActive) {
|
||||
throw new WIKI.Error.AuthAccountBanned()
|
||||
}
|
||||
if (!usr.isVerified) {
|
||||
throw new WIKI.Error.AuthAccountNotVerified()
|
||||
}
|
||||
|
||||
await WIKI.models.users.updateUser({
|
||||
id: usr.id,
|
||||
name: _.trim(args.name),
|
||||
jobTitle: _.trim(args.jobTitle),
|
||||
location: _.trim(args.location),
|
||||
timezone: args.timezone
|
||||
})
|
||||
|
||||
const newToken = await WIKI.models.users.refreshToken(usr.id)
|
||||
|
||||
return {
|
||||
responseResult: graphHelper.generateSuccess('User profile updated successfully'),
|
||||
jwt: newToken.token
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
},
|
||||
async changePassword (obj, args, context) {
|
||||
try {
|
||||
if (!context.req.user || context.req.user.id < 1 || context.req.user.id === 2) {
|
||||
throw new WIKI.Error.AuthRequired()
|
||||
}
|
||||
const usr = await WIKI.models.users.query().findById(context.req.user.id)
|
||||
if (!usr.isActive) {
|
||||
throw new WIKI.Error.AuthAccountBanned()
|
||||
}
|
||||
if (!usr.isVerified) {
|
||||
throw new WIKI.Error.AuthAccountNotVerified()
|
||||
}
|
||||
if (usr.providerKey !== 'local') {
|
||||
throw new WIKI.Error.AuthProviderInvalid()
|
||||
}
|
||||
try {
|
||||
await usr.verifyPassword(args.current)
|
||||
} catch (err) {
|
||||
throw new WIKI.Error.AuthPasswordInvalid()
|
||||
}
|
||||
|
||||
await WIKI.models.users.updateUser({
|
||||
id: usr.id,
|
||||
newPassword: args.new
|
||||
})
|
||||
|
||||
const newToken = await WIKI.models.users.refreshToken(usr)
|
||||
|
||||
return {
|
||||
responseResult: graphHelper.generateSuccess('Password changed successfully'),
|
||||
jwt: newToken.token
|
||||
}
|
||||
} catch (err) {
|
||||
return graphHelper.generateError(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
User: {
|
||||
groups(usr) {
|
||||
groups (usr) {
|
||||
return usr.$relatedQuery('groups')
|
||||
}
|
||||
},
|
||||
UserProfile: {
|
||||
async groups (usr) {
|
||||
const usrGroups = await usr.$relatedQuery('groups')
|
||||
return usrGroups.map(g => g.name)
|
||||
},
|
||||
async pagesTotal (usr) {
|
||||
const result = await WIKI.models.pages.query().count('* as total').where('creatorId', usr.id).first()
|
||||
return _.toSafeInteger(result.total)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user