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

Commit

Permalink
✨ Add Hash IDs utils
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 31, 2019
1 parent 7b8f591 commit 0ffab3e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ FRONTEND_URL = "http://localhost:3000" # URL for Staart UI
JWT_SECRET = "staart"
JWT_ISSUER = "staart"
SERVICE_2FA = "staart"
HASH_IDS = "staart"

# MySQL/MariaDB connection
DB_HOST = "localhost"
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.9",
"version": "1.1.10",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -114,6 +114,7 @@
"fs-extra": "^8.1.0",
"geolite2": "^1.2.1",
"handy-redis": "^1.6.2",
"hashids": "^2.0.0",
"helmet": "^3.20.1",
"http-aws-es": "^6.0.0",
"http-status-codes": "^1.3.2",
Expand Down Expand Up @@ -146,5 +147,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.9"
"staart-version": "1.1.10"
}
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const ALLOW_DISPOSABLE_EMAILS = !!process.env.DISPOSABLE_EMAIL;
export const JWT_SECRET = process.env.JWT_SECRET || "staart";
export const JWT_ISSUER = process.env.JWT_ISSUER || "staart";
export const SERVICE_2FA = process.env.SERVICE_2FA || "staart";
export const HASH_IDS = process.env.HASH_IDS || "staart";

export const TOKEN_EXPIRY_EMAIL_VERIFICATION =
process.env.TOKEN_EXPIRY_EMAIL_VERIFICATION || "7d";
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ import cryptoRandomString from "crypto-random-string";
import { Tokens } from "../interfaces/enum";
import { ApiKeyResponse } from "./jwt";
import { isMatch } from "matcher";
import Hashids from "hashids";
import { getUserIdFromUsername } from "../crud/user";
import { HASH_IDS } from "../config";

const hashIds = new Hashids(
HASH_IDS,
10,
"abcdefghijklmnopqrstuvwxyz1234567890"
);

/**
* Capitalize each first letter in a string
Expand Down Expand Up @@ -79,6 +87,21 @@ export const userUsernameToId = async (id: string, tokenUserId: number) => {
}
};

export const generateHashId = (id: number) => hashIds.encode(id);

export const hashIdToId = (id: string | number) => {
if (typeof id === "number") return id;
if (id.startsWith("h")) {
const numberId = parseInt(hashIds.decode(id).join(""));
if (isNaN(numberId)) {
return parseInt(id);
} else {
return numberId;
}
}
return parseInt(id);
};

export const localsToTokenOrKey = (res: Response) => {
if (res.locals.token.sub == Tokens.API_KEY) {
return res.locals.token as ApiKeyResponse;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3499,6 +3499,11 @@ has@^1.0.1, has@^1.0.3:
dependencies:
function-bind "^1.1.1"

hashids@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/hashids/-/hashids-2.0.0.tgz#8a3ad77598233e399594b73ad5fdb3c2c880528e"
integrity sha512-URg7dQlk6wQS9WqhQwKBC0cT/G0JMaEcwlHTvtRNB45JcIrr6mxY7dXiCUJwESf+tXnyaFdz7C8ETDYrQsyY1A==

[email protected]:
version "0.4.0"
resolved "https://registry.yarnpkg.com/helmet-crossdomain/-/helmet-crossdomain-0.4.0.tgz#5f1fe5a836d0325f1da0a78eaa5fd8429078894e"
Expand Down

0 comments on commit 0ffab3e

Please sign in to comment.