Skip to content

Commit

Permalink
Update user profile get user info logic (#783)
Browse files Browse the repository at this point in the history
* Update the way to retrieve user info when access user profile

* Bug fix for not login case

* Handle the case user exits and need to be updated
  • Loading branch information
hiveer authored Nov 15, 2024
1 parent db69ab5 commit 6d836d9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 20 deletions.
24 changes: 11 additions & 13 deletions frontend/src/components/navbar/TheNavbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@
<!-- avatar dropdown menu -->
<template #dropdown>
<el-dropdown-menu>
<a :href="userProfile">
<a :href="`/profile/${username}`">
<el-dropdown-item>
{{ $t('navbar.profile') }}
</el-dropdown-item>
</a>
<a :href="`/profile/likes/${this.userName}`">
<a :href="`/profile/likes/${username}`">
<el-dropdown-item>
{{ $t('profile.myCollect') }}
</el-dropdown-item>
Expand Down Expand Up @@ -241,7 +241,7 @@
import MenuItems from './MenuItems.vue'
import useUserStore from '../../stores/UserStore.js'
import { inject } from 'vue'
import jwtFetch from '../../packs/jwtFetch.js'
import useFetchApi from '../../packs/useFetchApi.js'
import { mapState } from 'pinia'
import { useCookies } from "vue3-cookies"
Expand All @@ -255,6 +255,7 @@
phone: String,
isLoggedIn: String,
userName: String,
uuid: String,
userId: String,
canCreateDailyPaper: Boolean,
starcloudUrl: String
Expand All @@ -268,7 +269,6 @@
? `${window.location.pathname}?class=${classParam}`
: window.location.pathname,
isLoggedInBoolean: JSON.parse(this.isLoggedIn.toLowerCase()),
userProfile: `/profile/${this.userName}`,
mobileMenuVisibility: false,
userAvatar: this.avatar,
userStore: useUserStore(),
Expand All @@ -280,7 +280,7 @@
MenuItems
},
computed: {
...mapState(useUserStore, ['email']),
...mapState(useUserStore, ['email', 'username']),
},
watch: {
email(newEmail, _) {
Expand All @@ -295,13 +295,11 @@
location.href = `/${locale}/settings/locale`
},
async fetchUser() {
jwtFetch(`${this.csghubServer}/api/v1/user/${this.userName}`, {
method: 'GET',
}).then((res) => res.json())
.then((body) => {
this.userAvatar = body.data.avatar
this.userStore.initialize(body.data)
})
const {data, _} = await useFetchApi(`${this.csghubServer}/api/v1/user/${this.uuid}?type=uuid`).json()
if (data.value) {
this.userAvatar = data.value.data.avatar
this.userStore.initialize(data.value.data)
}
},
clearCookies() {
const { cookies } = useCookies()
Expand All @@ -312,7 +310,7 @@
},
},
mounted() {
if (this.userName) {
if (this.uuid) {
this.fetchUser()
}
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/views/layouts/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
logo="/images/opencsg_logo.png"
is-logged-in="{{ .isLoggedIn }}"
user-name="{{ .currentUser.Name }}"
uuid="{{ .currentUser.LoginIdentity }}"
company-verified="false"
is-company-user="false"
/>
Expand Down
36 changes: 29 additions & 7 deletions internal/handlers/render/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"opencsg.com/portal/internal/models"
"opencsg.com/portal/internal/svc"
"opencsg.com/portal/pkg/server"
)

type ProfileHandler interface {
Expand All @@ -27,9 +28,10 @@ func NewProfileHandler(svcCtx *svc.ServiceContext) ProfileHandler {

func (i *ProfileHandlerImpl) Detail(ctx *gin.Context) {
user_id := ctx.Param("user_id")
user, err := i.userModel.FindyByName(ctx.Request.Context(), user_id)

if user.ID == 0 || err != nil {
user := i.retrieveUserInfoByUsername(user_id)

if user == nil {
ctx.Redirect(http.StatusFound, "/errors/not-found")
return
}
Expand All @@ -43,16 +45,36 @@ func (i *ProfileHandlerImpl) Detail(ctx *gin.Context) {

func (i *ProfileHandlerImpl) Likes(ctx *gin.Context) {
user_id := ctx.Param("user_id")
user, err := i.userModel.FindyByName(ctx.Request.Context(), user_id)

if user.ID == 0 || err != nil {
ctx.Redirect(http.StatusFound, "/errors/not-found")
return
}
user := i.retrieveUserInfoByUsername(user_id)

data := map[string]interface{}{
"initiator": "likes",
"user": user,
}
renderTemplate(ctx, "profile_likes", data)
}

func (i *ProfileHandlerImpl) retrieveUserInfoByUsername(username string) *models.User {
csghubServer, err := server.NewServer(i.svcCtx.Config)
if err != nil {
return nil
}
userResp, _, err := csghubServer.GetUserInfoByUsername(username)

var user *models.User

if err == nil {
user = &models.User{
Name: userResp.Data.Username,
Nickname: userResp.Data.Nickname,
Phone: userResp.Data.Phone,
Email: userResp.Data.Email,
LoginIdentity: userResp.Data.UUID,
}
user.SetRoles(userResp.Data.Roles...)
} else {
return nil
}
return user
}
12 changes: 12 additions & 0 deletions internal/handlers/render/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func (i *SessionHandlerImpl) Create(ctx *gin.Context) {
ctx.Redirect(http.StatusFound, "/errors/login-failed?error_msg='invalid jwt token'")
return
}
} else {
user.Name = userResp.Username
user.Nickname = userResp.Nickname
user.Phone = userResp.Phone
user.Email = userResp.Email
err = i.userModel.Update(ctx, user)
if err != nil {
stackTrace := string(debug.Stack())
slog.Error("Login Error", "error", "update user failed", slog.Any("error", err), "uuid", userResp.UUID, "stack", stackTrace)
ctx.Redirect(http.StatusFound, "/errors/login-failed")
return
}
}

user.SetRoles(userResp.Roles...)
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/backend/csghubserver/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ func (c *CsgHubServer) GetUserInfo(uuid string) (*types.VerifyJWTTokenResp, *htt
)
return userResp, resp, err
}

func (c *CsgHubServer) GetUserInfoByUsername(username string) (*types.VerifyJWTTokenResp, *http.Response, error) {
userResp := new(types.VerifyJWTTokenResp)

resp, err := c.getParsedResponse(
"GET",
fmt.Sprintf("/user/%s", username),
nil,
nil,
userResp,
)
return userResp, resp, err
}
1 change: 1 addition & 0 deletions pkg/server/backend/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ type Server interface {
DownloadFileRaw(req types.DownloadReq) (*types.DownloadFileRawResp, *http.Response, error)
DownloadFile(req types.DownloadReq) ([]byte, *http.Response, error)
GetUserInfo(uuid string) (*types.VerifyJWTTokenResp, *http.Response, error)
GetUserInfoByUsername(username string) (*types.VerifyJWTTokenResp, *http.Response, error)
}

0 comments on commit 6d836d9

Please sign in to comment.