Example #1
0
def index(request):
    def errorHandle(error):
        form = LoginForm()
        return render(request, "game/index.html", {"error": error, "form": form})

        # If the user is already logged in, always go to map page

    if request.user.is_authenticated():
        return HttpResponseRedirect("/map/")

    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():

            u = request.POST["uname"]
            p = request.POST["password"]

            user = authenticate(username=u, password=p)
            if user is not None:
                login(request, user)
                return HttpResponseRedirect("/map/")

            else:
                return errorHandle(u"Sorry, Password and or Username is incorrect. Ask your teacher for help.")

    else:
        form = LoginForm()

    return render(request, "game/index.html", {"form": form})
Example #2
0
def login_view(request):
    """
    Login page.

    :param request:
    :return:
    """
    context = dict()
    if request.method == 'POST':
        form = LoginForm(request.POST)
        try:
            if form.is_valid():
                user = authenticate(username=form.cleaned_data['username'], password=PASSWORD)
                login(request, user)
                redirect_url = request.POST.get('next', reverse("game:index"))
                return HttpResponseRedirect(unquote_redirect_url(redirect_url))
            else:
                context['next'] = unquote_redirect_url(request.GET.get('next', reverse("game:index")))
        except:
            context['next'] = unquote_redirect_url(request.GET.get('next', reverse("game:index")))

    else:
        context['next'] = request.GET.get('next', '')
        form = LoginForm()
    context['form'] = form
    # context['path'] = settings.DOMAIN
    return render(request, 'login.html', context)
Example #3
0
def login_view(request):
    form = LoginForm(request.POST or None)
    if request.POST and form.is_valid():
        user = form.login(request)
        if user:
            login(request, user)
            return redirect(play2)# Redirect to a success page.
    return render(request, 'login.html', {'login_form': form })
Example #4
0
def login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            identifier = request.POST['identifier']
            password = request.POST['password']
            if User.objects.filter(username=identifier).exists():
                username = identifier
            elif User.objects.filter(mturk=identifier).exists():
                username = User.objects.filter(mturk=identifier)[0]
            else:
                form.add_error('identifier', "Username does not exist")
                return render(request, 'login.html', {'form': form})
            user = authenticate(username=username, password=password)
            if user:  # and user.is_active:
                auth_login(request, user)
                return redirect(reverse('home'))
            else:
                form.add_error('password', "Password incorrect")
                return render(request, 'login.html', {'form': form})
    else:
        form = LoginForm()
        if 'next' in request.GET:
            context = {'form': form, 'next': request.GET['next']}
        # todo: added "logged out" message
        # todo: added "password reset" message
        else:
            context = {'form': form}
        return render(request, 'login.html', context)
Example #5
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.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')
            flash(f'Welcome back, {form.username.data}!', 'success')
            return redirect(next_page) if next_page else redirect(url_for('lobby'))
        else:
            flash(f'Login unsuccessful. Please check username and password', 'danger')
    return render_template('login.html', form=form)
Example #6
0
def index(request):
    player = get_player(request)
    if player is not None:
        # Delete old player and game objects
        if player.game and player == player.game.master:
            player.game.delete()
        player.delete()
        del request.session['player_pk']

    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['player_name']
            create_game = form.cleaned_data['create_game']

            checkin = CheckIn()
            checkin.save()
            player = Player(name=name, rand_id=Player.generate_rand_id(), checkin=checkin)

            if create_game:
                checkin = CheckIn()
                checkin.save()
                bot = Bot()
                bot.save()
                game = Game(checkin=checkin, bot=bot)
                game.save()
                player.game = game
                player.index = 1

            player.save()
            
            if create_game:
                game.master = player
                game.players.add(player)
                game.save()

            request.session['player_pk'] = player.pk
            request.session['player_rand_id'] = player.rand_id
            request.session['player_name'] = name

            if create_game:
                return redirect('/create-game')
            else:
                return redirect('/join-game')
    else:
        form = LoginForm(initial={'player_name': request.session.get('player_name', '')})

    return render(request, 'game/index.html', {'form': form})
Example #7
0
def user_login(request):
    context = {}
    if request.method == 'GET':
        context['form'] = LoginForm()
        return render(request, 'game/login.html', context)

    form = LoginForm(request.POST)
    context['form'] = form

    if not form.is_valid():
        return render(request, 'game/login.html', context)

    user = authenticate(username=form.cleaned_data['username'],
                        password=form.cleaned_data['password'])
    login(request, user)
    return redirect(reverse('home'))
Example #8
0
def user_login(request):
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(username=cd['username'], password=cd['password'])
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect('index')
                else:
                    messages.info(request,'Invalid username or password')
            else:
                messages.info (request, 'Invalid username or password')
    else:
        form = LoginForm()
    return render(request, 'game/login.html', {'form' : form})
Example #9
0
def login():
    error = None
    # Imports the login form from the file forms.py
    form = LoginForm(request.form)

    # When the form is submitted:
    if request.method == 'POST':
        if form.validate_on_submit():

            # Input that needs to be validated
            name = request.form['username']
            passwd = request.form['password']

            # Check that the username is a alphanumeric string
            # between 4 and 25 characters long (whitelist)
            if not re.search("^[0-9a-zA-Z]{4,25}$", name):
                flash('Invalid credentials. Please try again.')
                return redirect(url_for('auth.login'))

            # Check that the password is a alphanumeric string
            # between 6 and 40 characters long (whitelist)
            if not re.search("^[0-9a-zA-Z]{6,40}$", passwd):
                flash('Invalid credentials. Please try again.')
                return redirect(url_for('auth.login'))

            # Introduced user is searched in the DB
            user = User.query.filter_by(username=name).first()

            # Check if username and password are correct
            if user is not None and bcrypt.check_password_hash(
                    user.password, passwd):

                # Login user (with login extension)
                login_user(user)

                # Redirect to home
                flash('You were just logged in!')
                return redirect(url_for('info.home'))

            else:
                error = 'Invalid credentials. Please try again.'

    return render_template('auth/login.html', form=form, error=error)
Example #10
0
def login(request):
    form = LoginForm(request.POST)
    if form.is_valid():
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            user = None
        if user is not None and user.check_password(password):
            return JsonResponse(user.profile.get_profile_dict(user),
                                content_type="application/json")
        else:
            return JsonResponse(
                {'errors': {
                    'email': ['Email або ж пароль не вірний!']
                }},
                status=401)
    else:
        return JsonResponse(form.errors,
                            content_type="application/json",
                            status=400,
                            safe=False)