1
- "use server" ;
2
1
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" ;
7
4
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 > => {
14
12
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 { } ;
21
20
} 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" } ;
32
25
}
33
- throw error ;
26
+ return { error : "An unexpected error occurred" } ;
34
27
}
35
- } ;
28
+ } ;
0 commit comments