diff --git a/src/components/user/User.tsx b/src/components/user/User.tsx index 8d602a6..5d9c2cc 100644 --- a/src/components/user/User.tsx +++ b/src/components/user/User.tsx @@ -9,6 +9,7 @@ import { adminCredentials } from '../credentials'; import { put } from '~/lib/put'; import { del } from '~/lib/del'; import { isFunction } from 'lodash'; +import { userId } from '~/lib/user-id'; interface Props { values?: () => any; @@ -33,7 +34,7 @@ const User: Component = (props) => { values, context, }); - const id = getValues()?._id ?? `user:${uuid()}`; + const id = getValues()?._id ?? userId(values.username, values.database); await put(id, values, isNew()); const couchUserId = `org.couchdb.user:${values.username}`; const userDoc = { diff --git a/src/lib/user-id.ts b/src/lib/user-id.ts new file mode 100644 index 0000000..6032204 --- /dev/null +++ b/src/lib/user-id.ts @@ -0,0 +1,12 @@ +export const userId = (username: string, database: string) => { + const dbUrl = new URL(database); + return `@${username}>${dbUrl.hostname}`; +}; + +export const parseUserId = (id: string) => { + const matches = id.match(/^@([^>]+)>(.+)$/); + if (!matches) { + return null; + } + return { username: matches[1], hostname: matches[2] }; +};