Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

check_password_hash always failing

Hi guys,

I'm doing the Tacocat challenge, and I'm struggling a bit with logging in/registering a user.

I kept getting a "password incorrect" error, despite the fact I knew the password was correct. I made a new user, but this time I did pdb.set_trade() just after the new user was created so I could check the password hash at the horses mouth... and it failed.

I can only imagine I'm passing the wrong data to the password field but I can't see where it's coming from.

Any thoughts?

Here is my register view

@app.route('/register', methods = ('GET', 'POST'))
def register():
    form = forms.RegisterForm()
    if form.validate_on_submit():
        flash('You have been registered!')
        models.User.create_user(
            email = form.email.data,
            password = form.password.data
        )
        new_user = models.User.get(models.User.email == form.email.data)
        pdb.set_trace()
        login_user(new_user)
        return redirect(url_for('index'))
    return render_template('register.html', form = form)

Here is my User model

class User(UserMixin, Model):
    email = CharField(unique = True)
    password = CharField(max_length = 100)
    joined_date = DateTimeField(default = datetime.datetime.now)

    class Meta:
        database = DB

    @classmethod
    def create_user(cls, email, password):
        try:
            cls.create(
                email = email,
                password = generate_password_hash(password)
            )
        except IntegrityError:
            raise ValueError("User already exists")

And here is my register form

class RegisterForm(Form):
    email = StringField('Email address', validators = [
        DataRequired(),
        Email(),
        check_email_exists
    ])

    password = PasswordField('Password', validators = [
        DataRequired(),
        EqualTo('password2', message = "These passwords don't match!")
    ])

    password2 = PasswordField('Confirm Password', validators = [
        DataRequired()
    ])

I'd be grateful for any assistance, this has had my scratching my head for a while!

-Tayler

1 Answer

So... no one may ever read this but... I figured it out!

And word to the wise.

Be careful how long you make your database's password field length because hashed passwords are more than 50 characters!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Good job figuring it out! Awesome to see people using pdb, too.

Thanks Kenneth Love , you can only imagine the sigh on relief after I realised what I'd done..!

My newest issue is that all my tests are passing but I'm getting "bummer" on the code challenge! But I think that's going to have to wait until the morning...