/*
 * Copyright 2014-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.docksidestage.app.web.signin;

import javax.annotation.Resource;

import org.docksidestage.app.web.base.HarborBaseAction;
import org.docksidestage.app.web.base.login.HarborLoginAssist;
import org.docksidestage.app.web.mypage.MypageAction;
import org.docksidestage.mylasta.action.HarborMessages;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.HtmlResponse;

/**
 * @author jflute
 */
public class SigninAction extends HarborBaseAction {

    // ===================================================================================
    //                                                                           Attribute
    //                                                                           =========
    @Resource
    private HarborLoginAssist harborLoginAssist;

    // ===================================================================================
    //                                                                             Execute
    //                                                                             =======
    @Execute
    public HtmlResponse index() {
        if (getUserBean().isPresent()) {
            return redirect(MypageAction.class);
        }
        return asHtml(path_Signin_SigninJsp).useForm(SigninForm.class);
    }

    @Execute
    public HtmlResponse signin(SigninForm form) {
        validate(form, messages -> moreValidate(form, messages), () -> {
            form.clearSecurityInfo();
            return asHtml(path_Signin_SigninJsp);
        });
        return harborLoginAssist.loginRedirect(form.email, form.password, op -> op.rememberMe(form.rememberMe), () -> {
            return redirect(MypageAction.class);
        });
    }

    private void moreValidate(SigninForm form, HarborMessages messages) {
        if (isNotEmpty(form.email) && isNotEmpty(form.password)) {
            if (!harborLoginAssist.checkUserLoginable(form.email, form.password)) {
                messages.addErrorsLoginFailure("email");
            }
        }
    }
}
@@ -1003,16 +1003,65 @@
 esponse;
+%0Aimport org.lastaflute.web.response.JsonResponse;
 %0A%0A/**%0A *
@@ -2706,9 +2706,418 @@
 %7D%0A    %7D%0A
+%0A    @Execute%0A    public JsonResponse%3CObject%3E indexJson(SigninForm form) %7B%0A        validate(form, messages -%3E moreValidate(form, messages), () -%3E %7B%0A            form.clearSecurityInfo();%0A            return JsonResponse.asEmptyBody().httpStatus(400);%0A        %7D);%0A        harborLoginAssist.login(form.email, form.password, op -%3E op.rememberMe(form.rememberMe));%0A        return JsonResponse.asEmptyBody();%0A    %7D%0A%0A
 %7D

add Json Response