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

Commit

Permalink
🐛 Fix default username bug
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jun 22, 2019
1 parent 323d3df commit 42cc634
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart",
"version": "1.0.27",
"version": "1.0.28",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down
11 changes: 6 additions & 5 deletions src/crud/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import {
removeReadOnlyValues
} from "../helpers/mysql";
import { Organization } from "../interfaces/tables/organization";
import { capitalizeFirstAndLastLetter, dateToDateTime } from "../helpers/utils";
import {
capitalizeFirstAndLastLetter,
dateToDateTime,
createSlug
} from "../helpers/utils";
import { KeyValue } from "../interfaces/general";
import { cachedQuery, deleteItemFromCache } from "../helpers/cache";
import { CacheCategories, ErrorCode } from "../interfaces/enum";
import { ApiKey } from "../interfaces/tables/user";
import cryptoRandomString from "crypto-random-string";
import { getPaginatedData } from "./data";
import slugify from "slugify";

/*
* Create a new organization for a user
Expand All @@ -22,9 +25,7 @@ export const createOrganization = async (organization: Organization) => {
organization.name = capitalizeFirstAndLastLetter(organization.name);
organization.createdAt = new Date();
organization.updatedAt = organization.createdAt;
organization.username = `${slugify(organization.name, {
lower: true
}).replace(/'|"/g, "")}-${cryptoRandomString({ length: 5, type: "hex" })}`;
organization.username = createSlug(organization.name);
// Create organization
return await query(
`INSERT INTO organizations ${tableValues(organization)}`,
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { User } from "../interfaces/tables/user";
import Joi from "@hapi/joi";
import { getOrganizationIdFromUsername } from "../crud/organization";
import { Request, Response } from "express";
import slugify from "slugify";
import cryptoRandomString = require("crypto-random-string");

/**
* Capitalize each first letter in a string
Expand Down Expand Up @@ -62,6 +64,11 @@ export const organizationUsernameToId = async (id: string) => {
}
};

export const createSlug = (name: string) =>
`${slugify(name, {
lower: true
}).replace(/'|"/g, "")}-${cryptoRandomString({ length: 5, type: "hex" })}`;

export const safeRedirect = (req: Request, res: Response, url: string) => {
if (req.get("X-Requested-With") === "XMLHttpRequest")
return res.json({ redirect: url });
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/tables/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
export interface User {
id?: number;
name: string;
username?: string;
nickname?: string;
primaryEmail?: number;
password?: string;
Expand Down
2 changes: 2 additions & 0 deletions src/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
} from "../config";
import axios from "axios";
import { GitHubEmail } from "../interfaces/oauth";
import { createSlug } from "../helpers/utils";

export const validateRefreshToken = async (token: string, locals: Locals) => {
const data = <User>await verifyToken(token, Tokens.REFRESH);
Expand Down Expand Up @@ -104,6 +105,7 @@ export const register = async (
role?: MembershipRole
) => {
if (email) await checkIfNewEmail(email);
if (!user.username) user.username = createSlug(user.name);
const result = <InsertResult>await createUser(user);
const userId = result.insertId;
// Set email
Expand Down
8 changes: 0 additions & 8 deletions src/rest/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ export const inviteMemberToOrganization = async (
} catch (error) {}
if (isMemberAlready) throw new Error(ErrorCode.USER_IS_MEMBER_ALREADY);
await createMembership({ userId: newUser.id, organizationId, role });
await createNotification({
userId: newUser.id,
category: NotificationCategories.JOINED_ORGANIZATION,
text: `You were invited to the organization **${
(await getOrganization(organizationId)).name
}**`,
link: "/settings/organizations"
});
return;
} else {
await register(
Expand Down

0 comments on commit 42cc634

Please sign in to comment.