Skip to content

Commit

Permalink
chore(profile): fetch user's profile from db
Browse files Browse the repository at this point in the history
  • Loading branch information
mbaraa committed May 7, 2024
1 parent 7e7a04f commit c536c89
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
18 changes: 14 additions & 4 deletions handlers/pages/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,21 @@ func (p *pagesHandler) HandlePrivacyPage(w http.ResponseWriter, r *http.Request)
}

func (p *pagesHandler) HandleProfilePage(w http.ResponseWriter, r *http.Request) {
tokenPayload := p.getRequestSessionTokenPayload(r)
dbProfile, err := p.profileRepo.GetByConds("username = ?", tokenPayload["username"].(string))
if err != nil {
if p.isNoReload(r) {
w.Header().Set("HX-Redirect", "/")
} else {
http.Redirect(w, r, config.Env().Hostname, http.StatusTemporaryRedirect)
}
return
}

profile := entities.Profile{
Email: "[email protected]",
Name: "Baraa Al-Masri",
PfpLink: "",
Username: "mbaraa",
Name: dbProfile[0].Name,
PfpLink: dbProfile[0].PfpLink,
Username: dbProfile[0].Username,
}

if p.isNoReload(r) {
Expand Down
20 changes: 14 additions & 6 deletions services/login/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func (e *EmailLoginService) Login(user entities.LoginRequest) (string, error) {
profile[0].AccountId = account[0].Id

verificationToken, err := e.jwtUtil.Sign(map[string]string{
"name": profile[0].Name,
"email": profile[0].Account.Email,
"name": profile[0].Name,
"email": profile[0].Account.Email,
"username": profile[0].Username,
}, jwt.VerificationToken, time.Hour/2)
if err != nil {
return "", err
Expand Down Expand Up @@ -78,8 +79,9 @@ func (e *EmailLoginService) Signup(user entities.SignupRequest) (string, error)
}

verificationToken, err := e.jwtUtil.Sign(map[string]string{
"name": profile.Name,
"email": profile.Account.Email,
"name": profile.Name,
"email": profile.Account.Email,
"username": profile.Username,
}, jwt.VerificationToken, time.Hour/2)
if err != nil {
return "", err
Expand All @@ -105,6 +107,11 @@ func (e *EmailLoginService) VerifyOtp(token string, otp entities.OtpRequest) (st
if !nameExists {
return "", errors.New("missing name")
}
username, usernameExists := mappedUser["username"].(string)
// TODO: ADD THE FUCKING ERRORS SUKA
if !usernameExists {
return "", errors.New("missing username")
}

account, err := e.accountRepo.GetByConds("email = ?", email)
if err != nil {
Expand All @@ -126,8 +133,9 @@ func (e *EmailLoginService) VerifyOtp(token string, otp entities.OtpRequest) (st
}

sessionToken, err := e.jwtUtil.Sign(map[string]string{
"email": email,
"name": name,
"email": email,
"name": name,
"username": username,
}, jwt.SessionToken, time.Hour*24*30)
if err != nil {
return "", err
Expand Down
11 changes: 7 additions & 4 deletions services/login/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func (g *GoogleLoginService) Login(state, code string) (string, error) {
profile[0].AccountId = account[0].Id

verificationToken, err := g.jwtUtil.Sign(map[string]string{
"name": profile[0].Name,
"email": profile[0].Account.Email,
"name": profile[0].Name,
"email": profile[0].Account.Email,
"username": profile[0].Username,
}, jwt.SessionToken, time.Hour*24*30)
if err != nil {
return "", err
Expand All @@ -97,6 +98,7 @@ func (g *GoogleLoginService) Signup(googleUser oauthUserInfo) (string, error) {
IsOAuth: true,
},
Name: googleUser.FullName,
PfpLink: googleUser.PfpLink,
Username: googleUser.Email,
}
// creating a new account will create the account underneath it.
Expand All @@ -106,8 +108,9 @@ func (g *GoogleLoginService) Signup(googleUser oauthUserInfo) (string, error) {
}

verificationToken, err := g.jwtUtil.Sign(map[string]string{
"name": profile.Name,
"email": profile.Account.Email,
"name": profile.Name,
"email": profile.Account.Email,
"username": profile.Username,
}, jwt.SessionToken, time.Hour*24*30)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion views/pages/profile.templ
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ templ profile(prfl entities.Profile) {
alt="Profile picture"
/>
<h2 class={ "text-3xl" }>{ prfl.Name }</h2>
<h3>{ prfl.Email }</h3>
<h3>{ prfl.Username }</h3>
</div>
</section>
<section class={ "w-full", "flex", "justify-center" }>
Expand Down

0 comments on commit c536c89

Please sign in to comment.