1
0
Fork 0
mirror of synced 2024-06-14 08:24:48 +12:00

Getting api key/dev info docs setup and in use.

This commit is contained in:
mike12345567 2022-02-10 19:06:49 +00:00
parent 788cb19fa8
commit dff24e402d
5 changed files with 49 additions and 11 deletions

View file

@ -14,6 +14,7 @@ exports.DocumentTypes = {
APP_METADATA: `${PRE_APP}${exports.SEPARATOR}metadata`,
ROLE: "role",
MIGRATIONS: "migrations",
DEV_INFO: "devinfo",
}
exports.StaticDatabases = {

View file

@ -336,6 +336,14 @@ const getConfigParams = ({ type, workspace, user }, otherProps = {}) => {
}
}
/**
* Generates a new dev info document ID - this is scoped to a user.
* @returns {string} The new dev info ID which info for dev (like api key) can be stored under.
*/
const generateDevInfoID = userId => {
return `${DocumentTypes.DEV_INFO}${SEPARATOR}${userId}`
}
/**
* Returns the most granular configuration document from the DB based on the type, workspace and userID passed.
* @param {Object} db - db instance to query
@ -451,3 +459,4 @@ exports.generateConfigID = generateConfigID
exports.getConfigParams = getConfigParams
exports.getScopedFullConfig = getScopedFullConfig
exports.generateNewUsageQuotaDoc = generateNewUsageQuotaDoc
exports.generateDevInfoID = generateDevInfoID

View file

@ -7,11 +7,13 @@
async function generateAPIKey() {
try {
await auth.generateAPIKey()
apiKey = await auth.generateAPIKey()
notifications.success("New API key generated.")
} catch (err) {
notifications.error("Unable to generate new API key")
}
// need to return false to keep modal open
return false
}
onMount(async () => {

View file

@ -173,15 +173,12 @@ export function createAuthStore() {
})
},
generateAPIKey: async () => {
return API.generateAPIKey()
const info = await API.generateAPIKey()
return info?.apiKey ? info.apiKey : null
},
fetchAPIKey: async () => {
const info = await API.fetchDeveloperInfo()
if (info?.apiKey) {
return info.apiKey
} else {
return null
}
return info?.apiKey ? info.apiKey : null
},
}

View file

@ -1,11 +1,40 @@
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
const { generateDevInfoID } = require("@budibase/backend-core/db")
const { newid } = require("@budibase/backend-core/utils")
function cleanupDevInfo(info) {
// user doesn't need to aware of dev doc info
delete info._id
delete info._rev
return info
}
exports.generateAPIKey = async ctx => {
ctx.body = {
apiKey: "a175402a-89fc-11ec-a8a3-0242ac120002",
const db = getGlobalDB()
const id = generateDevInfoID(ctx.user._id)
let devInfo
try {
devInfo = await db.get(id)
} catch (err) {
devInfo = { _id: id }
}
devInfo.apiKey = newid()
await db.put(devInfo)
ctx.body = cleanupDevInfo(devInfo)
}
exports.fetchAPIKey = async ctx => {
ctx.body = {
apiKey: "a175402a-89fc-11ec-a8a3-0242ac120002",
const db = getGlobalDB()
const id = generateDevInfoID(ctx.user._id)
let devInfo
try {
devInfo = await db.get(id)
} catch (err) {
devInfo = {
_id: id,
apiKey: newid(),
}
await db.put(devInfo)
}
ctx.body = cleanupDevInfo(devInfo)
}