Skip to content

Commit 1676a8c

Browse files
Merge pull request #28 from monicasmith463/feature/frontend-signup-api
Feature: frontend signup API call working
2 parents a07f822 + 645b53c commit 1676a8c

File tree

6 files changed

+68
-27
lines changed

6 files changed

+68
-27
lines changed

backend/app/api/routes/users.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def read_users(session: SessionDep, skip: int = 0, limit: int = 100) -> Any:
4848
return UsersPublic(data=users, count=count)
4949

5050

51-
@router.post(
52-
"/", dependencies=[Depends(get_current_active_superuser)], response_model=UserPublic
53-
)
51+
@router.post("/", response_model=UserPublic)
5452
def create_user(*, session: SessionDep, user_in: UserCreate) -> Any:
5553
"""
5654
Create new user.

backend/tests/api/routes/test_users.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,6 @@ def test_create_user_existing_username(
132132
assert "_id" not in created_user
133133

134134

135-
def test_create_user_by_normal_user(
136-
client: TestClient, normal_user_token_headers: dict[str, str]
137-
) -> None:
138-
username = random_email()
139-
password = random_lower_string()
140-
data = {"email": username, "password": password}
141-
r = client.post(
142-
f"{settings.API_V1_STR}/users/",
143-
headers=normal_user_token_headers,
144-
json=data,
145-
)
146-
assert r.status_code == 403
147-
148-
149135
def test_retrieve_users(
150136
client: TestClient, superuser_token_headers: dict[str, str], db: Session
151137
) -> None:

frontend/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
"scripts": {
77
"dev": "next dev",
88
"build": "next build",
9-
"start": "next start",
9+
"start": "next start -H 0.0.0.0",
1010
"lint": "next lint",
11+
"lint:fix": "next lint --fix",
12+
"format": "prettier --write .",
13+
"clean": "npm run lint:fix && npm run format",
1114
"generate-client": "openapi-ts"
1215
},
1316
"dependencies": {
@@ -23,7 +26,7 @@
2326
"@tailwindcss/postcss": "^4.0.9",
2427
"@tanstack/react-query": "^5.90.5",
2528
"@tanstack/react-router": "^1.133.21",
26-
"apexcharts": "^4.3.0",
29+
"apexcharts": "^4.3.0",
2730
"autoprefixer": "^10.4.20",
2831
"axios": "^1.12.2",
2932
"flatpickr": "^4.6.13",

frontend/src/app/layout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import { Outfit } from 'next/font/google';
55
import { SidebarProvider } from '@/context/SidebarContext';
66
import { ThemeProvider } from '@/context/ThemeContext';
77
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
8+
import { OpenAPI } from "@/client/core/OpenAPI";
9+
10+
OpenAPI.TOKEN = async () => {
11+
return localStorage.getItem("access_token") || ""
12+
}
13+
14+
OpenAPI.BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000";
815

916
const queryClient = new QueryClient();
1017

frontend/src/components/auth/SignUpForm.tsx

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ import Label from "@/components/form/Label";
44
import { ChevronLeftIcon, EyeCloseIcon, EyeIcon } from "@/icons";
55
import Link from "next/link";
66
import React, { useState } from "react";
7-
import { namePattern, passwordRules } from "@/utils";
7+
import { namePattern, emailPattern, passwordRules } from "@/utils";
88
import { useForm } from "react-hook-form";
9+
import { useSignup } from "@/hooks/useSignup";
10+
import SpinnerButton from "../ui/button/SpinnerButton";
11+
912

1013
type FormData = {
1114
firstName: string;
1215
lastName: string;
1316
password: string;
17+
email: string;
1418
};
1519

1620
export default function SignUpForm() {
1721
const [showPassword, setShowPassword] = useState(false);
22+
const signupMutation = useSignup();
23+
1824
const {
1925
register,
2026
handleSubmit,
@@ -24,6 +30,23 @@ export default function SignUpForm() {
2430
});
2531
const onSubmit = (data: FormData) => {
2632
console.log("Form data:", data);
33+
signupMutation.mutate(
34+
{
35+
requestBody: {
36+
full_name: data.firstName + " " + data.lastName,
37+
password: data.password,
38+
email: data.email,
39+
},
40+
},
41+
{
42+
onSuccess: (res) => {
43+
console.log("Signup success:", res);
44+
},
45+
onError: (err) => {
46+
console.error("Signup failed:", err);
47+
},
48+
}
49+
);
2750
};
2851

2952
return (
@@ -34,7 +57,7 @@ export default function SignUpForm() {
3457
className="inline-flex items-center text-sm text-gray-500 transition-colors hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300"
3558
>
3659
<ChevronLeftIcon />
37-
Back to dashboard
60+
Back to home
3861
</Link>
3962
</div>
4063

@@ -81,7 +104,22 @@ export default function SignUpForm() {
81104
hint={errors.lastName?.message}
82105
/>
83106
</div>
84-
107+
{/* Email */}
108+
<div className="sm:col-span-2">
109+
<Label>
110+
Email<span className="text-error-500">*</span>
111+
</Label>
112+
<Input
113+
placeholder="Enter your email"
114+
type="email"
115+
{...register("email", {
116+
required: "Email is required",
117+
pattern: emailPattern
118+
})}
119+
error={!!errors.email}
120+
hint={errors.email?.message}
121+
/>
122+
</div>
85123
{/* Password */}
86124
<div className="sm:col-span-2">
87125
<Label>
@@ -107,19 +145,18 @@ export default function SignUpForm() {
107145
</span>
108146
</div>
109147
</div>
110-
111148
{/* Submit Button */}
112149
<div className="sm:col-span-2">
113-
<button
114-
type="submit"
150+
<SpinnerButton
115151
className="flex items-center justify-center w-full px-4 py-3 text-sm font-medium text-white transition rounded-lg bg-brand-500 shadow-theme-xs hover:bg-brand-600"
152+
disabled={signupMutation.isPending}
153+
loading={signupMutation.isPending}
116154
>
117155
Sign Up
118-
</button>
156+
</SpinnerButton>
119157
</div>
120158
</div>
121159
</form>
122-
123160
<div className="mt-5">
124161
<p className="text-sm font-normal text-center text-gray-700 dark:text-gray-400 sm:text-start">
125162
Already have an account?{" "}

frontend/src/hooks/useSignup.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useMutation } from "@tanstack/react-query";
2+
import { UsersService } from "@/client";
3+
import type { UsersCreateUserData } from "@/client";
4+
5+
export const useSignup = () => {
6+
return useMutation({
7+
mutationFn: (data: UsersCreateUserData) =>
8+
UsersService.createUser({ requestBody: data.requestBody }),
9+
});
10+
};

0 commit comments

Comments
 (0)