Skip to content

Commit c536c89

Browse files
committed
chore(profile): fetch user's profile from db
1 parent 7e7a04f commit c536c89

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

handlers/pages/pages.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,21 @@ func (p *pagesHandler) HandlePrivacyPage(w http.ResponseWriter, r *http.Request)
9393
}
9494

9595
func (p *pagesHandler) HandleProfilePage(w http.ResponseWriter, r *http.Request) {
96+
tokenPayload := p.getRequestSessionTokenPayload(r)
97+
dbProfile, err := p.profileRepo.GetByConds("username = ?", tokenPayload["username"].(string))
98+
if err != nil {
99+
if p.isNoReload(r) {
100+
w.Header().Set("HX-Redirect", "/")
101+
} else {
102+
http.Redirect(w, r, config.Env().Hostname, http.StatusTemporaryRedirect)
103+
}
104+
return
105+
}
106+
96107
profile := entities.Profile{
97-
Email: "pub@mbaraa.com",
98-
Name: "Baraa Al-Masri",
99-
PfpLink: "",
100-
Username: "mbaraa",
108+
Name: dbProfile[0].Name,
109+
PfpLink: dbProfile[0].PfpLink,
110+
Username: dbProfile[0].Username,
101111
}
102112

103113
if p.isNoReload(r) {

services/login/email.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ func (e *EmailLoginService) Login(user entities.LoginRequest) (string, error) {
4949
profile[0].AccountId = account[0].Id
5050

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

8081
verificationToken, err := e.jwtUtil.Sign(map[string]string{
81-
"name": profile.Name,
82-
"email": profile.Account.Email,
82+
"name": profile.Name,
83+
"email": profile.Account.Email,
84+
"username": profile.Username,
8385
}, jwt.VerificationToken, time.Hour/2)
8486
if err != nil {
8587
return "", err
@@ -105,6 +107,11 @@ func (e *EmailLoginService) VerifyOtp(token string, otp entities.OtpRequest) (st
105107
if !nameExists {
106108
return "", errors.New("missing name")
107109
}
110+
username, usernameExists := mappedUser["username"].(string)
111+
// TODO: ADD THE FUCKING ERRORS SUKA
112+
if !usernameExists {
113+
return "", errors.New("missing username")
114+
}
108115

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

128135
sessionToken, err := e.jwtUtil.Sign(map[string]string{
129-
"email": email,
130-
"name": name,
136+
"email": email,
137+
"name": name,
138+
"username": username,
131139
}, jwt.SessionToken, time.Hour*24*30)
132140
if err != nil {
133141
return "", err

services/login/google.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ func (g *GoogleLoginService) Login(state, code string) (string, error) {
8080
profile[0].AccountId = account[0].Id
8181

8282
verificationToken, err := g.jwtUtil.Sign(map[string]string{
83-
"name": profile[0].Name,
84-
"email": profile[0].Account.Email,
83+
"name": profile[0].Name,
84+
"email": profile[0].Account.Email,
85+
"username": profile[0].Username,
8586
}, jwt.SessionToken, time.Hour*24*30)
8687
if err != nil {
8788
return "", err
@@ -97,6 +98,7 @@ func (g *GoogleLoginService) Signup(googleUser oauthUserInfo) (string, error) {
9798
IsOAuth: true,
9899
},
99100
Name: googleUser.FullName,
101+
PfpLink: googleUser.PfpLink,
100102
Username: googleUser.Email,
101103
}
102104
// creating a new account will create the account underneath it.
@@ -106,8 +108,9 @@ func (g *GoogleLoginService) Signup(googleUser oauthUserInfo) (string, error) {
106108
}
107109

108110
verificationToken, err := g.jwtUtil.Sign(map[string]string{
109-
"name": profile.Name,
110-
"email": profile.Account.Email,
111+
"name": profile.Name,
112+
"email": profile.Account.Email,
113+
"username": profile.Username,
111114
}, jwt.SessionToken, time.Hour*24*30)
112115
if err != nil {
113116
return "", err

views/pages/profile.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ templ profile(prfl entities.Profile) {
3939
alt="Profile picture"
4040
/>
4141
<h2 class={ "text-3xl" }>{ prfl.Name }</h2>
42-
<h3>{ prfl.Email }</h3>
42+
<h3>{ prfl.Username }</h3>
4343
</div>
4444
</section>
4545
<section class={ "w-full", "flex", "justify-center" }>

0 commit comments

Comments
 (0)