Improve handling of context

This commit is contained in:
2021-12-07 19:08:53 +00:00
parent fbd283cd1c
commit 1d4bc158a8
8 changed files with 21 additions and 40 deletions

View File

@ -68,7 +68,7 @@ func (h *Handler) loginPost(c *gin.Context) {
username, _ := c.GetPostForm("username")
password, _ := c.GetPostForm("password")
user, err := h.Service.DB.GetUserByUsername(context.Background(), username)
user, err := h.Service.DB.GetUserByUsername(c.Request.Context(), username)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
@ -84,7 +84,8 @@ func (h *Handler) loginPost(c *gin.Context) {
c.AbortWithError(http.StatusUnauthorized, err)
}
_, _ = h.Service.DB.UpdateLastLogin(context.Background(), user.ID)
go h.Service.DB.UpdateLastLogin(context.Background(), user.ID)
maxAge := (int)((expiration * time.Hour).Seconds())
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
c.JSON(http.StatusOK, map[string]string{
@ -97,7 +98,7 @@ func (h *Handler) registerPost(c *gin.Context) {
password, _ := c.GetPostForm("password")
name, _ := c.GetPostForm("name")
_, err := h.Service.DB.GetUserByUsername(context.Background(), email)
_, err := h.Service.DB.GetUserByUsername(c.Request.Context(), email)
if err == nil {
c.AbortWithStatus(http.StatusUnauthorized)
return
@ -114,7 +115,7 @@ func (h *Handler) registerPost(c *gin.Context) {
Password: hash,
Email: email,
}
_, err = h.Service.DB.CreateUser(context.Background(), createUser)
_, err = h.Service.DB.CreateUser(c.Request.Context(), createUser)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
}