Compare commits
2 Commits
5a950af7a1
...
3796ed4d20
Author | SHA1 | Date | |
---|---|---|---|
3796ed4d20 | |||
d5fffa1d66 |
@ -62,3 +62,31 @@ func (a userClient) SignUp(username, password string) (domain.BaseResponse, erro
|
||||
|
||||
return bind, nil
|
||||
}
|
||||
|
||||
func (a userClient) RefreshJwtToken(username, refreshToken string) (domain.LoginResponse, error) {
|
||||
endpoint := fmt.Sprintf("%s/%s/refresh/token", a.serverAddress, UserBaseRoute)
|
||||
body := domain.RefreshTokenRequest{
|
||||
Username: username,
|
||||
RefreshToken: refreshToken,
|
||||
}
|
||||
|
||||
var bind = domain.LoginResponse{}
|
||||
err := PostBodyUrl(a.client, endpoint, body, &bind)
|
||||
if err != nil {
|
||||
return domain.LoginResponse{}, err
|
||||
}
|
||||
|
||||
return bind, nil
|
||||
}
|
||||
|
||||
func (a userClient) refreshSessionToken() (domain.BaseResponse, error) {
|
||||
endpoint := fmt.Sprintf("%s/%s/refresh/sessionToken", a.serverAddress, UserBaseRoute)
|
||||
|
||||
var bind = domain.BaseResponse{}
|
||||
err := PostUrl(a.client, endpoint, &bind)
|
||||
if err != nil {
|
||||
return domain.BaseResponse{}, err
|
||||
}
|
||||
|
||||
return bind, nil
|
||||
}
|
||||
|
@ -29,3 +29,40 @@ func PostUrlForm(client http.Client, endpoint string, param url.Values, t any) e
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func PostUrl(client http.Client, endpoint string, t any) error {
|
||||
response, err := http.Post(endpoint, ApplicationJson, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
err = decoder.Decode(&t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func PostBodyUrl(client http.Client, endpoint string, body any, t any) error {
|
||||
jsonBody, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response, err := http.Post(endpoint, ApplicationJson, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
err = decoder.Decode(&t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
5
internal/domain/context.go
Normal file
5
internal/domain/context.go
Normal file
@ -0,0 +1,5 @@
|
||||
package domain
|
||||
|
||||
type contextKey string
|
||||
|
||||
var UserNameContext contextKey = "username"
|
@ -13,7 +13,7 @@ import (
|
||||
func (h *Handler) ArticlesList(c echo.Context) error {
|
||||
_, err := ValidateJwt(c, h.config.JwtSecret, h.config.ServerAddress)
|
||||
if err != nil {
|
||||
return Render(c, http.StatusBadRequest, layout.Error(err))
|
||||
return Render(c, http.StatusOK, layout.Error(err))
|
||||
}
|
||||
|
||||
userToken, err := c.Cookie(domain.CookieToken)
|
||||
|
@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
@ -68,11 +69,14 @@ func Render(ctx echo.Context, statusCode int, t templ.Component) error {
|
||||
|
||||
// take the request context and make it a var
|
||||
request := ctx.Request().Context()
|
||||
|
||||
//Check to see if we the echo context has the cookie we are looking for, if so, create a new context based on what we had and add the value
|
||||
//darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode)
|
||||
//if err == nil {
|
||||
// request = context.WithValue(request, domain.CookieSettingsDarkMode, darkMode.Value)
|
||||
//}
|
||||
username, err := ctx.Cookie(domain.CookieUser)
|
||||
if err == nil {
|
||||
request = context.WithValue(request, domain.UserNameContext, username.Value)
|
||||
} else {
|
||||
request = context.WithValue(request, domain.UserNameContext, "")
|
||||
}
|
||||
|
||||
return t.Render(request, ctx.Response().Writer)
|
||||
}
|
||||
|
6
internal/models/layout.go
Normal file
6
internal/models/layout.go
Normal file
@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type LayoutIsLoggedInViewModel struct {
|
||||
IsLoggedIn bool
|
||||
Username string
|
||||
}
|
@ -19,39 +19,24 @@ templ navBar() {
|
||||
<div id="navbarBasicExample" class="navbar-menu">
|
||||
<div class="navbar-start">
|
||||
<a class="navbar-item" href="/articles">Articles</a>
|
||||
<a class="navbar-item">Documentation</a>
|
||||
<!--
|
||||
<div class="navbar-item has-dropdown is-hoverable">
|
||||
<a class="navbar-link">
|
||||
More
|
||||
</a>
|
||||
<div class="navbar-dropdown">
|
||||
<a class="navbar-item">
|
||||
About
|
||||
</a>
|
||||
<a class="navbar-item is-selected">
|
||||
Jobs
|
||||
</a>
|
||||
<a class="navbar-item">
|
||||
Contact
|
||||
</a>
|
||||
<hr class="navbar-divider"/>
|
||||
<a class="navbar-item">
|
||||
Report an issue
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
-->
|
||||
<a class="navbar-item">{ getUsername(ctx) }</a>
|
||||
</div>
|
||||
<div class="navbar-end">
|
||||
<div class="navbar-item">
|
||||
<div class="buttons">
|
||||
<a class="button is-primary" href="/users/signup">
|
||||
<strong>Sign up</strong>
|
||||
</a>
|
||||
<a class="button is-light" href="/users/login">
|
||||
Log in
|
||||
</a>
|
||||
if getUsername(ctx) == "" {
|
||||
<a class="button is-primary" href="/users/signup"><strong>Sign up</strong></a>
|
||||
<a class="button is-light" href="/users/login">Log in</a>
|
||||
} else {
|
||||
<div class="navbar-item has-dropdown is-hoverable">
|
||||
<a class="navbar-link">{ getUsername(ctx) }</a>
|
||||
|
||||
<div class="navbar-dropdown">
|
||||
<a class="navbar-item">Profile</a>
|
||||
<a class="navbar-item">Logout</a>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
14
internal/views/layout/util.go
Normal file
14
internal/views/layout/util.go
Normal file
@ -0,0 +1,14 @@
|
||||
package layout
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
||||
)
|
||||
|
||||
func getUsername(ctx context.Context) string {
|
||||
if theme, ok := ctx.Value(domain.UserNameContext).(string); ok {
|
||||
return theme
|
||||
}
|
||||
return ""
|
||||
}
|
Loading…
Reference in New Issue
Block a user