From 2b886ec2c29b5ad52784bd2d415cd786fe7138a1 Mon Sep 17 00:00:00 2001 From: swve Date: Sun, 7 May 2023 17:44:54 +0000 Subject: [PATCH] feat: add org_id to a user when signing up --- .../app/{ => _orgs/[orgslug]}/signup/page.tsx | 13 +++++++------ front/middleware.ts | 2 +- front/services/auth/auth.ts | 3 ++- src/routers/users.py | 4 ++-- src/services/users/users.py | 18 ++++++++++++++++-- 5 files changed, 28 insertions(+), 12 deletions(-) rename front/app/{ => _orgs/[orgslug]}/signup/page.tsx (72%) diff --git a/front/app/signup/page.tsx b/front/app/_orgs/[orgslug]/signup/page.tsx similarity index 72% rename from front/app/signup/page.tsx rename to front/app/_orgs/[orgslug]/signup/page.tsx index 2860c4d8..1eaac7a6 100644 --- a/front/app/signup/page.tsx +++ b/front/app/_orgs/[orgslug]/signup/page.tsx @@ -1,18 +1,19 @@ "use client"; import React from "react"; -import { Title } from "../../components/UI/Elements/Styles/Title"; -import { signup } from "../../services/auth/auth"; +import { Title } from "../../../../components/UI/Elements/Styles/Title"; +import { signup } from "../../../../services/auth/auth"; -const SignUp = () => { +const SignUp = (params : any) => { + const org_slug = params.params.orgslug; 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 }); + console.log({ email, password, username, org_slug }); + alert(JSON.stringify({ email, password, username, org_slug })); + signup({ email, password, username, org_slug }); }; const handleEmailChange = (e: any) => { 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()),