Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am still in learning phase and having this issue while going through tutorials
I do not understand why I am getting this error
ValueError: Invalid salt
Complete Error Code is:
File "C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py",
line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py",
line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask\app.py",
line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "C:\Users\Aparichit\Desktop\NoViewsIndia\noViews\views.py", line 46, in login_page
if attempted_user and bcrypt.check_password_hash(attempted_user.password_hash,
attempted_password):
File "C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\flask_bcrypt.py",
line 193, in check_password_hash
return safe_str_cmp(bcrypt.hashpw(password, pw_hash), pw_hash)
File "C:\Users\Aparichit\AppData\Local\Programs\Python\Python39\Lib\site-packages\bcrypt_
init
_.py",
line 105, in hashpw
raise ValueError("Invalid salt")
My
model.py
code is:
class User(db.Model, UserMixin):
name = db.Column(db.String(20), nullable=False)
userName = db.Column(db.String(15), primary_key=True, nullable=False)
password_hash = db.Column(db.String(20), nullable=False)
@property
def password(self):
return self.password
@password.setter
def password(self, plain_text_password):
self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
And views.py is
@app.route('/admin_login', methods=['GET', 'POST'])
def login_page():
login_form =Login()
if login_form.validate_on_submit():
attempted_password = login_form.pWord.data
attempted_user = User.query.filter_by(userName = login_form.uName.data).first()
print(f'Route Username is {login_form.uName.data}')
print(f'Route User is {attempted_user}')
print(f' Route Password is {attempted_password}')
print(f'Route Hashed Password is {attempted_user.password_hash}')
if attempted_user and bcrypt.check_password_hash(attempted_user.password_hash, attempted_password):
login_user(attempted_user)
flash(f'You have successfully logged in, {attempted_user.name}')
else:
flash('Invalid Username and Password')
return render_template('admin_login.html', loginForm=login_form)
And form.py is
class Login(FlaskForm):
uName = StringField(label='User Name')
pWord = PasswordField(label='Password')
submit = SubmitField(label='Login')
And password that are saved:
Hassed Password
You need to decode your data before storing the data
pw_hash = bcrypt.generate_password_hash(‘hunter2’).decode(‘utf-8’)
read the docs
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.