Skip to content

Commit ec6fd1a

Browse files
committed
fixed user problem
1 parent 65b6fcd commit ec6fd1a

5 files changed

Lines changed: 42 additions & 16 deletions

File tree

src/components/auth/auth-gate.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { $, component$, Slot, useSignal, useStore, useVisibleTask$ } from "@buil
33
import { brand } from "~/brand";
44
import BrandLogo from "~/components/furniture/brand-logo";
55
import { translate, useI18n, type TranslationKey } from "~/i18n";
6+
import { sanitizeUsername } from "~/lib/account";
67
import {
78
getStoredUser,
89
saveStoredUser,
@@ -31,7 +32,7 @@ export default component$(() => {
3132
});
3233

3334
const submit = $(async () => {
34-
const username = form.username.trim();
35+
const username = sanitizeUsername(form.username);
3536
const password = form.password;
3637
const name = form.name.trim() || username;
3738

@@ -57,9 +58,12 @@ export default component$(() => {
5758
}).catch(() => null);
5859

5960
if (!response?.ok) {
60-
form.error = mode.value === "register"
61-
? translate(language.value, "auth.registerError")
62-
: translate(language.value, "auth.loginError");
61+
const result = await response?.json().catch(() => null);
62+
form.error = mode.value === "register" && result?.error === "INVALID_INPUT"
63+
? translate(language.value, "auth.usernameError")
64+
: mode.value === "register"
65+
? translate(language.value, "auth.registerError")
66+
: translate(language.value, "auth.loginError");
6367
return;
6468
}
6569

src/lib/account.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const sanitizeUsername = (username: string | null | undefined) =>
2+
(username || "")
3+
.trim()
4+
.replace(/[ıİ]/g, (character) => (character === "ı" ? "i" : "I"))
5+
.normalize("NFKD")
6+
.replace(/[\u0300-\u036f]/g, "")
7+
.toLowerCase()
8+
.replace(/[^a-z0-9_-]/g, "")
9+
.slice(0, 32);

src/lib/server/settings-store.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { pbkdf2, randomBytes, timingSafeEqual } from 'node:crypto';
33
import { dirname, resolve } from 'node:path';
44
import { promisify } from 'node:util';
55

6+
import { sanitizeUsername } from '~/lib/account';
67
import type { LockstepUser } from '~/lib/user-session';
78

89
export type SettingsStore = Record<string, unknown>;
@@ -60,13 +61,6 @@ const makeInitials = (name: string) => {
6061
.slice(0, 2);
6162
};
6263

63-
export const sanitizeUsername = (username: string | null | undefined) =>
64-
(username || '')
65-
.trim()
66-
.toLowerCase()
67-
.replace(/[^a-z0-9_-]/g, '')
68-
.slice(0, 32);
69-
7064
const makeUserProfile = (
7165
username: string,
7266
name: string,

src/routes/api/auth/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { RequestHandler } from '@builder.io/qwik-city';
22

3+
import { sanitizeUsername } from '~/lib/account';
34
import {
45
authenticateUser,
56
registerUser,
6-
sanitizeUsername,
77
updateUserAccount,
88
} from '~/lib/server/settings-store';
99

@@ -44,9 +44,21 @@ export const onPost: RequestHandler = async ({ cookie, headers, json, request })
4444

4545
setUserCookie(cookie, user.id);
4646
json(200, { user });
47-
} catch {
48-
json(action === 'register' ? 409 : 401, {
49-
error: action === 'register' ? 'USER_EXISTS' : 'INVALID_CREDENTIALS',
47+
} catch (error) {
48+
const message = error instanceof Error ? error.message : '';
49+
const registerStatus = message === 'User already exists'
50+
? 409
51+
: message === 'Invalid credentials'
52+
? 400
53+
: 500;
54+
const registerError = message === 'User already exists'
55+
? 'USER_EXISTS'
56+
: message === 'Invalid credentials'
57+
? 'INVALID_INPUT'
58+
: 'REGISTER_FAILED';
59+
60+
json(action === 'register' ? registerStatus : 401, {
61+
error: action === 'register' ? registerError : 'INVALID_CREDENTIALS',
5062
});
5163
}
5264
};

src/routes/profile/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { brand } from "~/brand";
55
import Icon from "~/components/core/icon";
66
import { useLocalStorage } from "~/hooks/useLocalStorage";
77
import { translate, useI18n } from "~/i18n";
8+
import { sanitizeUsername } from "~/lib/account";
89
import {
910
getSettingsHeaders,
1011
getStoredUser,
@@ -89,12 +90,18 @@ export default component$(() => {
8990
const updateAccount = $(async () => {
9091
accountForm.error = "";
9192
accountForm.success = "";
93+
const username = sanitizeUsername(accountForm.username);
9294

9395
if (!accountForm.currentPassword || accountForm.currentPassword.length < 6) {
9496
accountForm.error = translate(language.value, "userProfile.currentPasswordError");
9597
return;
9698
}
9799

100+
if (username.length < 3) {
101+
accountForm.error = translate(language.value, "auth.usernameError");
102+
return;
103+
}
104+
98105
if (accountForm.newPassword && accountForm.newPassword.length < 6) {
99106
accountForm.error = translate(language.value, "auth.passwordError");
100107
return;
@@ -108,7 +115,7 @@ export default component$(() => {
108115
currentPassword: accountForm.currentPassword,
109116
name: accountForm.name,
110117
newPassword: accountForm.newPassword || undefined,
111-
username: accountForm.username,
118+
username,
112119
}),
113120
}).catch(() => null);
114121

0 commit comments

Comments
 (0)