def signin():
    if current_user.is_authenticated:
        if current_user.admin:
            return redirect(url_for('main.users'))
        else:
            return redirect(url_for('main.homepage'))
    form = SignInForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is None:
            flash('Invalid username or password.')
            return redirect(url_for('auth.signin'))
        elif not user.active:
            flash(
                'User account "{}" is deactivated. Please contact Administrator'
                .format(user.username))
            return redirect(url_for('auth.signin'))
        if user.check_password(form.password.data):
            access_token = create_access_token(identity=form.username.data)
            refresh_token = create_refresh_token(identity=form.username.data)
            login_user(user)
            if current_user.admin:
                resp = make_response(redirect(url_for('main.users')))
            else:
                resp = make_response(redirect(url_for('main.homepage')))
            set_access_cookies(resp, access_token)
            set_refresh_cookies(resp, refresh_token)
            return resp
        else:
            return {'message': 'Wrong credentials'}
    return render_template('auth/signin.html', title='Sign In', form=form)
Example #2
0
def sign_in():
    form = SignInForm()
    if form.validate_on_submit():
        email = form.email.data
        password = form.password.data

        #authenticate the user
        try:
            user = User.auth(email, password)
            login_user(user, remember=True)

            # Sign in successful
            flash(
                'User {}, logged in with id={}'.format(current_user.email,
                                                       current_user.id),
                'blue')
            return redirect(url_for('main.index'))

        except Exception as e:
            # Sign in unsuccessful
            error_json = e.args[1]
            error = json.loads(error_json)['error']['message']
            flash("Error: {}".format(error), 'red')

            return render_template('auth/sign_in.html',
                                   title='Sign In',
                                   form=form)

    return render_template('auth/sign_in.html', title='Sign In', form=form)
Example #3
0
def login():
    form = SignInForm()
    if form.validate_on_submit():
        user = User.query.filter_by(email=form.email.data).first_or_404()
        if user.is_correct_password(form.password.data):
            login_user(user)

            next = request.args.get('next')
            return redirect(next or url_for('home.index'))
        else:
            return redirect(url_for('auth.login'))

    return render_template('auth/login.html', form=form)
Example #4
0
def sign_in():
    signInForm = SignInForm()
    if signInForm.validate_on_submit():
        if verify_psw(psw=signInForm.password.data,
                      email=signInForm.email.data):
            if is_user_confirmed(email=signInForm.email.data):
                login_user(get_profile_from_db(signInForm.email.data))
                return redirect(url_for('users.dashboard'))
            else:
                signInForm.password.errors.append(
                    'Your profile is not yet verified! Check your email and follow instructions'
                )
        else:
            signInForm.password.errors.append('Wrong password!')
    return render_template('signin.html', form=signInForm, title='Sign In')
Example #5
0
def sign_in():
    if current_user.is_authenticated:
        set_visit(current_user)
        return redirect(url_for('main.index'))

    form = SignInForm()

    if form.validate_on_submit():
        user = User.query.filter_by(phone=form.phone.data).first()

        if user and user.check_password(form.password.data):
            # User login succesfully
            login_user(user, remember=form.remember_me.data)
            set_visit(user)
            return redirect(url_for('main.index'))

        flash("Invalid phone number or password")
    return render_template('auth/signin.html', form=form)
Example #6
0
def sign_in():
    form = SignInForm()

    if current_user.is_authenticated:
        redirect(url_for('main.index'))
    if request.method == 'GET':
        return render_template('auth/sign_in.html', form=form)
    else:
        if form.validate_on_submit():
            user = User.query.filter_by(username=form.username.data).first()
            if not user or not user.check_password(form.password.data):
                flash('Username or password is invalid.')
                return render_template('auth/sign_in.html', form=form)
            if not user.confirmed:
                flash('Please confirm email and then you can sign in.')
                return render_template('auth/sign_in.html', form=form)
            login_user(user, remember=form.remember_me.data)
            return redirect(url_for('main.index'))
        else:
            return render_template('auth/sign_in.html', form=form)