diff --git a/front/app/login/page.tsx b/front/app/_orgs/[orgslug]/login/page.tsx similarity index 96% rename from front/app/login/page.tsx rename to front/app/_orgs/[orgslug]/login/page.tsx index 79d2d5a6..f64a72a2 100644 --- a/front/app/login/page.tsx +++ b/front/app/_orgs/[orgslug]/login/page.tsx @@ -2,8 +2,8 @@ import { useRouter } from 'next/navigation'; import React, { useState } from "react"; import { styled } from '@stitches/react'; -import { Title } from "../../components/UI/Elements/Styles/Title"; -import { loginAndGetToken } from "../../services/auth/auth"; +import { Title } from "../../../../components/UI/Elements/Styles/Title"; +import { loginAndGetToken } from "../../../../services/auth/auth"; import FormLayout, { ButtonBlack, Flex, FormField, FormLabel, FormMessage, Input } from '@components/UI/Form/Form'; import * as Form from '@radix-ui/react-form'; import { BarLoader } from 'react-spinners'; diff --git a/front/app/_orgs/[orgslug]/signup/page.tsx b/front/app/_orgs/[orgslug]/signup/page.tsx new file mode 100644 index 00000000..3f0fc0db --- /dev/null +++ b/front/app/_orgs/[orgslug]/signup/page.tsx @@ -0,0 +1,118 @@ +"use client"; +import React from "react"; +import { Title } from "../../../../components/UI/Elements/Styles/Title"; +import { signup } from "../../../../services/auth/auth"; +import { useRouter } from "next/navigation"; + +const SignUp = (params: any) => { + const org_slug = params.params.orgslug; + const router = useRouter(); + const [email, setEmail] = React.useState(""); + const [password, setPassword] = React.useState(""); + const [username, setUsername] = React.useState(""); + + const handleSubmit = (e: any) => { + e.preventDefault(); + console.log({ email, password, username, org_slug }); + alert(JSON.stringify({ email, password, username, org_slug })); + try { + signup({ email, password, username, org_slug }); + router.push("/"); + } + catch (err) { + console.log(err); + } + }; + + const handleEmailChange = (e: any) => { + setEmail(e.target.value); + }; + + const handlePasswordChange = (e: any) => { + setPassword(e.target.value); + }; + + const handleUsernameChange = (e: any) => { + setUsername(e.target.value); + }; + + return ( +
+
+ Sign up + + {/* Create a login ui with tailwindcss */} +
+
+
+
+ + + + + + + + + + + + + + + Already have an account? Login + + + + Home + + + +
+
+ +
+
+
+
+ ); +}; +export default SignUp; diff --git a/front/app/signup/page.tsx b/front/app/signup/page.tsx deleted file mode 100644 index 2860c4d8..00000000 --- a/front/app/signup/page.tsx +++ /dev/null @@ -1,47 +0,0 @@ -"use client"; -import React from "react"; -import { Title } from "../../components/UI/Elements/Styles/Title"; -import { signup } from "../../services/auth/auth"; - -const SignUp = () => { - const [email, setEmail] = React.useState(""); - const [password, setPassword] = React.useState(""); - const [username, setUsername] = React.useState(""); - - const handleSubmit = (e: any) => { - e.preventDefault(); - console.log({ email, password, username }); - alert(JSON.stringify({ email, password, username })); - signup({ email, password, username }); - }; - - const handleEmailChange = (e: any) => { - setEmail(e.target.value); - }; - - const handlePasswordChange = (e: any) => { - setPassword(e.target.value); - }; - - const handleUsernameChange = (e: any) => { - setUsername(e.target.value); - }; - - return ( -
-
- Sign up - -
- - - - -
-
-
- ); -}; -export default SignUp; diff --git a/front/middleware.ts b/front/middleware.ts index f78c3cf4..3c3d3f61 100644 --- a/front/middleware.ts +++ b/front/middleware.ts @@ -11,7 +11,7 @@ export const config = { * 4. /examples (inside /public) * 5. all root files inside /public (e.g. /favicon.ico) */ - "/((?!api|_next|fonts|login|signup|examples|[\\w-]+\\.\\w+).*)", + "/((?!api|_next|fonts|examples|[\\w-]+\\.\\w+).*)", ], }; diff --git a/front/services/auth/auth.ts b/front/services/auth/auth.ts index 2eb90630..c952b2f8 100644 --- a/front/services/auth/auth.ts +++ b/front/services/auth/auth.ts @@ -64,6 +64,7 @@ interface NewAccountBody { username: string; email: string; password: string; + org_slug: string; } export async function signup(body: NewAccountBody): Promise { @@ -76,7 +77,7 @@ export async function signup(body: NewAccountBody): Promise { redirect: "follow", }; - return fetch(`${getAPIUrl()}users/`, requestOptions) + return fetch(`${getAPIUrl()}users/?org_slug=${body.org_slug}`, requestOptions) .then((result) => result.json()) .catch((error) => console.log("error", error)); } diff --git a/src/routers/users.py b/src/routers/users.py index 3a17567c..6826077a 100644 --- a/src/routers/users.py +++ b/src/routers/users.py @@ -37,11 +37,11 @@ async def api_get_user_by_userid(request: Request,user_id: str): @router.post("/") -async def api_create_user(request: Request,user_object: UserWithPassword, org_id: str ): +async def api_create_user(request: Request,user_object: UserWithPassword, org_slug: str ): """ Create new user """ - return await create_user(request, None, user_object, org_id) + return await create_user(request, None, user_object, org_slug) @router.delete("/user_id/{user_id}") diff --git a/src/services/users/users.py b/src/services/users/users.py index e64adcc4..87a90a7d 100644 --- a/src/services/users/users.py +++ b/src/services/users/users.py @@ -7,7 +7,7 @@ from src.security.security import security_hash_password, security_verify_passwo from src.services.users.schemas.users import PasswordChangeForm, PublicUser, User, UserOrganization, UserWithPassword, UserInDB -async def create_user(request: Request, current_user: PublicUser | None, user_object: UserWithPassword, org_id: str): +async def create_user(request: Request, current_user: PublicUser | None, user_object: UserWithPassword, org_slug: str): users = request.app.db["users"] isUsernameAvailable = await users.find_one({"username": user_object.username}) @@ -34,11 +34,25 @@ async def create_user(request: Request, current_user: PublicUser | None, user_o user_object.username = user_object.username.lower() user_object.password = await security_hash_password(user_object.password) + # Get org_id from org_slug + orgs = request.app.db["organizations"] + + # Check if the org exists + isOrgExists = await orgs.find_one({"slug": org_slug}) + + # If the org does not exist, raise an error + if not isOrgExists: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, detail="You are trying to create a user in an organization that does not exist") + + org_id = isOrgExists["org_id"] + + # Create initial orgs list with the org_id passed in orgs = [UserOrganization(org_id=org_id, org_role="member")] # Give role - roles = ["role_1"] + roles = ["role_member"] # Create the user user = UserInDB(user_id=user_id, creation_date=str(datetime.now()),