1+ from typing import Optional
2+ from siwe import SiweMessage
13from fastapi import HTTPException
2- from pydantic import BaseModel , Field , EmailStr
34from sqlalchemy .orm import Session
4- from src . workspace . services . workspace_service import WorkspaceService
5- from src . auth . utils . bcrypt_helper import hash_password , verify_password
5+ from pydantic import BaseModel , Field , EmailStr
6+
67from src .db .models import UserModel
78from src .db .sql_alchemy import Database
8- from siwe import SiweMessage
9+ from src .share .base_types import WalletAddress
10+ from src .user .services .user_service import UserService
11+ from src .workspace .services .workspace_service import WorkspaceService
12+ from src .auth .utils .bcrypt_helper import hash_password , verify_password
913
1014database = Database ()
1115
1216
1317class RegisterInput (BaseModel ):
14- email : EmailStr = Field (
15- ... , example = "user@example.com" , description = "The email address of the user"
18+ email : Optional [ EmailStr ] = Field (
19+ None , example = "user@example.com" , description = "The email address of the user"
1620 )
17- password : str = Field (
21+ password : Optional [str ] = Field (
22+ None , example = "securepassword123" , description = "The password for the user account"
23+ )
24+ wallet_address : WalletAddress = Field (
1825 ...,
19- example = "securepassword123" ,
20- description = "The password for the user account" ,
26+ example = "0x0..." ,
27+ description = "The wallet address of the user"
28+ )
29+ chain_id : int = Field (
30+ ...,
31+ example = "1" ,
32+ description = "The chain id of the user"
2133 )
2234
2335
@@ -47,34 +59,44 @@ class AuthService:
4759 """
4860
4961 def __init__ (self ):
50- self .workspace = WorkspaceService ()
62+ self .workspace_service = WorkspaceService ()
63+ self .user_service = UserService ()
5164
5265 def register (self , input : RegisterInput , db : Session ) -> str :
5366 """
5467 Registers a new user by saving their details into the database.
5568
5669 Args:
57- input (RegisterInput): The registration input containing email and password .
70+ input (RegisterInput): The registration input .
5871 db (Session): The database session for executing queries.
5972
6073 Returns:
6174 str: Success message indicating the user was registered.
6275
6376 Raises:
64- HTTPException: If a user with the same email already exists.
77+ HTTPException: If a user with the same wallet_address already exists.
6578 """
6679 # Check if the user already exists
67- existing_user = db .query (UserModel ).filter_by (email = input .email .lower ()).first ()
80+ existing_user = db .query (UserModel ).filter_by (
81+ wallet_address = input .wallet_address ).first ()
6882 if existing_user :
6983 raise HTTPException (status_code = 400 , detail = "User already exists" )
7084
71- # Hash the user's password and create a new user record
72- hashed_password = hash_password (input .password )
73- new_user = UserModel (email = input .email .lower (), password = hashed_password )
74- db .add (new_user )
75- db .commit ()
85+ # # Create a new user record
86+ if input .email and input .password :
87+ existing_user = db .query (UserModel ).filter_by (
88+ email = input .email .lower ()).first ()
89+ if existing_user :
90+ raise HTTPException (
91+ status_code = 400 , detail = "This email is already exists" )
92+
93+ if input .password :
94+ input .password = hash_password (input .password )
95+
96+ new_user = self .user_service .create_user (
97+ db , input .wallet_address , input .chain_id , input .email , input .password , )
7698
77- self .workspace .create_workspace (db , "Default" , new_user .id )
99+ self .workspace_service .create_workspace (db , "Default" , new_user .id )
78100
79101 return new_user
80102
@@ -93,16 +115,19 @@ def authenticate_user(self, email: str, password: str, db: Session):
93115 """
94116 # Fetch the user from the database using the provided email
95117 user = db .query (UserModel ).filter_by (email = email ).first ()
118+ print (password , user .password )
96119 if not user :
97- raise HTTPException (status_code = 401 , detail = "Incorrect email or password" )
120+ raise HTTPException (
121+ status_code = 401 , detail = "Incorrect email or password" )
98122
99123 # Verify the provided password matches the stored hash
100124 if not verify_password (password , user .password ):
101- raise HTTPException (status_code = 401 , detail = "Incorrect email or password" )
125+ raise HTTPException (
126+ status_code = 401 , detail = "Incorrect email or password" )
102127
103128 return user
104129
105- def verify_signature (self , message : str , signature : str ):
130+ def verify_signature (self , message : str , signature : str , db : Session ):
106131 """
107132 Verify a signature according to SIWE spec
108133
@@ -116,7 +141,6 @@ def verify_signature(self, message: str, signature: str):
116141 Raises:
117142 HTTPException: If message cannot be parsed or verified
118143 """
119-
120144 try :
121145 siwe_message = SiweMessage .from_message (message )
122146 siwe_message .verify (signature )
@@ -128,7 +152,15 @@ def verify_signature(self, message: str, signature: str):
128152 raise HTTPException (
129153 status_code = 401 , detail = "Signature is not valid" )
130154
131- return {
132- "chain_id" : siwe_message .chain_id ,
133- "address" : siwe_message .address ,
134- }
155+ # check if not exist create new user
156+ user = self .user_service .get_user_by_wallet_address (
157+ siwe_message .address , db )
158+ if not user :
159+ user = self .register (RegisterInput (
160+ email = None ,
161+ password = None ,
162+ wallet_address = siwe_message .address ,
163+ chain_id = siwe_message .chain_id
164+ ), db )
165+
166+ return user
0 commit comments