package middleware

import (
	"log/slog"
	"slgserver/constant"
	"slgserver/net"
)

func CheckLogin() net.MiddlewareFunc {
	return func(next net.HandlerFunc) net.HandlerFunc {
		return func(req *net.WsMsgReq, rsp *net.WsMsgRsp) {

			_, err := req.Conn.GetProperty("uid")
			if err != nil {
		slog.Warn("connect not found uid",
			"msgName", req.Body.Name)
				rsp.Body.Code = constant.UserNotInConnect
				req.Conn.Push("account.pleaseLogin", nil)
				return
			}

			next(req, rsp)
		}
	}
}