Subversion Repositories SmartDukaan

Rev

Rev 21598 | Rev 21987 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.web.model.FofoDetails;
import com.spice.profitmandi.web.util.CookiesFetcher;
import com.spice.profitmandi.web.util.GoogleLoginUtil;
import com.spice.profitmandi.web.util.MVCResponseSender;

@Controller
public class LoginController {

        private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
        
        @Autowired
        GoogleLoginUtil googleLoginUtil;
        
        @Autowired
        MVCResponseSender mvcResponseSender;
        
        @Autowired
        CookiesFetcher cookiesFetcher;
        
        @RequestMapping(value = "/login", method = RequestMethod.GET)
        public String loginPage(HttpServletRequest request, @ModelAttribute("model") ModelMap model) throws Exception{
                try{
                        cookiesFetcher.getCookiesObject(request);
                        LOGGER.info("Request session is already exist, should be redirect to /dashboard");
                        return "redirect:/dashboard";
                }catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
                        return "login";
                }
        }
        
        @RequestMapping(value = "/login", method = RequestMethod.POST)
        public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
                try{
                        FofoDetails fofoDetails = googleLoginUtil.getFofoDetail(token);
                        Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
                        cookieFofoId.setDomain("localhost");
                        cookieFofoId.setPath(request.getContextPath());
                        Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
                        cookieEmailId.setDomain("localhost");
                        cookieEmailId.setPath(request.getContextPath());
                        response.addCookie(cookieFofoId);
                        response.addCookie(cookieEmailId);
                        LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard"));
                        return "response";
//                      return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
                }catch(ProfitMandiBusinessException profitMandiBusinessException){
                        LOGGER.error("Error : ", profitMandiBusinessException);
                        model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
                        return "response";
                }
        }
        
}