from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.urls import reverse

def index(request):
    state = ""
    if request.user.is_authenticated:
        return redirect(reverse('main:index'))
    else:
        if request.method == "POST":
            if not request.POST.get("login") == None:
                username =  request.POST.get("inputEmail")
                password =  request.POST.get("inputPassword")
                user = authenticate(username=username, password=password)
                if user is not None:
                    login(request, user)
                    return redirect(reverse('main:index'))
                else:
                    state = "Fehler bei der Anmeldung!"
                    return render(request, 'login/login.html', {'state':state})

    return render(request, 'login/login.html')