diff --git a/src/db/index.ts b/src/db/index.ts index 1cc95e3f..19b8ed97 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -47,16 +47,23 @@ export function getUserDB(username: string): PouchDB.Database { guestAccount = true; } - function hexEncode(str: string): string { - let hex: string; - let returnStr: string = ''; +function hexEncode(str: string): string { + let hex: string; + let returnStr: string = ''; - for (let i = 0; i < str.length; i++) { - hex = str.charCodeAt(i).toString(16); - returnStr += ('000' + hex).slice(3); - } + for (let i = 0; i < str.length; i++) { + hex = str.charCodeAt(i).toString(16); + returnStr += ('000' + hex).slice(3); + } + + return returnStr; +} - return returnStr; +export function getUserDB(username: string): PouchDB.Database { + let guestAccount: boolean = false; + if (username === GuestUsername) { + username = accomodateGuest(); + guestAccount = true; } const hexName = hexEncode(username); @@ -82,6 +89,19 @@ export function getUserDB(username: string): PouchDB.Database { return ret; } +/** + * Checks the remote couchdb to see if a given username is available + * @param username The username to be checked + */ +export async function usernameIsAvailable(username: string): Promise { + log(`Checking availability of ${username}`); + const req = new XMLHttpRequest(); + const url = remote_couch_url + 'userdb-' + hexEncode(username); + req.open('HEAD', url, false); + req.send(); + return req.status === 404; +} + function updateGuestAccountExpirationDate(guestDB: PouchDB.Database<{}>) { const currentTime = moment(); const expirationDate: string = currentTime.add(2, 'months').toISOString();