def update_password(): form = UpdatePassword() if form.validate_on_submit(): if not bcrypt.check_password_hash(current_user.password, form.prev_password.data): flash(f'Incorrect Password', 'danger') else: current_user.password = bcrypt.generate_password_hash( form.password.data) db.session.commit() flash('Password was Updated', 'success') return render_template('update_password.html', form=form)
def register(): """Register user""" # If user is authenticated return tasks if current_user.is_authenticated: return redirect(url_for('tasks.task')) # User reached route via POST (as by submitting a form via POST) if request.method == "POST": # Ensure password and confirmation are the same if request.form.get("user_password") != request.form.get( "password_confirmation"): flash("The password did not match", "danger") return redirect(url_for('user.register')) # Form variables username = request.form.get("username") email = request.form.get('email') password = request.form.get("user_password") gender = request.form.get('user_gender') userJob = request.form.get('user_job') # Generate hash password hash = bcrypt.generate_password_hash(password).decode('utf-8') # Ensure username does not exist user = users.query.filter_by(username=username).first() if user: flash("Username exit. Please try another username", "danger") return redirect(url_for('user.register')) # Ensure email address does not exist userEmail = users.query.filter_by(email=email).first() if userEmail: flash("E-mail address exit. Please try another E-mail address", "danger") return redirect(url_for('user.register')) # Add user to database user = users(username=username, email=email, hash=hash, gender=gender, job=userJob) db.session.add(user) db.session.commit() flash("Register successfull", "info") return redirect(url_for("user.login")) else: return render_template("register.html")
def register(): form = registration() if form.validate_on_submit(): hashed_pwd = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_pwd) db.session.add(user) db.session.commit() flash( f'Your Account is successfully created! You are ready to Log In ', 'success') return redirect(url_for('login')) return render_template("register.html", title="register", form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): hashedPassword = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashedPassword) db.session.add(user) db.session.commit() flash(f'Account Created for {form.username.data}! You can log in now', 'success') return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def profile(): form = UpdateProfileForm() if form.validate_on_submit(): current_user.firstname = form.firstname.data current_user.lastname = form.lastname.data current_user.username = form.username.data current_user.email = form.email.data current_user.password = hashed_password = bcrypt.generate_password_hash( password=form.password.data).decode(encoding='utf-8') db.session.commit() flash(message="حساب کاربری شما با موفقیت به روزرسانی شد!", category='success') return redirect(location=url_for(endpoint='home')) elif request.method == 'GET': pass return render_template(template_name_or_list='profile.html', form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') worker = Worker(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(worker) db.session.commit() flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def register(): form = Registration() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') username = user(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(username) db.session.commit() return redirect(url_for('login')) else: print(form.email.data) print(form.password.errors) print(form.confirm_password.errors) return render_template('register.html', form=form)
def register(): """route for register page that uses register form, after account creation password hashes and inserts into the database and user gets redirected to login page """ if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash(f'Account created successfully!', 'success') return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('main.blog')) user = User.verify_reset_token(token) if user is None: flash('That is an Invalid or expired token', 'flash-unsuccess') return redirect(url_for('users.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data) user.password = hashed_password db.session.commit() flash('your password has been updated! You are now able to login.', 'flash-success') return redirect(url_for('users.login')) return render_template('Reset_Token.html', title='Reset Password', form=form)
def register(): form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( password=form.password.data).decode(encoding='utf-8') user = User( firstname=form.firstname.data, lastname=form.lastname.data, username=form.username.data, email=form.email.data, password=hashed_password ) db.session.add(user) db.session.commit() flash(message="ثبت نام شما با موفقیت انجام شد!", category='success') return redirect(location=url_for(endpoint='home')) return render_template(template_name_or_list='register.html', form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('users.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): hashedPassword = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashedPassword db.session.commit() flash('Your Password has been updated', 'success') return redirect(url_for('users.login')) return render_template('reset_token.html', title='Reset Password', form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('main.blog')) form = RegistrationForm() if form.validate_on_submit(): hashed_pwd = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(first_name=form.first_name.data.capitalize(), last_name=form.last_name.data.capitalize(), email=form.email.data.lower(), password=hashed_pwd) db.session.add(user) db.session.commit() flash("Your Account has been created! You are now able to login", "flash-success") return redirect(url_for('users.login')) return render_template("Register.html", title="Registering Page", form=form)
def reset_token(token): if current_user.is_authenticated: return redirect(url_for('home')) user = User.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): password = bcrypt.generate_password_hash(form.password.data) user.password = password db.session.commit() flash(f"Your Password has been updated.You are now able to login", "success") return redirect(url_for('login')) return render_template('reset_token.html', title='Reset Password', form=form)
def register(): if 'username' in session: return redirect(url_for('home')) form = RegistrationForm() # Conditional that checks if the registration is successful if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode( 'utf-8') # The user's password is hashed user = User(username=form.username.data, email=form.email.data, password=hashed_password) # The user is created SQLdb.session.add(user) # The user is added to the db SQLdb.session.commit() # The changes of the db are commited flash('Account created successfully. You can log in now.', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def resetToken(token): """Reset password Render reset_token.html template with valid token sent to user's email :type token: str :param token: password reset request token """ # If user is authenticated return tasks if current_user.is_authenticated: return redirect(url_for('task.task')) # Verify token user = users.verifyResetToken(token) # Ensure valid token if user is None: flash('That is an invalid or expired token', 'info') return redirect(url_for('user.resetRequest')) # User reached route via POST (as by submitting a form via POST) if request.method == "POST": # Ensure password and password confirmation are the same if request.form.get("new_password") != request.form.get( "password_confirmation"): flash("The password did not match", "danger") return redirect(url_for('user.resetToken')) # hash and update password hash = bcrypt.generate_password_hash( request.form.get("new_password")).decode('utf-8') user.hash = hash db.session.commit() flash('Your password has been updated!', 'info') return redirect(url_for('user.login')) return render_template('reset_token.html', token=token)
def add_users(): if not current_user.isAdmin: abort(403) form = RegistrationForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user: flash( f"This email is already registered. Please try with another email id", "info") else: password = secrets.token_hex(8) hashed_password = bcrypt.generate_password_hash(password) user = User(first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash(f"User Added", "success") send_create_user_email(user, password) return redirect(url_for('display_users')) return render_template('register.html', title='Register', form=form)
def account(): """User account""" # User reached route via POST (as by submitting a form via POST) if request.method == 'POST': # Form variables username = request.form.get("username") email = request.form.get('email') password = request.form.get("new_password") avatar = request.files['file'] # Ensure username was submitted if username: # Query database for username user = users.query.filter( func.lower(users.username) == func.lower(username)).first() # Ensure username does not exist if user: flash("Username exit. Please try another username", "info") return redirect(url_for('user.account')) # Update database user's username else: current_user.username = username db.session.commit() # Ensure email was submitted if email: # Ensure email address match with pattern '*****@*****.**' regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$' if (re.search(regex, email)): # Query database for username userEmail = users.query.filter( func.lower(users.email) == func.lower(email)).first() # Ensure email does not exist if userEmail: flash("Email exit. Please try another username", "info") return redirect(url_for('user.account')) else: current_user.email = email db.session.commit() else: flash("Invalid Email format", "info") return redirect(url_for('user.account')) # Ensure password was submitted if password: # Ensure password and confirmation are the same if request.form.get("new_password") != request.form.get( "password_confirmation"): flash("The password did not match", "danger") return redirect(url_for('user.account')) # Ensure old password is correct elif bcrypt.check_password_hash(current_user.hash, request.form.get('user_password')): hash = bcrypt.generate_password_hash(password).decode('utf-8') current_user.hash = hash db.session.commit() else: flash("Wrong password. Try again", "danger") return redirect(url_for('user.account')) # Ensure image was submitted if avatar: # Save avatar img return img name picFile = savePicture(avatar) # Update user's image_file current_user.image_file = picFile db.session.commit() # Make sure to show message if account updated if username or email or password or avatar: flash("Your account has been updated", "info") return redirect(url_for('user.account')) # User image file 'avatar' image_file = url_for('static', filename='avatars/' + current_user.image_file) return render_template('account.html', avatar=image_file)