1
0
Fork 0
mirror of synced 2024-06-28 11:00:55 +12:00
budibase/packages/server/src/api/controllers/auth.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const { outputProcessing } = require("../../utilities/rowProcessor")
const { InternalTables } = require("../../db/utils")
const { getFullUser } = require("../../utilities/users")
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
2020-04-08 07:34:21 +12:00
2021-05-04 22:32:22 +12:00
exports.fetchSelf = async ctx => {
const appId = ctx.appId
let userId = ctx.user.userId || ctx.user._id
2021-03-11 01:20:07 +13:00
/* istanbul ignore next */
if (!userId) {
ctx.body = {}
return
}
const user = await getFullUser(ctx, userId)
if (appId) {
const db = new CouchDB(appId)
// remove the full roles structure
delete user.roles
try {
const userTable = await db.get(InternalTables.USER_METADATA)
const metadata = await db.get(userId)
// specifically needs to make sure is enriched
ctx.body = await outputProcessing(ctx, userTable, {
...user,
...metadata,
})
} catch (err) {
let response
// user didn't exist in app, don't pretend they do
if (user.roleId === BUILTIN_ROLE_IDS.PUBLIC) {
response = {}
}
// user has a role of some sort, return them
else if (err.status === 404) {
const metadata = {
_id: userId,
}
const dbResp = await db.put(metadata)
user._rev = dbResp.rev
response = user
} else {
response = user
}
ctx.body = response
}
} else {
ctx.body = user
}
}