Skip to content

Commit 8e3c06c

Browse files
committed
fix: revert back to api for login
1 parent d528409 commit 8e3c06c

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

actions/login.ts

+22-29
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
1-
"use server";
21
import { loginSchema } from "@/schemas";
3-
import * as z from "zod";
4-
import { signIn } from "@/auth";
5-
import { DEFAULT_LOGIN_REDIRECT } from "@/routes";
6-
import { AuthError } from "next-auth";
2+
import { z } from "zod";
3+
import ky, { HTTPError } from "ky";
74

8-
export const login = async (values: z.infer<typeof loginSchema>) => {
9-
const validatedFields = loginSchema.safeParse(values);
10-
if (!validatedFields.success) {
11-
return { error: "invalid fields" };
12-
}
13-
const { email, password } = validatedFields.data;
5+
interface ErrorMessage {
6+
error?: string;
7+
}
8+
9+
export const login = async (
10+
values: z.infer<typeof loginSchema>,
11+
): Promise<ErrorMessage> => {
1412
try {
15-
const x = await signIn("credentials", {
16-
email,
17-
password,
18-
redirectTo: DEFAULT_LOGIN_REDIRECT,
19-
});
20-
console.log("x", x);
13+
const data = await ky
14+
.post("/api/auth/login", {
15+
json: values,
16+
})
17+
.json();
18+
19+
return {};
2120
} catch (error) {
22-
if (error instanceof AuthError) {
23-
switch (error.type) {
24-
case "CredentialsSignin": {
25-
return { error: "Invalid credentials" };
26-
}
27-
default:
28-
return {
29-
error: "something went wrong",
30-
};
31-
}
21+
if (error instanceof HTTPError) {
22+
// ky throws HTTPError for non-2xx responses
23+
const errorData = await error.response.json();
24+
return { error: errorData.error || "An error occurred" };
3225
}
33-
throw error;
26+
return { error: "An unexpected error occurred" };
3427
}
35-
};
28+
};

components/forms/user-auth-form.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,11 @@ const UserAuthForm: React.FC = (): ReactNode => {
207207
login(values).then((data) => {
208208
if (data && data.error) {
209209
setError(data.error);
210+
setLoading(false);
211+
return;
210212
}
213+
window.location.href = DEFAULT_LOGIN_REDIRECT;
211214
setLoading(false);
212-
// window.location.href = DEFAULT_LOGIN_REDIRECT;
213215
});
214216
};
215217

0 commit comments

Comments
 (0)