def login():
    session = AuthenticationService.authenticate(request.form["email"],
                                                 request.form["password"])
    if not session:
        return Response(
            "Incorrect username or password. Please check your credentials and try again.",
            status=403)

    user = User.objects.get(email=request.form["email"])
    if not AuthenticationService.isUserConfirmed(user):
        return Response("You must confirm your account to log in.", status=403)

    ret = make_response("Success")
    ret.set_cookie("SID", str(session.sessionId), expires=session.dateExpires)
    return ret
    def post(self):
        user = {
            "firstName": request.form["firstName"],
            "lastName": request.form["lastName"],
            "email": request.form["email"],
            "password": request.form["password"],
            "organization": request.form["organization"],
            "location": request.form["location"],
            "userType": request.form["userType"]
        }

        try:
            User.objects.get(email=user["email"])
            return Response(
                "There's already an account with the provided email.",
                status=400)
        except:
            try:
                AuthenticationService.signup(user)
                userConfirmationId = uuid4()
                user = User.objects.get(email=user["email"])
                if AuthenticationService.isUserConfirmed(user):
                    sessionId = uuid4()
                    session = Session(user=user, sessionId=sessionId)
                    session.save()
                    data = {
                        "message": "Google authorized successful!",
                        "user": user.email
                    }
                    ret = make_response(data)
                    ret.set_cookie("SID",
                                   str(session.sessionId),
                                   expires=session.dateExpires)
                    return ret
                AuthenticationService.setUserConfirmationId(
                    user, userConfirmationId)
                sub = "Confirm Account"
                msg = f"<p>Congratulations, you've registered for Agriworks. Please click the link below to confirm your account.</p><p><a href=\"{app.rootUrl}/confirm-user/{userConfirmationId}\"> Confirm account </a></p>"
                MailService.sendMessage(user, sub, msg)
                return Response("Signup successful", status=200)
            except:
                return Response("Signup unsuccessful. Please try again.",
                                status=403)