github.com/kotovmak/go-admin@v1.1.1/plugins/admin/modules/guard/server_login.go (about)

     1  package guard
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  
     7  	"github.com/kotovmak/go-admin/context"
     8  	"github.com/kotovmak/go-admin/modules/logger"
     9  )
    10  
    11  type ServerLoginParam struct {
    12  	Account  string
    13  	Password string
    14  }
    15  
    16  func (g *Guard) ServerLogin(ctx *context.Context) {
    17  
    18  	var p ServerLoginParam
    19  
    20  	body, err := ioutil.ReadAll(ctx.Request.Body)
    21  
    22  	if err != nil {
    23  		logger.Error("get server login parameter error: ", err)
    24  	}
    25  
    26  	err = json.Unmarshal(body, &p)
    27  
    28  	if err != nil {
    29  		logger.Error("unmarshal server login parameter error: ", err)
    30  	}
    31  
    32  	ctx.SetUserValue(serverLoginParamKey, &p)
    33  	ctx.Next()
    34  }
    35  
    36  func GetServerLoginParam(ctx *context.Context) *ServerLoginParam {
    37  	return ctx.UserValue[serverLoginParamKey].(*ServerLoginParam)
    38  }