From 74c4c7cb02881cf8334d4d5e0237e64e678a3f7a Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 25 Jan 2022 20:30:47 +0000 Subject: [PATCH] Improve login/logout Extract mutation types to mutation-types.js --- http/session.go | 21 +++++++++++++++++---- web/src/App.vue | 6 +++++- web/src/pages/Login.vue | 5 ++++- web/src/pages/Register.vue | 5 +++-- web/src/store/index.js | 13 ++++++------- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/http/session.go b/http/session.go index e82c467..5f61b38 100644 --- a/http/session.go +++ b/http/session.go @@ -93,9 +93,10 @@ func (h *Handler) loginPost(c *gin.Context) { go h.Service.UpdateLastLogin(context.Background(), user.ID) - c.JSON(http.StatusOK, map[string]string{ - "token": t, - }) + c.JSON(http.StatusOK, struct { + Token string + User postgres.User + }{t, user}) } type registerInformation struct { @@ -130,8 +131,20 @@ func (h *Handler) registerPost(c *gin.Context) { Password: hash, Email: register.Email, } - _, err = h.Service.CreateUser(c.Request.Context(), createUser) + user, err := h.Service.CreateUser(c.Request.Context(), createUser) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) } + + t, err := h.TokenVerifier.CreateToken(&user) + if err != nil { + c.AbortWithError(http.StatusUnauthorized, err) + } + + go h.Service.UpdateLastLogin(context.Background(), user.ID) + + c.JSON(http.StatusOK, struct { + Token string + User postgres.User + }{t, user}) } diff --git a/web/src/App.vue b/web/src/App.vue index 6cbb940..649a4f4 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -1,5 +1,8 @@