Skip to content

Commit

Permalink
Debug login form #4
Browse files Browse the repository at this point in the history
  • Loading branch information
adb-sh committed Sep 14, 2024
1 parent 7c91319 commit 18f89bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/components/LoginForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useNavigate, useSubmission } from "@solidjs/router";
import { createSignal, Show } from "solid-js";
import { login, register } from "~/lib";
import { ExclamationCircleIcon } from "@deploy-cat/heroicons-solid/24/solid/esm";

export const LoginForm = () => {
const loginStatus = useSubmission(login);
Expand Down Expand Up @@ -73,7 +74,16 @@ export const LoginForm = () => {
Lost Password?
</a>
</div> */}
<Show when={loginStatus.error}>
<div role="alert" class="alert alert-error">
<ExclamationCircleIcon class="w-6 h-6" />
<span>{loginStatus.error.toString()}</span>
</div>
</Show>
<button type="submit" class="btn btn-primary w-full">
<Show when={loginStatus.pending}>
<span class="loading loading-spinner"></span>
</Show>
Login to your account
</button>
<div class="text-sm font-medium text-gray-500 dark:text-gray-300">
Expand Down Expand Up @@ -139,7 +149,16 @@ export const LoginForm = () => {
required
/>
</div>
<Show when={registerStatus.error}>
<div role="alert" class="alert alert-error">
<ExclamationCircleIcon class="w-6 h-6" />
<span>{registerStatus.error.toString()}</span>
</div>
</Show>
<button type="submit" class="btn btn-primary w-full">
<Show when={registerStatus.pending}>
<span class="loading loading-spinner"></span>
</Show>
Create Account
</button>
<div class="text-sm font-medium text-gray-500 dark:text-gray-300">
Expand Down
28 changes: 11 additions & 17 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ function validatePassword(password: unknown) {

async function login(username: string, password: string) {
const user = await db.user.findUnique({ where: { username } });
if (!user || password !== user.password) throw new Error("Invalid login");
if (!user) throw new Error("User does not exist");
if (password !== user.password) throw new Error("Wrong password");
return user;
}

async function register(username: string, password: string) {
const existingUser = await db.user.findUnique({ where: { username } });
if (existingUser) throw new Error("User already exists");
let existingNamespace;
try {
await k8sCore.readNamespace(username);
throw new Error("Namespace already exists!");
existingNamespace = await k8sCore.readNamespace(username);
} catch (e) {}
if (existingNamespace) throw new Error("Namespace already exists!");

await k8sCore.createNamespace({
apiVersion: "v1",
Expand Down Expand Up @@ -57,27 +59,19 @@ export const loginFromForm = async (formData: FormData) => {
const username = String(formData.get("username"));
const password = String(formData.get("password"));

try {
const user = await login(username, password);
const session = await getSession();
await session.update((d) => (d.userId = user!.id));
} catch (err) {
return err as Error;
}
const user = await login(username, password);
const session = await getSession();
await session.update((d) => (d.userId = user!.id));
throw redirect("/cloud");
};

export const registerFromForm = async (formData: FormData) => {
const username = String(formData.get("username"));
const password = String(formData.get("password"));

try {
const user = await register(username, password);
const session = await getSession();
await session.update((d) => (d.userId = user!.id));
} catch (err) {
return err as Error;
}
const user = await register(username, password);
const session = await getSession();
await session.update((d) => (d.userId = user!.id));
throw redirect("/cloud");
};

Expand Down

0 comments on commit 18f89bc

Please sign in to comment.