Registrierung und Login gefixt

This commit is contained in:
Moritz Utcke
2023-04-14 15:30:52 +04:00
parent 24c29214be
commit 452e6aaa28
5 changed files with 16 additions and 30 deletions

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import cookie from "cookiejs" import cookie from "cookiejs"
let username: string; let email: string;
let password: string; let password: string;
let hasError: boolean; let hasError: boolean;
@@ -9,7 +9,7 @@
const response = await fetch("/api/login", { const response = await fetch("/api/login", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
username, password email, password
}) })
}) })
@@ -34,13 +34,13 @@
<p>Das hat leider nicht geklappt, haben sie ihr Passwort und den Nutzernamen richtig eingegeben?</p> <p>Das hat leider nicht geklappt, haben sie ihr Passwort und den Nutzernamen richtig eingegeben?</p>
{/if} {/if}
<div class="block_4" style="margin-top: 25px;"> <div class="block_4" style="margin-top: 25px;">
<h4 class="heading_3">Benutzername</h4> <h4 class="heading_3">Email</h4>
<input <input
type="text" type="text"
placeholder="Benutzername" placeholder="Email"
name="username" name="email"
class="formInput" class="formInput"
bind:value={username} bind:value={email}
required required
/> />
</div> </div>

View File

@@ -1,5 +1,4 @@
<script lang="ts"> <script lang="ts">
let username: string;
let password: string; let password: string;
let email: string; let email: string;
let hasError: boolean; let hasError: boolean;
@@ -8,7 +7,7 @@
const response = await fetch("/api/user", { const response = await fetch("/api/user", {
method: "PUT", method: "PUT",
body: JSON.stringify({ body: JSON.stringify({
username, password, email password, email
}) })
}) })
@@ -26,18 +25,8 @@
<h1>Registrieren:</h1> <h1>Registrieren:</h1>
<div class="login_page"> <div class="login_page">
{#if hasError} {#if hasError}
<p>Das hat leider nicht geklappt, haben sie ihr Passwort und den Nutzernamen richtig eingegeben?</p> <p>Leider ist diese Email bereits vergeben.</p>
{/if} {/if}
<div class="block_4" style="margin-top: 25px;">
<h4 class="heading_3">Benutzername</h4>
<input
type="text"
placeholder="Benutzername"
class="formInput"
bind:value={username}
required
/>
</div>
<div class="block_4" style="margin-top: 25px;"> <div class="block_4" style="margin-top: 25px;">
<h4 class="heading_3">Email</h4> <h4 class="heading_3">Email</h4>
<input <input

View File

@@ -24,12 +24,12 @@ export class User {
return user; return user;
} }
public static async fromUsername(username: string): Promise<UserType | null> { public static async fromEmail(email: string): Promise<UserType | null> {
if (!username || typeof username !== "string") { if (!email || typeof email !== "string") {
return null; return null;
} }
const user = await db<UserType>("users").select("*").where("username", username).first(); const user = await db<UserType>("users").select("*").where("email", email).first();
if (!user) { if (!user) {
return null; return null;
@@ -66,7 +66,6 @@ export class User {
const hashedPassword = hashPassword(user.password); const hashedPassword = hashPassword(user.password);
const result = await db<UserType>("users").insert({ const result = await db<UserType>("users").insert({
username: user.username,
email: user.email, email: user.email,
password: hashedPassword, password: hashedPassword,
uid: uid uid: uid

View File

@@ -1,7 +1,6 @@
import { z } from "zod" import { z } from "zod"
export const UserTypeValidator = z.object({ export const UserTypeValidator = z.object({
username: z.string().min(4).max(64),
id: z.number(), id: z.number(),
uid: z.string().length(36), uid: z.string().length(36),
email: z.string().max(255), email: z.string().max(255),
@@ -9,7 +8,6 @@ export const UserTypeValidator = z.object({
}) })
export const UserRegisterValidator = z.object({ export const UserRegisterValidator = z.object({
username: z.string().min(4).max(64),
email: z.string().max(255), email: z.string().max(255),
password: z.string().min(6), password: z.string().min(6),
}) })

View File

@@ -12,19 +12,19 @@ import { encodeToken } from "../../lib/JsonWebToken";
export const post: APIRoute = async ({ request }) => { export const post: APIRoute = async ({ request }) => {
const body = await request.json(); const body = await request.json();
if (!body.hasOwnProperty("username") || !body.hasOwnProperty("password")) { if (!body.hasOwnProperty("email") || !body.hasOwnProperty("password")) {
return MissingPropertyError(["username", "password"]); return MissingPropertyError(["email", "password"]);
} }
const user = await User.fromUsername(body.username); const user = await User.fromEmail(body.email);
if (!user) { if (!user) {
return error(["Invalid username or password."]); return error(["Invalid email or password."]);
} }
// Validate Password // Validate Password
if (!validatePassword(user.password, body.password)) { if (!validatePassword(user.password, body.password)) {
return error(["Invalid username or password."]); return error(["Invalid email or password."]);
} }
const expiry = moment().add(2, "days").unix(); const expiry = moment().add(2, "days").unix();