-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RSN-72] - Login/register logic (#69)
* feat: add login and register logic to components TODO: Add same logic to organizer registration. * feat: changed register page styling, added error handling * chore: linting --------- Co-authored-by: raczu <[email protected]>
- Loading branch information
Showing
20 changed files
with
891 additions
and
460 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
REASN_API_URL=https://localhost:5272 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use server"; | ||
|
||
import { LoginRequestSchema } from "@reasn/common/src/schemas/LoginRequest"; | ||
import { login } from "@/services/auth"; | ||
import { setToken } from "@/lib/token"; | ||
import { redirect } from "next/navigation"; | ||
import { | ||
formatZodError, | ||
handleErrorMessage, | ||
} from "@reasn/common/src/helpers/errorHelpers"; | ||
|
||
export type LoginFormState = { | ||
message?: string | null; | ||
errors?: string | {}; | ||
}; | ||
|
||
export const loginAction = async ( | ||
prevState: any, | ||
formData: FormData, | ||
): Promise<LoginFormState | undefined> => { | ||
const result = LoginRequestSchema.safeParse({ | ||
email: formData.get("email"), | ||
password: formData.get("password"), | ||
}); | ||
|
||
if (!result.success) { | ||
return { | ||
errors: formatZodError(result.error), | ||
message: "Niepoprawne wartości formularza.", | ||
}; | ||
} | ||
|
||
try { | ||
const payload = await login(result.data); | ||
setToken(payload); | ||
redirect("/"); | ||
} catch (e) { | ||
return handleErrorMessage(e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use server"; | ||
|
||
import { | ||
RegisterRequestSchema, | ||
RegisterRequestMapper, | ||
} from "@reasn/common/src/schemas/RegisterRequest"; | ||
import { register } from "@/services/auth"; | ||
import { redirect } from "next/navigation"; | ||
import { z } from "zod"; | ||
import { | ||
formatZodError, | ||
handleErrorMessage, | ||
} from "@reasn/common/src/helpers/errorHelpers"; | ||
|
||
export type RegisterFormState = { | ||
message?: string | null; | ||
errors?: string | {}; | ||
}; | ||
|
||
const RegisterRequestExtendedSchema = RegisterRequestSchema.extend({ | ||
confirmPassword: z.string(), | ||
}).refine((data) => data.password === data.confirmPassword); | ||
|
||
export type RegisterFormData = z.infer<typeof RegisterRequestExtendedSchema>; | ||
|
||
export const registerAction = async ( | ||
prevState: any, | ||
formData: FormData, | ||
): Promise<RegisterFormState | undefined> => { | ||
const result = RegisterRequestExtendedSchema.safeParse({ | ||
name: formData.get("name"), | ||
surname: formData.get("surname"), | ||
email: formData.get("email"), | ||
username: formData.get("username"), | ||
password: formData.get("password"), | ||
confirmPassword: formData.get("confirmPassword"), | ||
phone: formData.get("phone"), | ||
address: { | ||
country: formData.get("country"), | ||
city: formData.get("city"), | ||
street: formData.get("street"), | ||
state: formData.get("state"), | ||
zipCode: formData.get("postcode"), | ||
}, | ||
role: formData.get("role"), | ||
}); | ||
|
||
if (!result.success) { | ||
console.log(result.error); | ||
return { | ||
errors: formatZodError(result.error), | ||
message: "Niepoprawne wartości formularza.", | ||
}; | ||
} | ||
|
||
try { | ||
await register(RegisterRequestMapper.fromObject(result.data)); | ||
redirect("/login"); | ||
} catch (e) { | ||
return handleErrorMessage(e); | ||
} | ||
}; |
Oops, something went wrong.