def register():
    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('Your account has been created! You are now able to log in', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #2
0
def register():
    if current_user.is_authenticated:
        flash('Current User have to logout first.', 'danger')
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pass = bcrypt.generate_password_hash(
            form.password.data).decode()
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pass)
        db.session.add(user)
        db.session.commit()
        flash('Account has been created, You can now login', 'success')
        return redirect(url_for('users.login'))
    return render_template("register.html", title='Register', form=form)
Beispiel #3
0
def register():
    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'Your account has been created! You are now able to log in.',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw)
        db.session.add(user)
        db.session.commit()
        flash(f'You have Successfully registered! You can now Login',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='About', form=form)
Beispiel #5
0
def register():
    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('Sua conta foi criada! Agora você já pode fazer o acesso :) ',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #6
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        passwd = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=passwd)
        db.session.add(user)
        db.session.commit()
        login_user(user, remember=False)
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('main.home'))
    return render_template("register.html", title="Register", form=form)
Beispiel #7
0
def set_register():
    # (1)生成注册表单实例
    register_form = RegistrationForm()

    # (2)自动获取前端用户输入的注册信息,进行验证
    # Step 1 验证注册【信息格式】及【唯一性】
    # 使用flash-WTF自带的validate_on_submit()验证用户提交的注册数据【是否符合格式要求】且【是否具有唯一性】
    # 【注意】预计这里将调用forms.py中自定义的validate_username、validate_email方法验证【数据唯一性】
    if register_form.validate_on_submit():
        # Step 2 若通过验证,则执行:
        # 哈希加密用户的密码 --> 将用户注册信息存入后端数据库 --> 弹出注册成功的提示 --> 跳转到登录页面

        # I、使用bcrypt模块,将用户注册时输入的密码使用哈希加密,生成hashed_password
        hashed_password = bcrypt.generate_password_hash(
            register_form.password.data).decode("utf-8")

        # II、将用户注册信息储存在后台db数据库中
        # 【注意】在使用User()储存用户注册数据之前,必须要确保已经执行了db.create_all()创建数据库(我们在命令行中执行过了),否则报错,no such table:User!!!!
        user = User(username=register_form.username.data,
                    email=register_form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()

        # III、弹出注册成功的提示
        flash(f"Account created for {register_form.username.data} 注册成功,请登录!!",
              category="success")
        # 在layout.html文件中(将传入register.html文件),使用get_flashed_messages方法调用这里的flash提示
        # 第二个参数category默认为message(无底色),但是可以传入基于bootstrap的六大标签类型default、primary、success、info、warning、danger,代表不同的底色

        # IV、登录成功后的网页跳转 redirect
        return redirect(url_for("users.set_login"))
        # redirect模块用于重定向至新的url,url_for("main.set_home")可以自动生成set_home函数对应的url路径
        # 重定向至/home路径之后,给用户展示的是home.html,且已添加flash页面提示。

    # 这里的else:pass可以省略不写,这里仅仅为了展示清晰
    else:
        pass

    # 本语句表示如果用户已经处于登录状态,在地址栏输入“/register”将直接重定向至set_home的页面
    if current_user.is_authenticated:
        return redirect(url_for("main.set_home"))

    # 【注意】这里的render_template渲染页面无论是否进行了验证,都是会执行的
    return render_template("register.html",
                           title="Register Page",
                           register_form=register_form)
Beispiel #8
0
def register():
    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(
            'Вы успешно зарегестрировались! Теперь вы можете войти в свой аккаунт',
            'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #9
0
def register():
    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 has been created for with ❤️! You are now be able to log in",
            "success")
        return redirect(url_for("users.login"))
    return render_template("register.html", title="Register", form=form)
Beispiel #10
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        ## 算出密码的HASH值
        Hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        ## 将固定格式的username,email和hash值密码存入数据库
        user_account = User(username=form.username.data.lower().title(),
                            email=form.email.data.lower(),
                            password=Hashed_password)
        db.session.add(user_account)
        db.session.commit()
        flash('Account created for {} !'.format(form.username.data), 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register Form', form=form)
Beispiel #11
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        #Here we add the user to the database
        #utf-8 is used to convert the hash into string
        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('Your account has been created. Please log in', 'success')
        return redirect(url_for('users.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'Your account has been created successfully! Please Login to continue to your feed.',
            'success')
        return redirect(url_for('users.login'))
    return render_template("register.html", title="Register", form=form)
Beispiel #13
0
def register():
    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,
                    passwords=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(
            f"Account Created for {form.username.data}, You Are Now Able To Log In!",
            'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', form=form, title='Registration')
Beispiel #14
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    if Config.REGISTRATION_DISABLED:
        return render_template('register_disabled.html', title='Register')

    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(name=form.name.data, email=form.email.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Welcome!', 'success')
        login_user(user)
        next_page = request.args.get('next')
        return redirect(next_page) if next_page else redirect(url_for('main.home', thoughts=[]))
    return render_template('register.html', title='Register', form=form)
Beispiel #15
0
def register():
    # 6 sends login user back to home page if he tries to click register page
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        # 6 hasing pasword with Bcrypt
        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()
        # python 3.6 for f'string'
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #16
0
def register():
    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 is created for {form.username.data} and activation link has been sent to your email. Please verify !',
            'success')
        send_activation_mail(user)
        return redirect(url_for('users.login'))
    return render_template('register.html', title='register', form=form)
Beispiel #17
0
def update_user(user_id):
    user = User.query.get_or_404(user_id)
    if current_user.username != 'admin237story':
        abort(403)
    form = RegistrationForm()
    if form.validate_on_submit():
        user.username = form.username.data
        user.email = form.email.data
        db.session.commit()
        flash('Your user has been updated!', 'success')
        return redirect(url_for('users.user_all', user_id=user.id))
    elif request.method == 'GET':
        form.username.data = user.username
        form.email.data = user.email
    return render_template('register.html',
                           title='Update Account',
                           form=form,
                           legend='Update Account')
Beispiel #18
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("main.home"))
    form = RegistrationForm()
    # To check the values entered is correct of not
    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 for {form.username.data} ! You can now login",
            "success")
        return redirect(url_for('users.login'))
    return render_template('register.html', title="Register", form=form)
Beispiel #19
0
def register():
    if current_user.is_authenticated:  # if user is already logged in, redirect him/her to the home page
        return redirect(url_for('main.home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        # if the form is validated, hash the password and create a new user with the username,
        # email and hashed_password and save the user to database to access later
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        # used .decode() to get string instead of bytes
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Your account has been created! You can now log in', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #20
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    register_form = RegistrationForm()
    if register_form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(
            register_form.password.data).decode('utf-8')
        user = User(username=register_form.username.data,
                    email=register_form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created for {register_form.username.data}!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html',
                           title='Register',
                           form=register_form,
                           num_registered=User.get_num_registered())
Beispiel #21
0
def register():
    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')
        item = {
            'Id': str(uuid.uuid4()),
            'Username': form.username.data,
            'Email': form.email.data,
            'Password': hashed_password
        }
        table.put_item(Item=item)
        flash('Your account has been created! You are now able to log in',
              'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #22
0
def register():
    if current_user.is_authenticated:
        return redirect('home')
    form = RegistrationForm()
    if form.validate_on_submit():
        hash_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hash_password)
        db.session.add(user)
        db.session.commit()
        flash("Account created successfully!. Now you can do login.",
              'success')
        return redirect('home')
    return render_template('registration.html',
                           title="Registration",
                           form=form)
Beispiel #23
0
def register():
    if current_user.is_authenticated:  #if user is logged in redirect on login/register to home page
        return redirect(url_for('home'))

#register using bcrypt for hashing passwords securely in db

    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('Registration complete, please log in!', 'success')
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #24
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("main.home_page"))
    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"{form.username.data} successfully registered! You can now log in.",
            "success",
        )
        return redirect(url_for("users.login"))
    return render_template("register.html", title="Sign Up", form=form)
Beispiel #25
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        # since no salt is passed, one is generated randomly
        # `fullhash` is of the form `salt$hash`
        full_hash = '$'.join(hash_pw(form.password.data))
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=full_hash)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in.',
              'success')
        return redirect(url_for('users.login'))

    return render_template('register.html', title='Register', form=form)
Beispiel #26
0
def register_page():
    if current_user.is_authenticated:
        return redirect(url_for("main.home_page"))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = brcypt.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(
            "Your Account has been Created! You can now Login!".format(
                {form.username.data}),
            "success",
        )
        return redirect(url_for("users.login_page"))
    return render_template("register.html", title="Register", form=form)
Beispiel #27
0
def registration():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    reg_form = RegistrationForm()

    if reg_form.validate_on_submit():
        password_hash = generate_password_hash(reg_form.password.data)
        user = User(username=reg_form.username.data,
                    email=reg_form.email.data,
                    password=password_hash)
        db.session.add(user)
        db.session.commit()
        flash(f'Account created for { reg_form.username.data } !', 'success')
        return redirect(url_for('users.login'))
    else:
        return render_template('users/registration.html',
                               title='Registration',
                               form=reg_form)
Beispiel #28
0
def register():
    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()
        # Bootstrap alert (success/category) as second arg
        flash(
            f'Account created for {form.username.data}! You are now able to log in.',
            'success')
        # redirect takes @users.route function
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #29
0
def register():
    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()
        # This gonna flash a message.
        # The second arguement of the flash method is for the category (here is used 'success').
        flash('Your account has been created! You are now able to log in',
              'success')
        # This gonna redirect to the home page after registering
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)
Beispiel #30
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw, confirmed=False
                    )
        db.session.add(user)
        db.session.commit()

        token = generate_email_confirmation_token(form.email.data)
        username = form.username.data
        flash('Account created for {}! An email has been sent with instructions howto activate your account'.format(username), 'success')
        return redirect(url_for('main.home'))
    return render_template('register.html', title='Register', form=form)
Beispiel #31
0
def register():
    if current_user.is_authenticated:  #if logged in, redirect to home
        return redirect(url_for('main.home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_pw = bcrypt.generate_password_hash(form.password.data).decode(
            'utf-8')  #hash pw
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_pw)
        #create user in database
        db.session.add(user)  #add to database
        db.session.commit()  #commit to database

        flash(f'Account created for {form.username.data}! You can now Login.',
              'success'
              )  #f means variable in flash msg, success tells type of message
        return redirect(url_for('users.login'))
    return render_template('register.html', title='Register', form=form)