<reponame>Mihaaai/RealEstateMarketplace
from ..forms import LoginForm

from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login


def LoginView(request):
    context = {}

    if request.method == 'GET':
        form = LoginForm()

    elif request.method == 'POST':
        form = LoginForm(request.POST)

        if form.is_valid():
            user = authenticate(email=form.cleaned_data['email'],
                                password=form.cleaned_data['password'])

            if user:
                login(request=request,
                      user=user)
                next_url = request.GET.get('next')
                if next_url:
                    return HttpResponseRedirect(next_url)
                else:
                    return redirect('list_listings')
            else:
                context['error'] = "The email or password are not correct. Please try again"

    context['form'] = form

    return render(request, 'login_template.html', context)

from ..forms import LoginForm

from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login


def LoginView(request):
    context = {}

    if request.method == 'GET':
        form = LoginForm()

    elif request.method == 'POST':
        form = LoginForm(request.POST)

        if form.is_valid():
            user = authenticate(email=form.cleaned_data['email'],
                                password=form.cleaned_data['password'])

            if user:
                login(request=request,
                      user=user)
                next_url = request.GET.get('next')
                if next_url:
                    return HttpResponseRedirect(next_url)
                else:
                    return redirect('list_listings')
            else:
                context['error'] = "The email or password are not correct. Please try again"

    context['form'] = form

    return render(request, 'login_template.html', context)