Skip to content

Commit

Permalink
fix a register event emit issue
Browse files Browse the repository at this point in the history
Signed-off-by: Toby Yan <me@tobyan.com>
  • Loading branch information
toby1991 committed May 10, 2019
1 parent 740d71b commit db09c5a
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions app/http/controllers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package controllers

import (
"errors"
"log"
"net/http"

"github.com/gin-gonic/gin"
"github.com/totoval/framework/hub"

"github.com/totoval/framework/config"
"github.com/totoval/framework/helpers"
"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/http/controller"
"github.com/totoval/framework/hub"
"github.com/totoval/framework/model/helper"
"github.com/totoval/framework/utils/crypt"
"github.com/totoval/framework/utils/jwt"

"totoval/app/events"
pbs "totoval/app/events/protocol_buffers"

"totoval/app/http/requests"
"totoval/app/models"
)
Expand Down Expand Up @@ -46,6 +46,7 @@ func (r *Register) Register(c *gin.Context) {
}()

var token string
var userId uint
m.Transaction(func(TransactionHelper *helper.Helper) {
// determine if exist
user := models.User{
Expand All @@ -63,20 +64,6 @@ func (r *Register) Register(c *gin.Context) {
panic(errors.New(helpers.L(c, "auth.register.failed_system_error")))
}

// emit user-registered event
ur := events.UserRegistered{}
param := &pbs.UserRegistered{
UserId: uint32(*user.ID),
AffiliationFromCode: "",
}
if requestData.AffiliationFromCode != nil {
param.AffiliationFromCode = *requestData.AffiliationFromCode
}
ur.SetParam(param)
if errs := hub.Emit(&ur); errs != nil {
panic(errors.New(helpers.L(c, "auth.register.failed_system_error")))
}

// create jwt
newJwt := jwt.NewJWT(config.GetString("auth.sign_key"))
username := ""
Expand All @@ -89,13 +76,28 @@ func (r *Register) Register(c *gin.Context) {
panic(helpers.L(c, "auth.register.failed_token_generate_error"))
}

userId = *user.ID
}, 1)

if responseErr != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": responseErr.Error()})
return
}

// emit user-registered event
ur := events.UserRegistered{}
param := &pbs.UserRegistered{
UserId: uint32(userId),
AffiliationFromCode: "",
}
if requestData.AffiliationFromCode != nil {
param.AffiliationFromCode = *requestData.AffiliationFromCode
}
ur.SetParam(param)
if errs := hub.Emit(&ur); errs != nil {
log.Println("user registered event emit failed", ur, errs)
}

c.JSON(http.StatusOK, gin.H{"token": token})
return
}

0 comments on commit db09c5a

Please sign in to comment.