(
null,
);
const auth = useAuthentication();
useEffect(() => {
- const fetchListing = async () => {
- if (token === undefined) {
+ const fetchSignUpToken = async () => {
+ console.log(`id in useEffect: ${id}`);
+ if (id === undefined) {
return;
}
try {
const { data, error } = await auth.client.GET(
- "/email-signup/get/{token}",
+ "/email-signup/get/{id}",
{
params: {
- path: { token },
+ path: { id },
},
},
);
if (error) {
addErrorAlert(error);
} else {
- setEmailToken(data);
+ setSignUpToken(data);
}
} catch (err) {
addErrorAlert(err);
}
};
- fetchListing();
- }, [token]);
+ fetchSignUpToken();
+ }, [id]);
- if (!emailToken) {
+ if (!signUpToken) {
return (
Invalid Sign Up Link
diff --git a/store/app/crud/email_signup.py b/store/app/crud/email_signup.py
index 91e73eeb..27b28bb5 100644
--- a/store/app/crud/email_signup.py
+++ b/store/app/crud/email_signup.py
@@ -6,20 +6,20 @@
class EmailSignUpCrud(BaseCrud):
async def create_email_signup_token(self, email: str) -> EmailSignUpToken:
- token = EmailSignUpToken.create(email=email)
- await self._add_item(token)
- return token
+ signup_token = EmailSignUpToken.create(email=email)
+ await self._add_item(signup_token)
+ return signup_token
- async def get_email_signup_token(self, token: str) -> EmailSignUpToken | None:
- return await self._get_item(token, EmailSignUpToken, throw_if_missing=False)
+ async def get_email_signup_token(self, id: str) -> EmailSignUpToken | None:
- async def delete_email_signup_token(self, token: str) -> None:
- await self._delete_item(token)
+ return await self._get_item(id, EmailSignUpToken, throw_if_missing=False)
+
+ async def delete_email_signup_token(self, id: str) -> None:
+ await self._delete_item(id)
async def test_adhoc() -> None:
async with EmailSignUpCrud() as crud:
- token = await crud.create_email_signup_token(email="test@example.com")
- retrieved_token = await crud.get_email_signup_token(token.token)
- print(f"Retrieved Token: {retrieved_token}")
- await crud.delete_email_signup_token(token.token)
+ signup_token = await crud.create_email_signup_token(email="test@example.com")
+ await crud.get_email_signup_token(signup_token.id)
+ await crud.delete_email_signup_token(signup_token.id)
diff --git a/store/app/model.py b/store/app/model.py
index e0634eff..6b15f7b8 100644
--- a/store/app/model.py
+++ b/store/app/model.py
@@ -80,11 +80,10 @@ class EmailSignUpToken(RobolistBaseModel):
"""
email: EmailStr
- token: str
@classmethod
def create(cls, email: str) -> Self:
- return cls(email=email, token=new_uuid())
+ return cls(id=new_uuid(), email=email)
class OAuthKey(RobolistBaseModel):
diff --git a/store/app/routers/email_signup.py b/store/app/routers/email_signup.py
index 82aea647..71ded152 100644
--- a/store/app/routers/email_signup.py
+++ b/store/app/routers/email_signup.py
@@ -3,7 +3,8 @@
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel, EmailStr
-from store.app.crud.users import UserCrud
+from store.app.crud.email_signup import EmailSignUpCrud
+from store.app.utils.email import send_register_email
email_signup_router = APIRouter()
@@ -29,27 +30,31 @@ class DeleteTokenResponse(BaseModel):
# POST: Create Signup Token
@email_signup_router.post("/create/", response_model=EmailSignUpResponse)
-async def create_signup_token(data: EmailSignUpRequest, crud: UserCrud = Depends()) -> EmailSignUpResponse:
- try:
- await crud.create_email_signup_token(data.email)
- return {"message": "Sign-up token created successfully."}
- except Exception as e:
- raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
+async def create_signup_token(data: EmailSignUpRequest) -> EmailSignUpResponse:
+ async with EmailSignUpCrud() as crud:
+ try:
+ signup_token = await crud.create_email_signup_token(data.email)
+ await send_register_email(email=data.email, token=signup_token.id)
+
+ return {"message": "Sign up email sent! Follow the link sent to you to continue registration."}
+ except Exception as e:
+ print(f"Error creating signup token: {e}")
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
# GET: Retrieve Signup Token
-@email_signup_router.get("/get/{token}", response_model=GetTokenResponse)
-async def get_signup_token(token: str, crud: UserCrud = Depends()) -> GetTokenResponse:
- signup_token = await crud.get_email_signup_token(token)
+@email_signup_router.get("/get/{id}", response_model=GetTokenResponse)
+async def get_signup_token(id: str, crud: EmailSignUpCrud = Depends()) -> GetTokenResponse:
+ signup_token = await crud.get_email_signup_token(id)
if not signup_token:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Token not found.")
return signup_token
# DELETE: Delete Signup Token
-@email_signup_router.delete("/delete/{token}", response_model=DeleteTokenResponse)
-async def delete_signup_token(token: str, crud: UserCrud = Depends()) -> DeleteTokenResponse:
- deleted = await crud.delete_email_signup_token(token)
+@email_signup_router.delete("/delete/{id}", response_model=DeleteTokenResponse)
+async def delete_signup_token(id: str, crud: EmailSignUpCrud = Depends()) -> DeleteTokenResponse:
+ deleted = await crud.delete_email_signup_token(id)
if not deleted:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Token not found.")
return {"message": "Token deleted successfully."}