Esempio n. 1
0
def login():

    # Logged in? Send to home screen
    if current_user.is_authenticated:
        return redirect('/')

    form = LoginForm()
    if form.validate_on_submit():

        # Check if we are logging in with email
        is_email = False
        email_reg = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
        if re.match(email_reg, form.username.data, re.IGNORECASE):
            is_email = True

        # Attempt to find user and validate credentials
        user = User.get_user(form.username.data, email=is_email)
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password!')
            return redirect('/login')

        # Log in the user
        login_user(user, remember=form.remember_me.data)
        return redirect('/')

    return render_template('login.html', title='Sign In', form=form)
Esempio n. 2
0
    def validate_username(self, username):
        name = username.data
        sanitized = re.sub(r'\W+', '*', name)

        # Sanitize username, ensure it is alphanumeric with underscore
        if name is not sanitized:
            self.username.errors.append('Invalid username.')
            return False

        # Check if username is already being used
        user = User.get_user(name)
        if user is not None:
            self.username.errors.append('Username already in use.')
            return False
        return True
Esempio n. 3
0
def load_user(username):
    return User.get_user(username)
Esempio n. 4
0
 def validate_email(self, email):
     user = User.get_user(email.data, email=True)
     if user is None:
         self.email.errors.append('Could not find account.')
         return False
     return True
Esempio n. 5
0
 def validate_email(self, email):
     user = User.get_user(email.data, email=True)
     if user is not None:
         self.email.errors.append('Email already in use.')
         return False
     return True