|
|
|
@ -17,6 +17,8 @@ import (
|
|
|
|
|
connectMidleware "github.com/go-zoox/connect/pkg/middleware"
|
|
|
|
|
"github.com/rocboss/paopao-ce/internal/conf"
|
|
|
|
|
"github.com/rocboss/paopao-ce/internal/servants"
|
|
|
|
|
"github.com/rocboss/paopao-ce/internal/servants/base"
|
|
|
|
|
"github.com/rocboss/paopao-ce/pkg/app"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
@ -52,9 +54,65 @@ func newWebEngine() *gin.Engine {
|
|
|
|
|
|
|
|
|
|
e.Use(connectMidleware.CreateGinMiddleware(os.Getenv("SECRET_KEY")))
|
|
|
|
|
e.Use(func(ctx *gin.Context) {
|
|
|
|
|
v, _ := ctx.Get(connectMidleware.ContextUserKeyForGin)
|
|
|
|
|
u, ok := ctx.Get(connectMidleware.ContextUserKeyForGin)
|
|
|
|
|
if !ok {
|
|
|
|
|
ctx.JSON(http.StatusUnauthorized, gin.H{
|
|
|
|
|
"message": "unauthorized (1)",
|
|
|
|
|
})
|
|
|
|
|
ctx.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
connectUser, ok := u.(*connectMidleware.User)
|
|
|
|
|
if !ok {
|
|
|
|
|
ctx.JSON(http.StatusUnauthorized, gin.H{
|
|
|
|
|
"message": "unauthorized (2)",
|
|
|
|
|
})
|
|
|
|
|
ctx.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("user:", v)
|
|
|
|
|
fmt.Println("connect user:", connectUser)
|
|
|
|
|
dao := base.NewDaoServant()
|
|
|
|
|
user, err := dao.Ds.GetOrCreateUserByEmail(connectUser.Email, connectUser)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
"message": fmt.Sprintf("failed to get user by email: %s", err),
|
|
|
|
|
})
|
|
|
|
|
ctx.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// auth login
|
|
|
|
|
if ctx.Request.URL.Path == "/v1/auth/login" {
|
|
|
|
|
token, err := app.GenerateToken(user)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
|
"message": "failed to generate token",
|
|
|
|
|
})
|
|
|
|
|
ctx.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"code": 0,
|
|
|
|
|
"msg": "success",
|
|
|
|
|
"data": gin.H{
|
|
|
|
|
"token": token,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
ctx.Abort()
|
|
|
|
|
return
|
|
|
|
|
} else if ctx.Request.URL.Path == "/v1/user/info" {
|
|
|
|
|
// user info
|
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
|
|
|
"code": 0,
|
|
|
|
|
"msg": "success",
|
|
|
|
|
"data": user,
|
|
|
|
|
})
|
|
|
|
|
ctx.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.Next()
|
|
|
|
|
})
|
|
|
|
|