Gregniuki commited on
Commit
b81db0f
1 Parent(s): e163adf

Update auth.py

Browse files
Files changed (1) hide show
  1. auth.py +20 -18
auth.py CHANGED
@@ -53,7 +53,24 @@ def authenticate_user(db: Session, email: str, password: str):
53
 
54
  from emailx import send_verification_email, generate_verification_token
55
 
 
 
 
 
 
 
 
 
 
 
56
 
 
 
 
 
 
 
 
57
 
58
  def register(user: UserCreate, db: Session):
59
  # Validate email format and check for existing users
@@ -61,7 +78,6 @@ def register(user: UserCreate, db: Session):
61
  if db_user:
62
  raise HTTPException(status_code=400, detail="Email already registered")
63
 
64
-
65
  # Hash the password
66
  hashed_password = pwd_context.hash(user.password)
67
 
@@ -71,6 +87,9 @@ def register(user: UserCreate, db: Session):
71
  # Send a verification email
72
  send_verification_email(user.email, verification_token)
73
 
 
 
 
74
  # Create the user in the database
75
  user_in_db = User(email=user.email, hashed_password=hashed_password)
76
  db.add(user_in_db)
@@ -78,24 +97,7 @@ def register(user: UserCreate, db: Session):
78
  db.refresh(user_in_db)
79
  return user_in_db
80
 
81
- def verify_email(self, verification_token: str, db: Session = Depends(get_db)):
82
- # Verify the email using the token (implement email.verify_token)
83
- email = email.verify_token(verification_token)
84
- if not email:
85
- raise HTTPException(status_code=400, detail="Invalid verification token")
86
-
87
- # Get the user by email
88
- user = database.get_user_by_email(db, email)
89
- if not user:
90
- raise HTTPException(status_code=400, detail="User not found")
91
-
92
- if user.is_verified:
93
- raise HTTPException(status_code=400, detail="Email already verified")
94
 
95
- # Mark the email as verified
96
- user.is_verified = True
97
- db.commit()
98
- return {"message": "Email verification successful"}
99
 
100
  def get_current_user(token: str = Depends(verify_token)):
101
  if not token:
 
53
 
54
  from emailx import send_verification_email, generate_verification_token
55
 
56
+ def verify_email(self, verification_token: str, db: Session = Depends(get_db)):
57
+ # Verify the email using the token (implement email.verify_token)
58
+ email = email.verify_token(verification_token)
59
+ if not email:
60
+ raise HTTPException(status_code=400, detail="Invalid verification token")
61
+
62
+ # Get the user by email
63
+ user = database.get_user_by_email(db, email)
64
+ if not user:
65
+ raise HTTPException(status_code=400, detail="User not found")
66
 
67
+ if user.is_verified:
68
+ raise HTTPException(status_code=400, detail="Email already verified")
69
+
70
+ # Mark the email as verified
71
+ user.is_verified = True
72
+ db.commit()
73
+ return {"message": "Email verification successful"}
74
 
75
  def register(user: UserCreate, db: Session):
76
  # Validate email format and check for existing users
 
78
  if db_user:
79
  raise HTTPException(status_code=400, detail="Email already registered")
80
 
 
81
  # Hash the password
82
  hashed_password = pwd_context.hash(user.password)
83
 
 
87
  # Send a verification email
88
  send_verification_email(user.email, verification_token)
89
 
90
+ # Verify the email
91
+ verify_email(verification_token, db)
92
+
93
  # Create the user in the database
94
  user_in_db = User(email=user.email, hashed_password=hashed_password)
95
  db.add(user_in_db)
 
97
  db.refresh(user_in_db)
98
  return user_in_db
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
 
 
 
 
101
 
102
  def get_current_user(token: str = Depends(verify_token)):
103
  if not token: