Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
fix: remove username, fix user creation error in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
radioegor146 committed Sep 5, 2024
1 parent 6a21903 commit bd0413c
Show file tree
Hide file tree
Showing 18 changed files with 536 additions and 571 deletions.
11 changes: 2 additions & 9 deletions bin/create-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ import { create } from "../src/lib/member";
dotenv.config();

(async () => {
const { name, username, email } = await inquirer.prompt([
const { name, email } = await inquirer.prompt([
{
type: "input",
name: "name",
message: "Name:",
validate: (input) => input.length > 0,
},
{
type: "input",
name: "username",
message: "Username:",
validate: (input) => input.length > 0,
},
{
type: "input",
name: "email",
Expand All @@ -36,12 +30,11 @@ dotenv.config();
const password = (await randomBytes(8)).toString("hex");
const member = await create({
name,
username,
email,
status: MemberStatuses.ACTIVE,
password,
});

console.log(`Created ${name} (${username}) ${member}`);
console.log(`Created ${name} ${member}`);
console.log("Password:", password);
})();
8 changes: 8 additions & 0 deletions prisma/migrations/20240905182619_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- You are about to drop the column `deleted` on the `SpaceTransaction` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "SpaceTransaction" DROP COLUMN "deleted";
11 changes: 11 additions & 0 deletions prisma/migrations/20240905190329_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- You are about to drop the column `username` on the `Member` table. All the data in the column will be lost.
*/
-- DropIndex
DROP INDEX "Member_username_key";

-- AlterTable
ALTER TABLE "Member" DROP COLUMN "username";
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ model Member {
id String @id @default(uuid())
name String
email String @unique
username String @unique
status MemberStatuses @default(ACTIVE)
joinedAt DateTime @default(now())
Expand Down
2 changes: 1 addition & 1 deletion src/app/(protected)/members/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function MemberPage(props: { params: { id: string } }) {
const membershipHistory = await fetchMemberHistory(member.id);
const acsKeys = await fetchMemberKeys(member.id);
const balanceData = await fetchBalance(member.id);
console.log('balanceData', balanceData);

return (
<>
<div className="flex flex-col gap-8">
Expand Down
18 changes: 1 addition & 17 deletions src/app/(protected)/members/_components/CreateMember.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Spinner from "@/shared/Spinner";
export type FormValues = {
email: string;
name: string;
username: string;
};

export type CreateMemberModalProps = {
Expand Down Expand Up @@ -46,7 +45,7 @@ const CreateMemberModal: React.FC<CreateMemberModalProps> = ({ onClose }) => {
</header>
<form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col gap-4">
{error && (
{error !== undefined && (
<div
className="p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:text-red-400"
role="alert"
Expand All @@ -70,21 +69,6 @@ const CreateMemberModal: React.FC<CreateMemberModalProps> = ({ onClose }) => {
})}
/>
</div>
<div className="flex flex-col gap-1">
<span className="font-semibold text-gray-600">Username</span>
<input
type="text"
className={classNames(
"w-full rounded ring-1 ring-gray-200 p-3",
{ "ring-red-500 text-red-600": !!errors.username }
)}
placeholder="mary_doe"
{...register("username", {
pattern: /^[a-zA-Z]+[a-zA-Z0-9_]*$/u,
required: true,
})}
/>
</div>
<div className="flex flex-col gap-1">
<span className="font-semibold text-gray-600">Email</span>
<input
Expand Down
5 changes: 0 additions & 5 deletions src/app/(protected)/members/_components/MembersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type MembersTableProps = {
members: {
id: string;
name: string;
username: string;
status: MemberStatuses;
}[];
title: string;
Expand Down Expand Up @@ -41,9 +40,6 @@ const MembersTable: React.FC<MembersTableProps> = ({
<th scope="col" className="px-6 py-3">
Name
</th>
<th scope="col" className="px-6 py-3">
Username
</th>
<th scope="col" className="px-6 py-3">
<span className="sr-only">Actions</span>
</th>
Expand All @@ -66,7 +62,6 @@ const MembersTable: React.FC<MembersTableProps> = ({
>
{member.name}
</th>
<td className="px-6 py-4">{member.username}</td>
<td className="px-6 py-4 text-right">
<Link
href={`/members/${member.id}`}
Expand Down
1 change: 0 additions & 1 deletion src/app/api/tg-accounting/residents/debt/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { PrismaClient } from '@prisma/client'
import authorizedOnlyRequest from "@/lib/auth/telegram-bot-api";

type DebtData = {
username: string,
id: string,
debt: number
};
Expand Down
2 changes: 0 additions & 2 deletions src/app/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ const nextAuth = NextAuth({
provider: token.externalProvider as string,
};
session.user.id = token.sub as string;
session.user.username = token.username as string | undefined;
session.user.image = token.picture;
return session;
},
async jwt({ token, user, profile, account }) {
if (profile && account) {
token.username = user.name;
token.picture = user.image;
token.externalId = profile.sub;
token.externalProvider = account.provider;
Expand Down
3 changes: 1 addition & 2 deletions src/data/members/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import { Record, Static, String } from "runtypes";

const CreateMemberRequest = Record({
email: String,
name: String,
username: String,
name: String
});

export type CreateMemberRequest = Static<typeof CreateMemberRequest>;
Expand Down
2 changes: 0 additions & 2 deletions src/data/members/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MemberStatuses } from "@prisma/client";
export type GetAllMembersDTO = {
id: string;
name: string;
username: string;
status: MemberStatuses;
}[];

Expand All @@ -13,7 +12,6 @@ export async function getAll(): Promise<GetAllMembersDTO> {
return members.map((m) => ({
id: m.id,
name: m.name,
username: m.username,
status: m.status,
}));
}
4 changes: 1 addition & 3 deletions src/lib/auth/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export type Profile = {
id: string;
name: string;
email: string;
username: string;
image?: string;
};

Expand All @@ -16,12 +15,11 @@ export function memberToProfile(
member: Member,
ssoProfile?: SSOProfile,
): Profile {
const { id, email, name, username } = member;
const { id, email, name } = member;
return {
id,
email,
name,
username,
image: ssoProfile?.image,
};
}
1 change: 0 additions & 1 deletion src/lib/integrations/logto/account-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export default class LogtoAccountManagement
const api = await this.getClient();
const response = await api.post("/users", {
name: props.name,
username: props.username,
password: props.password,
primaryEmail: props.email,
});
Expand Down
1 change: 0 additions & 1 deletion src/lib/integrations/logto/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ type LogtoProfile = {
sub: string;
name: string | null;
picture: string | null;
username: string;
email: string;
email_verified: boolean;
at_hash: string;
Expand Down
10 changes: 1 addition & 9 deletions src/lib/member/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MemberStatuses, PrismaClient } from "@prisma/client";
import { AccountManagement } from "../auth/provider";
import { isEmail, isName, isUsername } from "../validation";
import { isEmail, isName } from "../validation";
import prisma from "../db";

export type AccountCreateDTO = {
name: string;
username: string;
email: string;
status?: MemberStatuses;
password?: string;
Expand Down Expand Up @@ -48,7 +47,6 @@ export async function isExistsById(memberId: string): Promise<boolean> {
*/
export async function create({
name,
username,
email,
status,
password,
Expand All @@ -63,15 +61,10 @@ export async function create({
throw new Error("Incorrect email");
}

if (!isUsername(username)) {
throw new Error("Incorrect username");
}

const { member, externalAccount } = await prisma.$transaction(async (tx) => {
const member = await tx.member.create({
data: {
name,
username,
email,
status,
},
Expand All @@ -80,7 +73,6 @@ export async function create({
const externalAccount = await accountManagement.createAccount({
name,
email,
username,
password,
active: status ? status === "ACTIVE" : true,
});
Expand Down
6 changes: 0 additions & 6 deletions src/lib/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ const isName = isGenericEntityName;

const isEmail = (email: string) => email.match(/.+@.+\..+/);

const isUsername = (username: string) =>
username.match(/^[a-zA-Z]+[a-zA-Z0-9_]*$/u) &&
username.length >= 3 &&
username.length <= 64;

const isSubscriptionTitle = (title: string) =>
title.length >= 0 && title.match(/^.*$/u);

Expand Down Expand Up @@ -47,7 +42,6 @@ const isACSKeyName = isGenericEntityName;

export {
isName,
isUsername,
isEmail,
isSubscriptionTitle,
isTransactionDescription,
Expand Down
1 change: 0 additions & 1 deletion src/types/interfaces/account-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export interface AccountDTO {
export interface AccountCreateDTO {
email: string;
name: string;
username: string;
active?: boolean;
password?: string;
}
Expand Down
Loading

0 comments on commit bd0413c

Please sign in to comment.