This commit is contained in:
Jay
2018-09-02 09:15:27 +08:00
parent 3b7aa71338
commit 521607ff89
4 changed files with 90 additions and 7 deletions
+12 -1
View File
@@ -3,6 +3,8 @@ package api
import (
"git.trj.tw/golang/mtfosbot/model"
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/module/utils"
"github.com/gin-gonic/contrib/sessions"
"golang.org/x/crypto/bcrypt"
)
@@ -20,7 +22,7 @@ func UserLogin(c *context.Context) {
acc, err := model.GetAccount(bodyArg.Account)
if err != nil {
c.ServerError(nil)
c.ServerError(`account or password error`)
return
}
@@ -30,4 +32,13 @@ func UserLogin(c *context.Context) {
return
}
accInt := utils.ToMap(acc)
delete(accInt, "password")
session := sessions.Default(c.Context)
session.Set("user", accInt)
session.Set("loginType", "system")
session.Save()
c.Success(nil)
}
+19
View File
@@ -1,19 +1,33 @@
package routes
import (
"log"
"git.trj.tw/golang/mtfosbot/module/context"
"git.trj.tw/golang/mtfosbot/router/api"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
// NewServ - create new gin server
func NewServ() *gin.Engine {
r := gin.New()
store, err := sessions.NewRedisStore(10, "tcp", "localhost:6379", "")
if err != nil {
log.Fatal(err)
}
// access log
r.Use(gin.Logger())
// error catch
r.Use(gin.Recovery())
// enable cors
r.Use(cors.Default())
// session
r.Use(sessions.Sessions("gin:sess", store))
return r
}
@@ -24,4 +38,9 @@ func SetRoutes(r *gin.Engine) {
"message": "ok",
})
})
apiGroup := r.Group("/api")
{
apiGroup.POST("/login", context.PatchCtx(api.UserLogin))
}
}