<commit_before>﻿using Blog.Models;
using Blog.Models.Account;
using Blog.Models.Data;
using Blog.ViewModels.Account;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.Controllers
{
    sealed public class AuthController : Controller
    {
        private SignInManager<BlogUser> _signInManager { get; }

        public AuthController(SignInManager<BlogUser> signInManager)
        {
            _signInManager = signInManager;
        }

        public IActionResult Login()
        {
            if(User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Blog");
            }
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Login(LoginViewModel viewModel) 
        {
            if(ModelState.IsValid)
            {

                var user = ModelFactory.Create(viewModel);

                var signInResult = await _signInManager
                    .PasswordSignInAsync( user.UserName, 
                                          user.Password,
                                          true, 
                                          false );

                if ( signInResult.Succeeded )
                {
                    return RedirectToAction("Index", "Blog");
                }
                else
                {
                    ModelState.AddModelError("", "Username or password incorrect");
                }

            }
            return View();
        }

        public async Task<IActionResult> Logout()
        {
            if(User.Identity.IsAuthenticated)
            {
                await _signInManager.SignOutAsync();
            }
            return RedirectToActionPermanent("Index", "Blog");
        }
    }
}<commit_msg>Added auth by email and username. Added verification.<commit_after>﻿using Blog.Models;
using Blog.Models.Account;
using Blog.Models.Data;
using Blog.ViewModels.Account;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.Controllers
{
    sealed public class AuthController : Controller
    {
        private UserManager<BlogUser> userManager { get; }
        private SignInManager<BlogUser> signInManager { get; }

        public AuthController(SignInManager<BlogUser> signInManager, UserManager<BlogUser> userManager)
        {
            this.signInManager = signInManager;
            this.userManager   = userManager;
        }

        public IActionResult Login()
        {
            if(User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Blog");
            }
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Login(LoginViewModel viewModel) 
        {
            if (ModelState.IsValid)
            {
                //get an existing user by name or email
                var user  = await userManager.FindByNameAsync(viewModel.UserName);
                    user  = await userManager.FindByEmailAsync(viewModel.UserName); 

                //if user exist
                if (user != null) {

                    if (user.isBanned)
                    {
                        ModelState.AddModelError("", $"Your account has been temporarily suspended until {user.LockoutEnd}");
                        return View();
                    }

                    var signInResult = await signInManager
                        .PasswordSignInAsync(user,
                                             viewModel.Password,
                                             viewModel.RememberMe,
                                             false);

                    if (signInResult.Succeeded)
                    {
                        return RedirectToAction("Index", "Blog");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Password is incorrect");
                        return View();
                    }

                }
            }

            ModelState.AddModelError("", "Username or email incorrect");
       
            return View();
        }

        public async Task<IActionResult> Logout()
        {
            if(User.Identity.IsAuthenticated)
            {
                await signInManager.SignOutAsync();
            }
            return RedirectToActionPermanent("Index", "Blog");
        }
    }
}
{'fraction_non_alphanumeric': 0.07823643829695266, 'fraction_numerical': 0.0, 'mean_word_length': 1.3709016393442623, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1679029', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1049, 'n_words': 226}