features/bootstrapping #1
@ -7,16 +7,10 @@ import (
|
|||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
|
||||||
_ "git.jamestombleson.com/jtom38/newsbot-api/docs"
|
"git.jamestombleson.com/jtom38/newsbot-portal/apiclient"
|
||||||
"git.jamestombleson.com/jtom38/newsbot-portal/internal/config"
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/config"
|
||||||
//"git.jamestombleson.com/jtom38/newsbot-api/domain"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
|
||||||
Router *echo.Echo
|
|
||||||
config config.Configs
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ErrParameterIdMissing = "The requested parameter ID was not found."
|
ErrParameterIdMissing = "The requested parameter ID was not found."
|
||||||
ErrParameterMissing = "The requested parameter was not found found:"
|
ErrParameterMissing = "The requested parameter was not found found:"
|
||||||
@ -39,9 +33,16 @@ var (
|
|||||||
ErrUnableToConvertToJson string = "Unable to convert to json"
|
ErrUnableToConvertToJson string = "Unable to convert to json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewServer(ctx context.Context, configs config.Configs) *Handler {
|
type Handler struct {
|
||||||
|
Router *echo.Echo
|
||||||
|
config config.Configs
|
||||||
|
api apiclient.ApiClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.ApiClient) *Handler {
|
||||||
s := &Handler{
|
s := &Handler{
|
||||||
config: configs,
|
config: configs,
|
||||||
|
api: apiClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
//jwtConfig := echojwt.Config{
|
//jwtConfig := echojwt.Config{
|
||||||
@ -57,6 +58,11 @@ func NewServer(ctx context.Context, configs config.Configs) *Handler {
|
|||||||
router.Pre(middleware.Recover())
|
router.Pre(middleware.Recover())
|
||||||
|
|
||||||
router.GET("/", s.HomeIndex)
|
router.GET("/", s.HomeIndex)
|
||||||
|
router.GET("/about", s.HomeAbout)
|
||||||
|
|
||||||
|
users := router.Group("/users")
|
||||||
|
users.GET("/login", s.UserLogin)
|
||||||
|
users.POST("/login", s.UserAfterLogin)
|
||||||
|
|
||||||
s.Router = router
|
s.Router = router
|
||||||
return s
|
return s
|
||||||
|
40
internal/handlers/users.go
Normal file
40
internal/handlers/users.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
|
||||||
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/views/users"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Handler) UserLogin(c echo.Context) error {
|
||||||
|
return Render(c, http.StatusOK, users.Login())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) UserAfterLogin(c echo.Context) error {
|
||||||
|
user := c.FormValue("username")
|
||||||
|
password := c.FormValue("password")
|
||||||
|
|
||||||
|
resp, err := h.api.Users.Login(user, password)
|
||||||
|
if err != nil {
|
||||||
|
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie := new(http.Cookie)
|
||||||
|
cookie.Name = domain.CookieToken
|
||||||
|
cookie.Value = resp.Token
|
||||||
|
c.SetCookie(cookie)
|
||||||
|
|
||||||
|
cookie = new(http.Cookie)
|
||||||
|
cookie.Name = domain.CookieRefreshToken
|
||||||
|
cookie.Value = resp.RefreshToken
|
||||||
|
c.SetCookie(cookie)
|
||||||
|
|
||||||
|
cookie = new(http.Cookie)
|
||||||
|
cookie.Name = domain.CookieUser
|
||||||
|
cookie.Value = user
|
||||||
|
c.SetCookie(cookie)
|
||||||
|
|
||||||
|
return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true))
|
||||||
|
}
|
15
internal/views/users/afterLogin.templ
Normal file
15
internal/views/users/afterLogin.templ
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package users
|
||||||
|
|
||||||
|
// This is returned after the user logs into the application.
|
||||||
|
// It just returns a partial view because it will overlap with the existing template.
|
||||||
|
templ AfterLogin(message string, success bool) {
|
||||||
|
if success {
|
||||||
|
<div class="notification is-success">
|
||||||
|
{ message }
|
||||||
|
</div>
|
||||||
|
} else {
|
||||||
|
<div class="notification is-error">
|
||||||
|
{ message }
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
23
internal/views/users/login.templ
Normal file
23
internal/views/users/login.templ
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package users
|
||||||
|
|
||||||
|
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
|
||||||
|
|
||||||
|
templ Login() {
|
||||||
|
@layout.WithTemplate() {
|
||||||
|
<form hx-post="/users/login">
|
||||||
|
<div class="field">
|
||||||
|
<label class="label">Username</label>
|
||||||
|
<div class="control">
|
||||||
|
<input class="input" type="text" name="username" id="username"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label class="label">Password</label>
|
||||||
|
<div class="control">
|
||||||
|
<input class="input" type="password" name="password" id="exampleInputPassword1"/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="button is-primary">Submit</button>
|
||||||
|
</form>
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user