Exemplo n.º 1
0
def login():
    '''
        login 
        1. check validity of email such as [email protected] etc...
        2. via digest to compare user input password and password in the DB
    '''
    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 not user:
            flash('Login Failed,Please Check Your Email', 'danger')
            return render_template('login.html', title='Login', form=form)
        '''
            To compare hashpassword (backend and user-input)
        '''
        hashed_password = bcrypt.generate_password_hash(user.password)
        if bcrypt.check_password_hash(hashed_password, form.password.data):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
            #return '<p>White Test two Digests are The Same</p>'
        else:
            #return 'White Test two Digests are not The Same'
            flash('Login Failed,Please check your Password', 'danger')
    return render_template('login.html', title='Login', form=form)
Exemplo n.º 2
0
def login():
    print('gate called')
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = LoginForm()
    print(form.__dict__)
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        print(f'Login user {user}')
        if user is None or not user.check_password(
                form.password.data
        ):  # If user does not exist or has wrong username and pass
            flash('Invalid username or password')
            return redirect(url_for('login'))
        else:  # If user exist and has the correct username and pass
            user.last_login = datetime.utcnow()
            session.commit()
            print('user last login!', user.last_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')
            if not is_safe_url(next_page):
                return app.abort(400)
            print(f'next page: {next_page}')
            return redirect(next_page)
    return render_template('gate.html', title='Login', form=form)
Exemplo n.º 3
0
def login(request):
    # A HTTP POST?
    if request.method == 'POST':
        form = LoginForm(request.POST)

        # cleaned_data[]
        # Have we been provided with a valid form?
        if (form.is_valid() and User.objects.filter(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password']).exists()):
            # Save the new category to the database.
            context_dict = {'boldmessage': "Usuario logueado correctamente"}
            return render(request, 'RatonGato/index.html', context_dict)
        else:
            context_dict = {
                'boldmessage': "Nombre de usuario o contrasenya incorrecta."
            }
            # The supplied form contained errors - just print them to the terminal.
            render(request, 'RatonGato/login.html', context_dict)
    else:
        # If the request was not a POST, display the form to enter details.
        form = LoginForm()

    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    return render(request, 'RatonGato/login.html', {'form': form})
def login_view(request):
    # Authentication check. Users currently logged in cannot view this page.
    if request.user.is_authenticated:
        return HttpResponseRedirect('/profile/')
    elif Account.objects.all().count() == 0:
        return HttpResponseRedirect('/setup/')
    # get template data from session
    template_data = views.parse_session(request, {'form_button': "Login"})
    # Proceed with the rest of view
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            user = authenticate(username=form.cleaned_data['email'].lower(),
                                password=form.cleaned_data['password'])
            userInfo = Account.objects.get(user=user)
            if userInfo.archive == False:
                login(request, user)
                logger.log(Action.ACTION_ACCOUNT, "Account login",
                           request.user.account)
                request.session[
                    'alert_success'] = "Successfully logged into VirtualClinic."
                return HttpResponseRedirect('/profile/')
            else:
                request.session[
                    'alert_danger'] = "Account is archived! Please create a new account"
                return HttpResponseRedirect('/register/')
    else:
        form = LoginForm()
    template_data['form'] = form
    return render(request, 'virtualclinic/login.html', template_data)
Exemplo n.º 5
0
def login():
    form = LoginForm()

    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).one()
        login_user(user)

        flash("Logged in successfully.", "success")
        return redirect(request.args.get("next") or url_for(".home"))

    return render_template("login.html", form=form)
Exemplo n.º 6
0
def login():
    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):
            login_user(user, remember=form.remember.data)
            flash("You have been logged in successfully", 'success')
            return redirect(url_for('main'))
        else:
            flash("Login Unsuccessful,Please check your email and password",
                  'danger')
    return render_template('login.html', title="Login", form=form)
Exemplo n.º 7
0
def login():
    # if g.user is not None:
    #     return redirect(url_for('home'))
    form = LoginForm()
    if request.method == 'POST':
        if form.validate_on_submit() and check_login(form.nick.data):
            session['nickname'] = form.nick.data
            flash(form.nick.data+"login successfuly")
            return redirect('/home')
    return render_template('login.html',
                           title='Login',
                           error='Please input an unused nickname!',
                           form=form)
Exemplo n.º 8
0
def login():
    # if g.user is not None:
    #     return redirect(url_for('home'))
    form = LoginForm()
    if request.method == 'POST':
        if form.validate_on_submit() and check_login(form.nick.data):
            session['nickname'] = form.nick.data
            flash(form.nick.data + "login successfuly")
            return redirect('/home')
    return render_template('login.html',
                           title='Login',
                           error='Please input an unused nickname!',
                           form=form)
Exemplo n.º 9
0
def login():
    form = LoginForm()
    if request.method == 'POST':
        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):
                return jsonify({'message': 'Invalid password or username'})
            login_user(user)
            return jsonify({'message': 'logged in'})
    if request.method == 'GET':
        if current_user.is_authenticated:
            return render_template('index.html')
        return render_template('login.html', form=form)
Exemplo n.º 10
0
def index():
    form = LoginForm()
    if request.method == 'GET':
        if not current_user.is_authenticated:
            return render_template('index.html', form=form)
        else:
            return redirect(url_for('landing_page'))
    elif form.validate_on_submit():
        print(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):
            return jsonify({'message': 'Invalid password or username'})
        login_user(user)
        return redirect(url_for('landing_page'))
Exemplo n.º 11
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)
Exemplo n.º 12
0
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        username = form.username.data
        password = form.password.data
        user_doc = db.find_user(username)
        passwords_match = check_password_hash(user_doc['password'], password)

        if user_doc and passwords_match:
            session['user'] = username
            return redirect(url_for('messages'))
        else:
            flash("Грешно потребителско име или парола")
            return render_template('login.html', form=form)
    return render_template('login.html', form=form)
Exemplo n.º 13
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('blog'))
    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):
            flash(f'Welcome {user.username}!', 'success')
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('blog'))
        else:
            flash('Login failed, check email or password', 'danger')
    return render_template('login.html', title="Login", form=form)
Exemplo n.º 14
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):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
        else:
            flash('Login Unsuccessful. Please check username and password',
                  'danger')
    return render_template('login.html', title='Login', form=form)
Exemplo n.º 15
0
def admin_login():
    form = LoginForm(request.form)
    if form.validate_on_submit():
        user = User.query.filter_by(id=form.username.data).first()
        if user and check_password_hash(user.password, form.password.data):
            # Keep the user info in the session using Flask-Login
            login_user(user, remember=form.remember_me.data)

            next = request.args.get('next')
            #if not is_safe_url(next):
            #    return abort(400)
            #return redirect(next or url_for('profile.landing_page'))
            return redirect(prefix + '/admin')

        pass

    return 'validation failed'
Exemplo n.º 16
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):
            login_user(user, remember=form.remember.data)
            next_page = request.args.get('next')
            return redirect(next_page) if next_page else redirect(
                url_for('home'))
        else:
            flash(
                'Авторизация не прошла успешно. Пожалуйста, проверьте свой логин и пароль',
                'danger')
    return render_template('login.html', title='Вход', form=form)
Exemplo n.º 17
0
 def test_valid_login_form(self):
     """Test login form validation with valid user"""
     with self.flaskapp.test_request_context():
         form = LoginForm.from_json({
             'username': '******',
             'password': '******'
         })
         assert form.validate() is True
Exemplo n.º 18
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('my_page'))
    form = LoginForm()
    if form.validate_on_submit():
        user = Customer.query.filter_by(email=form.email.data).first()
        if user is None:
            flash("Användaren med denna mailadress finns inte.")
            return redirect(url_for('login'))
        if not user.check_password(form.password.data):
            flash('Lösenordet matchar inte mailadressen.')
            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 != '':
            # print("\n\welcome\n\n")
            next_page = url_for('welcome')
        return redirect(next_page)
    return render_template('login.html', title='Logga in', form=form)
Exemplo n.º 19
0
 def test_nonexisting_user(self):
     """Test login form validation with nonexisting user"""
     with self.flaskapp.test_request_context():
         form = LoginForm.from_json({
             'username': '******',
             'password': '******'
         })
         assert form.validate() is False
         assert len(form.errors['username']) is 1
         assert form.errors['username'][0] == invalid_combo_message
def login():
    if session.get('email') is not None:
        return redirect(url_for('authy.twofa'))

    form = LoginForm(request.form)

    if form.validate_on_submit():
        email = form.email.data

        authy_id = utils.get_authy_id(email)
        if authy_id is not None:
            session['authy_id'] = authy_id
            session['email'] = email
            return redirect(url_for('authy.twofa'))
        else:
            flash("Please register", "error")
            return redirect(url_for("authy.register"))

    return render_template('login.html', form=form)
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.rememberme.data)
        flash("Logged in as " + user.name())

        # go to previous page if login used as interstitial, otherwise /
        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", form=form)
Exemplo n.º 22
0
def signin(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            try:
                username = form.cleaned_data.get('username')
                raw_password = form.cleaned_data.get('password')
                user = authenticate(username=username, password=raw_password)
                if user:
                    login(request, user)
                    return HttpResponseRedirect('/lk/')

                else:
                    return HttpResponse("403")
            except Exception as e:
                print(e)
                return HttpResponse("403")
    else:
        form = LoginForm()
    return render(request, 'login.html', {'form': form})
Exemplo n.º 23
0
 def test_invalid_password(self):
     """Test login form validation with invalid username/password
     combination"""
     with self.flaskapp.test_request_context():
         form = LoginForm.from_json({
             'username': '******',
             'password': '******'
         })
         assert form.validate() is False
         assert len(form.errors['username']) is 1
         assert form.errors['username'][0] == invalid_combo_message
def login_admin(request):
    form = LoginForm(request.POST or None)

    msg = None

    if request.method == "POST":

        if form.is_valid():
            username = form.cleaned_data.get("username")
            password = form.cleaned_data.get("password")
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_superuser:
                    login(request, user)
                    return redirect("/serverAPI/admin_control_panel/")
                else:
                    msg = 'Invalid user'
            else:
                msg = 'Invalid credentials'
        else:
            msg = 'Error validating the form'

    return render(request, "accounts/login.html", {"form": form, "msg": msg})
Exemplo n.º 25
0
 def test_invalid_empty_form(self):
     """Test login form validation with an empty form"""
     with self.flaskapp.test_request_context():
         form = LoginForm.from_json({})
         assert form.validate() is False
Exemplo n.º 26
0
 def test_invalid_missing_password(self):
     """Test login form validation with a form missing the password"""
     with self.flaskapp.test_request_context():
         form = LoginForm.from_json({'username': '******'})
         assert form.validate() is False
Exemplo n.º 27
0
 def index(self):
     form = LoginForm(request.form)
     return self.render('admin/home.html',
                        form=form,
                        login_url=url_for('admin_login'))