Skip to content
This repository was archived by the owner on Jun 28, 2025. It is now read-only.

Commit 95dca49

Browse files
committed
feat(web): add registration, sign up function
1 parent 5f2971a commit 95dca49

File tree

5 files changed

+84
-28
lines changed

5 files changed

+84
-28
lines changed

web/src/app/api/auth/[...nextauth]/route.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
import CredentialsProvider from "next-auth/providers/credentials";
22
import NextAuth from "next-auth/next";
33
import { signIn } from "@/lib/strapi/auth";
4-
5-
type StrapiResponse = {
6-
jwt: string;
7-
user: {
8-
id: number;
9-
username: string;
10-
email: string;
11-
provider: string;
12-
confirmed: boolean;
13-
blocked: boolean;
14-
createdAt: string;
15-
updatedAt: string;
16-
};
17-
error?: {
18-
details: string[];
19-
};
20-
};
4+
import { StrapiSignInResponse } from "@/lib/types";
215

226
const authOptions = {
237
secret: process.env.NEXTAUTH_SECRET,
@@ -29,10 +13,11 @@ const authOptions = {
2913
password: { label: "Password", type: "password" },
3014
},
3115
async authorize(credentials) {
16+
console.log("heee");
3217
try {
3318
if (credentials?.email == null || credentials.password == null) return null;
3419

35-
const strapiResponse: StrapiResponse = await signIn(credentials.email, credentials.password);
20+
const strapiResponse: StrapiSignInResponse = await signIn(credentials.email, credentials.password);
3621

3722
if (strapiResponse.error) {
3823
return null;
@@ -44,7 +29,8 @@ const authOptions = {
4429
email: strapiResponse.user.email,
4530
name: strapiResponse.user.username,
4631
};
47-
} catch {
32+
} catch (e) {
33+
console.log(e);
4834
return null;
4935
}
5036
},

web/src/app/registration/page.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use client";
2-
import { ChangeEvent, useState } from "react";
2+
import { ChangeEvent, FormEvent, useState } from "react";
33
import { getMailValidation } from "@/helpers/validators";
44
import Input from "@/components/Inputs/TextInput";
5+
import { signUp, signUpApi } from "@/lib/strapi/auth";
6+
import { signIn, useSession } from "next-auth/react";
57

68
type RegistrationFormData = {
79
username: string;
@@ -13,6 +15,7 @@ type RegistrationFormData = {
1315
};
1416

1517
export default function Registration() {
18+
const { data: session } = useSession();
1619
const [formData, setFormData] = useState<RegistrationFormData>({
1720
username: "",
1821
firstname: "",
@@ -22,6 +25,8 @@ export default function Registration() {
2225
passwordConfirm: "",
2326
});
2427

28+
const [errorMessage, setErrorMessage] = useState("");
29+
2530
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
2631
const { name, value } = e.target;
2732
setFormData(prevFormData => ({ ...prevFormData, [name]: value }));
@@ -36,12 +41,28 @@ export default function Registration() {
3641
<div>
3742
<h1 className="h3">My Account</h1>
3843
<div className="grid grid-cols-1 xl:grid-cols-2 xl:gap-8">
39-
<form className="flex flex-col items-stretch gap-4">
44+
<form
45+
className="flex flex-col items-stretch gap-4"
46+
onSubmit={async (e: FormEvent<HTMLFormElement>) => {
47+
e.preventDefault();
48+
console.log("submit");
49+
console.log("err");
50+
const registerresp = await signUp(formData.username, formData.email, formData.password);
51+
if (registerresp.error) {
52+
console.log(registerresp.error.message);
53+
setErrorMessage(registerresp.error.message);
54+
return;
55+
}
56+
console.log(registerresp);
57+
const res = await signIn("credentials", { email: formData.email, password: formData.password, redirect: false });
58+
console.log(res);
59+
}}
60+
>
4061
<Input type="text" label="Username" name="username" id="username" value={formData.username} onChange={handleInputChange} required />
41-
<div className="grid grid-cols-1 xl:grid-cols-2 xl:gap-3">
62+
{/* <div className="grid grid-cols-1 xl:grid-cols-2 xl:gap-3">
4263
<Input type="text" label="First Name" name="firstname" id="firstname" value={formData.firstname} onChange={handleInputChange} required />
4364
<Input type="text" label="Last Name" name="lastname" id="lastname" value={formData.lastname} onChange={handleInputChange} required />
44-
</div>
65+
</div> */}
4566
<Input
4667
type="text"
4768
label="Email Address"
@@ -66,8 +87,9 @@ export default function Registration() {
6687
<input className="btn ml-auto hover:cursor-pointer" type="submit" value="Register" />
6788
</form>
6889
<div>
90+
{errorMessage != "" && <h2 className="text-secondary">{errorMessage}</h2>}
6991
<h2 className="text-secondary">Please note</h2>
70-
92+
{session?.user && <h2 className="text-secondary">{session.user.name}</h2>}
7193
<p>
7294
Lorem ipsum dolor sit amet consectetur adipisicing elit. Vel est adipisci quas, aperiam voluptas, omnis tempora blanditiis quae fuga eum ullam
7395
commodi a perspiciatis provident pariatur sunt excepturi doloremque! Commodi!

web/src/config/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const CMS_API_URL = process.env.CMS_API ?? "http://cms:1337/api";
1+
export const CMS_PUBLIC_API_URL = process.env.CMS_PUBLIC_API_URL ?? "http://localhost:1337/api";
2+
export const CMS_INTERNAL_API_URL = process.env.CMS_INTERNAL_API_URL ?? "http://cms:1337/api";

web/src/lib/strapi/auth.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { CMS_API_URL } from "@/config/constants";
1+
import { CMS_PUBLIC_API_URL, CMS_INTERNAL_API_URL } from "@/config/constants";
22

33
export const signIn = async (email: string, password: string) => {
44
try {
5-
const response = await fetch(`${CMS_API_URL}/auth/local`, {
5+
const response = await fetch(`${CMS_INTERNAL_API_URL}/auth/local`, {
66
method: "POST",
77
headers: {
88
"Content-Type": "application/json",
@@ -12,9 +12,34 @@ export const signIn = async (email: string, password: string) => {
1212
password: password,
1313
}),
1414
});
15+
console.log("signin");
16+
return await response.json();
17+
} catch (e) {
18+
console.log(e);
19+
return null;
20+
}
21+
};
22+
23+
export const signUp = async (username: string, email: string, password: string) => {
24+
try {
25+
const response = await fetch(`${CMS_PUBLIC_API_URL}/auth/local/register`, {
26+
method: "POST",
27+
headers: {
28+
"Content-Type": "application/json",
29+
Accept: "application/json",
30+
},
31+
body: JSON.stringify({
32+
username: username,
33+
email: email,
34+
password: password,
35+
}),
36+
});
37+
console.log(response);
1538

1639
return await response.json();
17-
} catch {
40+
} catch (e) {
41+
console.log("something went wrong");
42+
console.log(e);
1843
return null;
1944
}
2045
};

web/src/lib/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export type StrapiSignInResponse = {
2+
jwt: string;
3+
user: {
4+
id: number;
5+
username: string;
6+
email: string;
7+
provider: string;
8+
confirmed: boolean;
9+
blocked: boolean;
10+
createdAt: string;
11+
updatedAt: string;
12+
};
13+
error?: {
14+
details: string[];
15+
};
16+
};
17+
18+
export type SignUpPayload = {
19+
username: string;
20+
email: string;
21+
password: string;
22+
};

0 commit comments

Comments
 (0)