neo4j-graphacademy / app-python

https://graphacademy.neo4j.com/courses/app-python
56 stars 92 forks source link

Registering a User Test Issue #20

Closed Sanchitkedia closed 6 months ago

Sanchitkedia commented 1 year ago

I followed the tutorial, but I'm experiencing an issue. Do you have any suggestions for how to resolve it? image

teowave commented 1 year ago

I am getting the exact same issue

I checked the 03 branch here on github as well, it looks like my code in the course is the same as on the 03 branch, but I get the test error

Sanchitkedia commented 1 year ago

Yes, I tested the 03 Branch as well and encountered the same problem.

adam-cowley commented 1 year ago

Can I take a look at the code you have in the register method of your AuthDAO?

Sanchitkedia commented 1 year ago

I have the code from the tutorial dropdown that says "Click here to reveal the fully-implemented register() method"

teowave commented 1 year ago

this is my code, I get the same error:

tag::register[]

def register(self, email, plain_password, name):
    encrypted = bcrypt.hashpw(plain_password.encode("utf8"), bcrypt.gensalt()).decode('utf8')

    def create_user(tx, email, encrypted, name):
        return tx.run(""" // (1)
            CREATE (u:User {
                userId: randomUuid(),
                email: $email,
                password: $encrypted,
                name: $name
            })
            RETURN u
        """,
        email=email, encrypted=encrypted, name=name # (2)
        ).single() 
    #end create_user function definition 

    try:
        with self.driver.session() as session:
            result = session.execute_write(create_user, email, encrypted, name)

            user = result['u']

            payload = {
                "userId": user["userId"],
                "email":  user["email"],
                "name":  user["name"],
            }

            payload["token"] = self._generate_token(payload)

            return payload
    except ConstraintError as err:
        # Pass error details through to a ValidationException
        raise ValidationException(err.message, {
            "email": err.message
        })

    # TODO: Handle unique constraint error
    if email != "graphacademy@neo4j.com":
        raise ValidationException(
            f"An account already exists with the email address {email}",
            {"email": "An account already exists with this email"}
        )

    # Build a set of claims
    payload = {
        "userId": "00000000-0000-0000-0000-000000000000",
        "email": email,
        "name": name,
    }

    # Generate Token
    payload["token"] = self._generate_token(payload)

    return payload
# end::register[]
adam-cowley commented 1 year ago

Can you try creating a .env file in the project root and adding JWT_SECRET=somerandomstring to it?

Akshithvicky commented 1 year ago

Can you try creating a .env file in the project root and adding JWT_SECRET=somerandomstring to it?

It solved the issue. First create the .env file using the command "touch .env" and write the content provided in the screenshot. Screenshot from 2023-09-12 13-05-20