package com.lion.lionwebsite.Interceptor;

import com.lion.lionwebsite.Dao.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


@Component
public class TaskHandlerInterceptor implements HandlerInterceptor {

    @Resource
    UserMapper userMapper;

    String[] AuthCodes = null;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        if(AuthCodes == null)
            AuthCodes = userMapper.selectAllAuthCode();

        String auth = request.getParameter("AuthCode");

        if(auth != null)
            for(String AuthCode: AuthCodes)
                if(auth.equals(AuthCode))
                    return true;

        return false;
    }

    public void updateAuthCodes(){
        AuthCodes = userMapper.selectEnableAuthCode();
    }
}
