def login():
    notice = Notice_Post.query.all()
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()

    if form.validate_on_submit():
        user = Worker.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            if form.email.data == '*****@*****.**' and form.password.data == 'admin':
                next_page = request.args.get('next')
                return redirect(next_page) if next_page else redirect(
                    url_for('admin'))
            else:
                login_user(user)
                next_page = request.args.get('next')
                return redirect(next_page) if next_page else redirect(
                    url_for('home'))
        else:
            flash('Login Unsuccessful. Pleach check again', 'danger')
    return render_template('login.html',
                           title='Login',
                           form=form,
                           notice=notice)
def login():
    """ Get data for the recent activity sidebar (in base.html) """
    recent = utils.get_recent_activity()
    """ If user is logged in, these routes are unnecessary and so redirect to home page """
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    """ Instantiate form """
    form = LoginForm()
    if form.validate_on_submit():
        user = db.users.find_one({'email': form.email.data})
        if user and user['password'] == form.password.data:
            user_obj = User(username=user['username'],
                            email=user['email'],
                            bio=user['bio'],
                            location=user['location'],
                            level=user['level'],
                            password=user['password'])
            login_user(user_obj, remember=form.remember.data)
            """ The following assures the user will be redirected to the page she wanted before being prompted to sign in (if prompted) """
            next_page = request.args.get('next')
            if next_page:
                return redirect(next_page)
            else:
                return redirect(url_for('home'))
        else:
            flash('Login Unsuccessful. Please check email and password',
                  'danger')
    return render_template('login.html',
                           title='Login',
                           form=form,
                           recent=recent)
Example #3
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if form.email.data == '*****@*****.**' and form.password.data == 'password':
            flash('You have been logged in!', 'success')
            return redirect(url_for('home'))
        else:
            flash('Login Unsuccessful. Please check username and password',
                  'danger')
    return render_template('login.html', title='Login', form=form)
Example #4
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash(_('Invalid username or password'))
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', title=_('Sign In'), form=form)
Example #5
0
def login():
    if current_user.is_authenticated:
        return redirect(location=url_for(endpoint='home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user and bcrypt.check_password_hash(pw_hash=user.password, password=form.password.data):
            login_user(user=user, remember=form.remember.data)
            next_page = request.args.get('next')
            flash(message="ورود شما با موفقیت انجام شد!", category='success')
            return redirect(location=next_page if next_page else url_for(endpoint='home'))
        else:
            flash(message="نام کاربری یا رمز عبور وارد شده صحیح نمیباشد!",
                  category='danger')
    return render_template(template_name_or_list='login.html', form=form)
Example #6
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first()
        if user and bcrypt.check_password_hash(user.password,
                                               form.password.data):
            next_page = request.args.get('next')
            login_user(user)
            flash(f'Welcome {current_user.first_name}!', 'success')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
        else:
            flash(f"Your login credentials don't match", 'danger')

    return render_template('login.html', form=form)
Example #7
0
def login():
    if current_user.is_authenticated:  #already logged in ..so dont login again
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():  #checks if all reqd fields are entered?
        flash('Login requested for user {}'.format(form.username.data))
        user = User.query.filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            #checks with pass entered by input user
            flash('Invalid username or password')
            return redirect(url_for('login'))
        login_user(user, remember=True)
        next_page = request.args.get('next')
        if not next_page or url_parse(next_page).netloc != '':
            #next arg has no val in url then go to index
            next_page = url_for('index')
        return redirect(next_page)
    return render_template('login.html', title='Sign In', form=form)
Example #8
0
def login(oid):
    if g.user is not None and g.user.is_authenticated:  # Flask 中的 g 全局变量是一个在请求生命周期中用来存储和共享数据。我敢肯定你猜到了,我们将登录的用户存储在这里(g)。
        return redirect(url_for('index'))
    form = LoginForm()
    if form.validate_on_submit():
        # flash('Login requested for OpenID="' + form.openid.data + '", remember_me=' + str(form.remember_me.data))
        # return redirect('/index')
        session[
            'remember_me'] = form.remember_me.data  # 当我们从登录表单获取的数据后的处理代码也是新的。这里我们做了两件事。首先,我们把 remember_me 布尔值存储到 flask 的会话中,这里别与 Flask-SQLAlchemy 中的 db.session 弄混淆。之前我们已经知道 flask.g 对象在请求整个生命周期中存储和共享数据。flask.session 提供了一个更加复杂的服务对于存储和共享数据。一旦数据存储在会话对象中,在来自同一客户端的现在和任何以后的请求都是可用的。数据保持在会话中直到会话被明确地删除。为了实现这个,Flask 为我们应用程序中每一个客户端设置不同的会话文件。
        return oid.try_login(
            form.openid.data, ask_for=['nickname', 'email']
        )  # id.try_login 被调用是为了触发用户使用 Flask-OpenID 认证。该函数有两个参数,用户在 web 表单提供的 openid 以及我们从 OpenID 提供商得到的数据项列表。因为我们已经在用户模型类中定义了 nickname 和 email,这也是我们将要从 OpenID 提供商索取的。
    return render_template(
        'login.html',
        title='Sign In',
        form=form,
        providers=Config.OPENID_PROVIDERS
    )  # OpenID 认证异步发生。如果认证成功的话,Flask-OpenID 将会调用一个注册了 oid.after_login 装饰器的函数。如果失败的话,用户将会回到登陆页面。
Example #9
0
File: user.py Project: Paddyy/flask
def login():
    form = LoginForm()
    # post才能进
    if form.validate_on_submit():  # 此段代码可优化,不够简洁
        #判断用户存不存在
        u = User.query.filter_by(username=form.username.data).first()
        if not u:
            flash('亲,账号或密码输错了呀~')
            return render_template('user/login.html', form=form)
        #存在在判断密码是否相等,用models里边的方法验证
        if not u.verify_password(form.password.data):
            flash('亲,账号或密码输错了呀~')
        #登录写入session
        # 此处需要用到第三方包pip install flask-login,并且在ext进行初始化,
        # 并且需要一个认证的回调函数,写在user model中
        login_user(u, remember=form.remenber.data)  #需要导入,然后看底层的东西, 记住我
        #登录成功返回首页,或者之前野蛮
        return redirect(request.args.get('next') or url_for('main.index'))
    return render_template('user/login.html', form=form)