<gh_stars>0
from django.conf import settings
from django.http.response import HttpResponse
from django.utils.http import urlsafe_base64_decode
from django.utils.encoding import force_str
from django.contrib.auth import login, authenticate
from django.shortcuts import redirect
from utils.tokens import account_activation_token
from users.models import User


def activate(request, uidb64, token):
    try:
        uid = force_str(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except(TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None
    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.save()
        login(request, user, backend=settings.AUTHENTICATION_BACKENDS[0])
        response = redirect("http://localhost:4200")
        return response
    else:
        return HttpResponse('Activation link is invalid!')