@* Remove this section if you are using bundling *@
@section Scripts {
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}

@{
    Layout = "~/_SiteLayout.cshtml";
    Page.Title = "Log in";

    // Initialize general page variables
    var email = "";
    var password = "";
    var rememberMe = false;

    var returnUrl = Request.QueryString["ReturnUrl"];
    if (returnUrl.IsEmpty()) {
        // Some external login providers always require a return URL value
        returnUrl = Href("~/");
    }

    // Setup validation
    Validation.RequireField("email", "You must specify an email address.");
    Validation.RequireField("password", "You must specify a password.");
    Validation.Add("password",
        Validator.StringLength(
            maxLength: Int32.MaxValue,
            minLength: 6,
            errorMessage: "Password must be at least 6 characters"));

    // If this is a POST request, validate and process data
    if (IsPost) {
        AntiForgery.Validate();
        // is this an external login request?
        string provider = Request.Form["provider"];
        if (!provider.IsEmpty()) {
            OAuthWebSecurity.RequestAuthentication(provider, Href("~/Account/RegisterService", new { returnUrl }));
            return;
        } else if (Validation.IsValid()) {
            email = Request.Form["email"];
            password = Request.Form["password"];
            rememberMe = Request.Form["rememberMe"].AsBool();

            if (WebSecurity.UserExists(email) && WebSecurity.GetPasswordFailuresSinceLastSuccess(email) > 4 && WebSecurity.GetLastPasswordFailureDate(email).AddSeconds(60) > DateTime.UtcNow) {
                Response.Redirect("~/Account/AccountLockedOut");
                return;
            }

            // Attempt to log in using provided credentials
            if (WebSecurity.Login(email, password, rememberMe)) {
                Context.RedirectLocal(returnUrl);
                return;
            } else {
                ModelState.AddFormError("The user name or password provided is incorrect.");
            }
        }
    }
}

<hgroup class="title">
    <h1>@Page.Title.</h1>
</hgroup>

<section id="loginForm">
    <h2>Use a local account to log in.</h2>
    <form method="post">
        @AntiForgery.GetHtml()
        @* If one or more validation errors exist, show an error *@
        @Html.ValidationSummary("Log in was unsuccessful. Please correct the errors and try again.", excludeFieldErrors: true, htmlAttributes: null)

        <fieldset>
            <legend>Log in to Your Account</legend>
            <ol>
                <li class="email">
                    <label for="email" @if (!ModelState.IsValidField("email"))
                                       {<text>class="error-label"</text>}>Email address</label>
                    <input type="text" id="email" name="email" value="@email" @Validation.For("email")/>
                    @* Write any user name validation errors to the page *@
                    @Html.ValidationMessage("email")
                </li>
                <li class="password">
                    <label for="password" @if (!ModelState.IsValidField("password")) {<text>class="error-label"</text>}>Password</label>
                    <input type="password" id="password" name="password" @Validation.For("password")/>
                    @* Write any password validation errors to the page *@
                    @Html.ValidationMessage("password")
                </li>
                <li class="remember-me">
                    <input type="checkbox" id="rememberMe" name="rememberMe" value="true" checked="@rememberMe" />
                    <label class="checkbox" for="rememberMe">Remember me?</label>
                </li>
            </ol>
            <input type="submit" value="Log in" />
        </fieldset>
    </form>
    <p>
        <a href="~/Account/Register">Don't have a Account?</a>
        <a href="~/Account/ForgotPassword">Did you forget your password?</a>
    </p>
</section>

<section class="social" id="socialLoginForm">
    <h2>Use another service to log in.</h2>
    @RenderPage("~/Account/_ExternalLoginsList.cshtml")
</section>
