Update auth.py
Browse files
auth.py
CHANGED
@@ -21,69 +21,69 @@ class AuthViews:
|
|
21 |
|
22 |
def register(self, user: UserCreate, db: Session = Depends(get_db)):
|
23 |
# Validate email format and check for existing users
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
# Hash the password
|
29 |
-
|
30 |
|
31 |
# Generate a verification token
|
32 |
-
|
33 |
|
34 |
# Send a verification email (implement email.send_verification_email)
|
35 |
|
36 |
# Create the user in the database
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
return user_in_db
|
43 |
|
44 |
def verify_email(self, verification_token: str, db: Session = Depends(get_db)):
|
45 |
# Verify the email using the token (implement email.verify_token)
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
# Get the user by email
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
|
58 |
# Mark the email as verified
|
59 |
-
|
60 |
-
|
61 |
return {"message": "Email verification successful"}
|
62 |
|
63 |
|
64 |
def login(self, form_data: OAuth2PasswordRequestForm = Depends()):
|
65 |
# Check email verification
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
71 |
-
|
72 |
|
73 |
# Generate an access token
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
return {"access_token": access_token, "token_type": "bearer"}
|
82 |
|
83 |
|
84 |
# Import User model and database functions
|
85 |
-
from app.models import User
|
86 |
-
from app.database import get_user_by_email
|
87 |
|
88 |
# ...
|
89 |
|
|
|
21 |
|
22 |
def register(self, user: UserCreate, db: Session = Depends(get_db)):
|
23 |
# Validate email format and check for existing users
|
24 |
+
db_user = database.get_user_by_email(db, user.email)
|
25 |
+
if db_user:
|
26 |
+
raise HTTPException(status_code=400, detail="Email already registered")
|
27 |
|
28 |
# Hash the password
|
29 |
+
hashed_password = self.pwd_context.hash(user.password)
|
30 |
|
31 |
# Generate a verification token
|
32 |
+
verification_token = email.generate_verification_token(user.email)
|
33 |
|
34 |
# Send a verification email (implement email.send_verification_email)
|
35 |
|
36 |
# Create the user in the database
|
37 |
+
user_in_db = models.User(email=user.email, hashed_password=hashed_password)
|
38 |
+
db.add(user_in_db)
|
39 |
+
db.commit()
|
40 |
+
db.refresh(user_in_db)
|
41 |
|
42 |
return user_in_db
|
43 |
|
44 |
def verify_email(self, verification_token: str, db: Session = Depends(get_db)):
|
45 |
# Verify the email using the token (implement email.verify_token)
|
46 |
+
email = email.verify_token(verification_token)
|
47 |
+
if not email:
|
48 |
+
raise HTTPException(status_code=400, detail="Invalid verification token")
|
49 |
|
50 |
# Get the user by email
|
51 |
+
user = database.get_user_by_email(db, email)
|
52 |
+
if not user:
|
53 |
+
raise HTTPException(status_code=400, detail="User not found")
|
54 |
|
55 |
+
if user.is_verified:
|
56 |
+
raise HTTPException(status_code=400, detail="Email already verified")
|
57 |
|
58 |
# Mark the email as verified
|
59 |
+
user.is_verified = True
|
60 |
+
db.commit()
|
61 |
return {"message": "Email verification successful"}
|
62 |
|
63 |
|
64 |
def login(self, form_data: OAuth2PasswordRequestForm = Depends()):
|
65 |
# Check email verification
|
66 |
+
db_user = database.get_user_by_email(db, form_data.username)
|
67 |
+
if not db_user or not self.pwd_context.verify(form_data.password, db_user.hashed_password):
|
68 |
+
raise HTTPException(status_code=400, detail="Incorrect email or password")
|
69 |
|
70 |
+
if not db_user.is_verified:
|
71 |
+
raise HTTPException(status_code=400, detail="Email not verified")
|
72 |
|
73 |
# Generate an access token
|
74 |
+
access_token_expires = timedelta(minutes=self.ACCESS_TOKEN_EXPIRE_MINUTES)
|
75 |
+
access_token = jwt.encode(
|
76 |
+
{"sub": db_user.email, "exp": datetime.utcnow() + access_token_expires},
|
77 |
+
self.SECRET_KEY,
|
78 |
+
algorithm=self.ALGORITHM,
|
79 |
+
)
|
80 |
|
81 |
return {"access_token": access_token, "token_type": "bearer"}
|
82 |
|
83 |
|
84 |
# Import User model and database functions
|
85 |
+
#from app.models import User
|
86 |
+
#from app.database import get_user_by_email
|
87 |
|
88 |
# ...
|
89 |
|