/*
 * This file is part of EasyExchange.
 *
 * (c) 2014 - Machiel Molenaar <machiel@machiel.me>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

package com.mtech.easyexchange.mvc.user;

import com.mtech.easyexchange.annotation.ActiveUser;
import com.mtech.easyexchange.model.User;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String index(ModelMap map) {

        return "user/login";

    }

    @RequestMapping(value = "/login/failed", method = RequestMethod.GET)
    public String loginFailed(ModelMap model, HttpSession session) {

        model.addAttribute("error", true);
        model.addAttribute("session", session.getAttribute("SPRING_SECURITY_LAST_EXCEPTION"));

        return "user/login";
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String welcome(@ActiveUser User user, ModelMap map) {

        map.addAttribute("user", user);

        return "user/welcome";

    }

}
