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">
import cookie from "cookiejs"
let username: string;
let email: string;
let password: string;
let hasError: boolean;
@@ -9,7 +9,7 @@
const response = await fetch("/api/login", {
method: "POST",
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>
{/if}
<div class="block_4" style="margin-top: 25px;">
<h4 class="heading_3">Benutzername</h4>
<h4 class="heading_3">Email</h4>
<input
type="text"
placeholder="Benutzername"
name="username"
placeholder="Email"
name="email"
class="formInput"
bind:value={username}
bind:value={email}
required
/>
</div>

View File

@@ -1,5 +1,4 @@
<script lang="ts">
let username: string;
let password: string;
let email: string;
let hasError: boolean;
@@ -8,7 +7,7 @@
const response = await fetch("/api/user", {
method: "PUT",
body: JSON.stringify({
username, password, email
password, email
})
})
@@ -26,18 +25,8 @@
<h1>Registrieren:</h1>
<div class="login_page">
{#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}
<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;">
<h4 class="heading_3">Email</h4>
<input

View File

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

View File

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

View File

@@ -12,19 +12,19 @@ import { encodeToken } from "../../lib/JsonWebToken";
export const post: APIRoute = async ({ request }) => {
const body = await request.json();
if (!body.hasOwnProperty("username") || !body.hasOwnProperty("password")) {
return MissingPropertyError(["username", "password"]);
if (!body.hasOwnProperty("email") || !body.hasOwnProperty("password")) {
return MissingPropertyError(["email", "password"]);
}
const user = await User.fromUsername(body.username);
const user = await User.fromEmail(body.email);
if (!user) {
return error(["Invalid username or password."]);
return error(["Invalid email or password."]);
}
// Validate 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();