package admin import ( "net/http" "github.com/gin-gonic/gin" "github.com/metarare/metarare_api/common" "github.com/metarare/metarare_api/helpers/gerror" "github.com/metarare/metarare_api/models" "gorm.io/gorm" ) type AdminAuthV1Router struct { group *gin.RouterGroup mDB *gorm.DB rDB *gorm.DB } func NewAdminAuthV1Router(r common.Router, basePath string) AdminAuthV1Router { a := AdminAuthV1Router{ group: r.Version.Group(basePath), mDB: r.Db.MasterDB, rDB: r.Db.ReadDB, } a.group.POST("signin", a.adminSignin) // a.group.POST("", a.registerAdmin) return a } // adminSignin godoc // @Summary signin // @Description 어드민 로그인 // @Schemes // @Tags admin // @Accept json // @Produce json // @Param AccountInfo body AccountInfo true "body struct" // @Success 200 {object} AdminAuthContainer // @Router /admin/auth/signin [post] func (a AdminAuthV1Router) adminSignin(c *gin.Context) { request := AccountInfo{} if err := c.ShouldBindJSON(&request); err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } admin := models.Admin{} if err := a.rDB.Where("email = ? AND password = ? AND status = 'stable'", request.Email, request.Password).Find(&admin).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } else if admin.ID == 0 { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.NotFoundRecord, nil, err) return } authenitcation := AdminAuthContainer{} if err := a.rDB.Where("admin_id = ?", admin.ID).Find(&authenitcation.Permission).Error; err != nil { gerror.IntegratedResponseToRequest(c, http.StatusBadRequest, gerror.InvalidParameterValue, nil, err) return } standardClaims, customClaims := common.MakeClaims(int64(admin.ID)) _, authContainer := common.MakeSession(standardClaims, customClaims) authenitcation.AccessToken = authContainer.AccessToken gerror.IntegratedResponseToRequest(c, http.StatusOK, gerror.OK, authenitcation, nil) return }