示例#1
0
def login(app):
    """

    :param app:
    :return: :rtype:
    """
    if request.method == 'GET':
        return render_template('login.html', error=None, title='Twic Login')

    if request.method == 'POST':
        error = list()
        name = request.form['name']
        password = request.form['password']

        # should be verifying via the redis wrapper methods, not using app.*
        # if name != app.config['name']:
        #     error.append('Invalid Username')
        # elif password != app.config['password']:
        #     error.append('Invalid Password')
        # else:
        # Set the session as logged in.
        user = None
        redis = StrictRedis()
        dbh = UserHelper(redis)
        email_validator = lepl.apps.rfc3696.Email()
        valid = email_validator(name)

        if email_validator(name) and dbh.email_exists(name):
            user = dbh.get_user_by_email(request.form['name'])
        elif dbh.username_exists(name):
            user = dbh.get_user_by_username(request.form['name'])

        if user:
            hashed_password = UserHelper.hash_password(password, user.salt)
            if hashed_password == user.password:
                # Save the user to the session
                session['user'] = user.get_dict()
                session['logged_in'] = True
                return redirect(url_for('dash'))
        else:
            error.append('That user does not exist.')

        return render_template('login.html', error=error)
示例#2
0
def register(app):
    """

    @param app:
    @type app: Flask
    @return:
    @rtype:
    """

    if request.method == 'GET':
        return render_template('registration.html',
                               error=None,
                               title='Twic Registration')

    if request.method == 'POST':
        errors = list()
        name = request.form['name']
        username = request.form['username']
        email = request.form['email']
        password = request.form['password']
        user = User(username, email, password, name)
        dbh = UserHelper(StrictRedis())

        if not name or not username or not email or not password:
            errors.append('All fields are required')

        if dbh.username_exists(user.username):
            errors.append(
                'That username already exists. Please choose another')

        if dbh.email_exists(user.email):
            errors.append('That email already exists. Please choose another')

        if len(errors):
            return render_template('registration.html',
                                   error=errors,
                                   title='Twic Registration',
                                   user=user)

        dbh.add_user(user)
        session['user'] = user.get_dict()
        session['logged_in'] = True
        return redirect(url_for('dash'))