Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Generate best username of user/org
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Feb 28, 2020
1 parent a286e30 commit 4bbea78
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@staart/manager",
"version": "1.3.26",
"version": "1.3.27",
"main": "index.js",
"repository": "[email protected]:staart/api.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -125,5 +125,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.3.26"
"staart-version": "1.3.27"
}
24 changes: 23 additions & 1 deletion src/crud/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import {
} from "../interfaces/tables/user";
import { decode } from "jsonwebtoken";
import { deleteSensitiveInfoUser } from "../helpers/utils";
import { capitalizeFirstAndLastLetter, anonymizeIpAddress } from "@staart/text";
import {
capitalizeFirstAndLastLetter,
anonymizeIpAddress,
slugify,
createSlug
} from "@staart/text";
import { hash } from "bcryptjs";
import { KeyValue } from "../interfaces/general";
import { NotificationEmails, CacheCategories } from "../interfaces/enum";
Expand Down Expand Up @@ -49,6 +54,23 @@ import ClientOAuth2 from "client-oauth2";
import Axios from "axios";
import { createHash } from "crypto";

export const getBestUsernameForUser = async (name: string) => {
let result: string;
if (name.split(" ")[0].length) {
result = slugify(name.split(" ")[0]);
if (checkUsernameAvailability(result)) return result;
}
result = slugify(name);
if (checkUsernameAvailability(result)) return result;

let available = false;
while (!available) {
result = createSlug(name);
if (checkUsernameAvailability(result)) available = true;
}
return result;
};

/**
* Get a list of all ${tableName("users")}
*/
Expand Down
7 changes: 4 additions & 3 deletions src/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getUserBackupCode,
updateBackupCode,
checkUsernameAvailability,
deleteSessionByJwt
deleteSessionByJwt,
getBestUsernameForUser
} from "../crud/user";
import { InsertResult } from "../interfaces/mysql";
import {
Expand Down Expand Up @@ -125,9 +126,9 @@ export const register = async (
await checkIfNewEmail(email);
if (!ALLOW_DISPOSABLE_EMAILS) checkIfDisposableEmail(email);
}
if (!user.username) user.username = createSlug(user.name);
if (!(await checkUsernameAvailability(user.username)))
if (user.username && !(await checkUsernameAvailability(user.username)))
throw new Error(USERNAME_EXISTS);
user.username = user.username || (await getBestUsernameForUser(user.name));
const result = <InsertResult>await createUser(user);
const userId = result.insertId;
// Set email
Expand Down

0 comments on commit 4bbea78

Please sign in to comment.