package org.holer.server.interceptor;

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

import org.holer.server.constant.HolerCode;
import org.holer.server.model.HolerResult;
import org.holer.server.util.ServerUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class ApiInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest req, HttpServletResponse rep, Object handler) throws Exception {
        if (!ServerUtil.isValidToken(req)) {
            ServerUtil.write(rep, new HolerResult(HolerCode.BAD_TOKEN));
            return false;
        }
        return true;
    }
}
