Skip to content

Commit

Permalink
#32 Add function which checks if username is used
Browse files Browse the repository at this point in the history
  • Loading branch information
NiloCK committed Jan 15, 2019
1 parent 9d3d322 commit 039e309
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<boolean> {
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();
Expand Down

0 comments on commit 039e309

Please sign in to comment.