Skip to content

Commit

Permalink
Update Auth API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
DMalone87 committed Sep 28, 2024
1 parent 2b726f3 commit e8ce9d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions backend/dto/user/register_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
class RegisterUserDTO(BaseModel):
email: EmailStr
password: str
firstName: Optional[str]
lastName: Optional[str]
phoneNumber: Optional[str]
firstname: Optional[str]
lastname: Optional[str]
phone_number: Optional[str]
35 changes: 18 additions & 17 deletions backend/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@
from ..mixpanel.mix import track_to_mp
from ..database import User, UserRole, Invitation, StagedInvitation
from ..dto import LoginUserDTO, RegisterUserDTO
from ..schemas import validate_request

bp = Blueprint("auth", __name__, url_prefix="/api/v1/auth")


@bp.route("/login", methods=["POST"])
# @validate(auth=False, json=LoginUserDTO)
@validate_request(LoginUserDTO)
def login():
"""Sign in with email and password.
Returns an access token and sets cookies.
"""
logger = logging.getLogger("user_login")

body: LoginUserDTO = request.context.json
body: LoginUserDTO = request.validated_body

# Verify user
if body.password is not None and body.email is not None:
user = User.nodes.first_or_none(email=body.email)
if user is not None and user.verify_password(body.password):
token = create_access_token(identity=user.uid)
logger.info(f"User {user.uid} logged in successfully.")
resp = jsonify(
{
"message": "Successfully logged in.",
Expand All @@ -58,15 +61,14 @@ def login():


@bp.route("/register", methods=["POST"])
# @validate(auth=False, json=RegisterUserDTO)
@validate_request(RegisterUserDTO)
def register():
"""Register for a new public account.
If successful, also performs login.
"""

body: RegisterUserDTO = request.context.json
logger = logging.getLogger("user_register")
body: RegisterUserDTO = request.validated_body

# Check to see if user already exists
user = User.nodes.first_or_none(email=body.email)
Expand All @@ -79,11 +81,10 @@ def register():
if body.password is not None and body.email is not None:
user = User(
email=body.email,
password=User.hash_password(body.password),
first_name=body.firstName,
last_name=body.lastName,
role=UserRole.PUBLIC,
phone_number=body.phoneNumber,
password_hash=User.hash_password(body.password),
first_name=body.firstname,
last_name=body.lastname,
phone_number=body.phone_number,
)
user.save()
token = create_access_token(identity=user.uid)
Expand Down Expand Up @@ -133,7 +134,6 @@ def register():

@bp.route("/refresh", methods=["POST"])
@jwt_required()
# @validate()
def refresh_token():
"""Refreshes the currently-authenticated user's access token."""

Expand All @@ -149,7 +149,7 @@ def refresh_token():


@bp.route("/logout", methods=["POST"])
# @validate(auth=False)
@jwt_required()
def logout():
"""Unsets access cookies."""
resp = jsonify({"message": "successfully logged out"})
Expand All @@ -161,7 +161,6 @@ def logout():
@cross_origin()
@jwt_required()
@min_role_required(UserRole.PUBLIC)
# @validate()
def test_auth():
"""Returns the currently-authenticated user."""
current_identity = get_jwt_identity()
Expand All @@ -173,9 +172,9 @@ class EmailDTO(BaseModel):


@bp.route("/forgotPassword", methods=["POST"])
# @validate(auth=False, json=EmailDTO)
@validate_request(EmailDTO)
def send_reset_email():
body: EmailDTO = request.context.json
body: EmailDTO = request.validated_body
logger = logging.getLogger("user_forgot_password")
user = User.get_by_email(body.email)
if user is not None:
Expand All @@ -194,11 +193,13 @@ class PasswordDTO(BaseModel):

@bp.route("/setPassword", methods=["POST"])
@jwt_required()
# @validate(auth=True, json=PasswordDTO)
@validate_request(PasswordDTO)
def reset_password():
body: PasswordDTO = request.context.json
logger = logging.getLogger("user_reset_password")
body: PasswordDTO = request.validated_body
# NOTE: 401s if the user or token is not valid
# NOTE: This token follows the logged in user token lifespan
user = User.get(get_jwt_identity())
user.set_password(body.password)
logger.info(f"User {user.uid} reset their password.")
return {"message": "Password successfully changed"}, 200

0 comments on commit e8ce9d9

Please sign in to comment.