def login():
username = loggedIn(session, LoggedIn)
if username != False:
return render_template('index.html', username=username)
form = LoginForm()
if form.validate_on_submit():
pwd = request.form['password']
existing_user = User.query.filter_by(username=request.form['username']).first()
if not existing_user:
error = 'Username or password are incorrect.'
return render_template('login.html', form=form, loggedInError=error)
hash1 = hashpw(str(pwd).encode('utf-8'), str(existing_user.password).encode('utf-8'))
hash2 = existing_user.password
if hash1 != hash2:
error = 'Username or password are incorrect.'
return render_template('login.html', form=form, loggedInError=error)
rand_ID = str(randId())
while True:
user = LoggedIn.query.filter_by(rand_id=rand_ID).first()
if user:
rand_ID = str(randId())
else:
break
userLoggedIn = LoggedIn(username=request.form['username'], rand_id=rand_ID)
db.session.add(userLoggedIn)
db.session.commit()
session['user'] = rand_ID
return render_template('index.html', username=userLoggedIn.username)
return render_template('login.html', form=form)
评论列表
文章目录