feat: add org_id to a user when signing up

This commit is contained in:
swve 2023-05-07 17:44:54 +00:00
parent b88ee6643e
commit 2b886ec2c2
5 changed files with 28 additions and 12 deletions

View file

@ -1,18 +1,19 @@
"use client"; "use client";
import React from "react"; import React from "react";
import { Title } from "../../components/UI/Elements/Styles/Title"; import { Title } from "../../../../components/UI/Elements/Styles/Title";
import { signup } from "../../services/auth/auth"; import { signup } from "../../../../services/auth/auth";
const SignUp = () => { const SignUp = (params : any) => {
const org_slug = params.params.orgslug;
const [email, setEmail] = React.useState(""); const [email, setEmail] = React.useState("");
const [password, setPassword] = React.useState(""); const [password, setPassword] = React.useState("");
const [username, setUsername] = React.useState(""); const [username, setUsername] = React.useState("");
const handleSubmit = (e: any) => { const handleSubmit = (e: any) => {
e.preventDefault(); e.preventDefault();
console.log({ email, password, username }); console.log({ email, password, username, org_slug });
alert(JSON.stringify({ email, password, username })); alert(JSON.stringify({ email, password, username, org_slug }));
signup({ email, password, username }); signup({ email, password, username, org_slug });
}; };
const handleEmailChange = (e: any) => { const handleEmailChange = (e: any) => {

View file

@ -11,7 +11,7 @@ export const config = {
* 4. /examples (inside /public) * 4. /examples (inside /public)
* 5. all root files inside /public (e.g. /favicon.ico) * 5. all root files inside /public (e.g. /favicon.ico)
*/ */
"/((?!api|_next|fonts|login|signup|examples|[\\w-]+\\.\\w+).*)", "/((?!api|_next|fonts|examples|[\\w-]+\\.\\w+).*)",
], ],
}; };

View file

@ -64,6 +64,7 @@ interface NewAccountBody {
username: string; username: string;
email: string; email: string;
password: string; password: string;
org_slug: string;
} }
export async function signup(body: NewAccountBody): Promise<any> { export async function signup(body: NewAccountBody): Promise<any> {
@ -76,7 +77,7 @@ export async function signup(body: NewAccountBody): Promise<any> {
redirect: "follow", redirect: "follow",
}; };
return fetch(`${getAPIUrl()}users/`, requestOptions) return fetch(`${getAPIUrl()}users/?org_slug=${body.org_slug}`, requestOptions)
.then((result) => result.json()) .then((result) => result.json())
.catch((error) => console.log("error", error)); .catch((error) => console.log("error", error));
} }

View file

@ -37,11 +37,11 @@ async def api_get_user_by_userid(request: Request,user_id: str):
@router.post("/") @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 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}") @router.delete("/user_id/{user_id}")

View file

@ -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 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"] users = request.app.db["users"]
isUsernameAvailable = await users.find_one({"username": user_object.username}) 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.username = user_object.username.lower()
user_object.password = await security_hash_password(user_object.password) 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 # Create initial orgs list with the org_id passed in
orgs = [UserOrganization(org_id=org_id, org_role="member")] orgs = [UserOrganization(org_id=org_id, org_role="member")]
# Give role # Give role
roles = ["role_1"] roles = ["role_member"]
# Create the user # Create the user
user = UserInDB(user_id=user_id, creation_date=str(datetime.now()), user = UserInDB(user_id=user_id, creation_date=str(datetime.now()),