users can now register with the service

This commit is contained in:
James Tombleson 2024-05-12 09:35:13 -07:00
parent 3c35824dc5
commit a10358ff91
6 changed files with 79 additions and 1 deletions

View File

@ -15,6 +15,7 @@ const (
type Users interface {
Login(username, password string) (domain.LoginResponse, error)
SignUp(username, password string) (domain.BaseResponse, error)
}
type userClient struct {
@ -45,3 +46,20 @@ func (a userClient) Login(username, password string) (domain.LoginResponse, erro
return bind, nil
}
func (a userClient) SignUp(username, password string) (domain.BaseResponse, error) {
endpoint := fmt.Sprintf("%s/%s", a.serverAddress, UserRegisterRoute)
param := url.Values{}
param.Set("username", username)
param.Set("password", password)
// Create the struct we are expecting back from the API so we can have it populated
var bind = domain.BaseResponse{}
err := PostUrlForm(a.client, endpoint, param, &bind)
if err != nil {
return domain.BaseResponse{}, err
}
return bind, nil
}

View File

@ -63,6 +63,8 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.
users := router.Group("/users")
users.GET("/login", s.UserLogin)
users.POST("/login", s.UserAfterLogin)
users.GET("/signup", s.UserSignUp)
users.POST("/signup", s.UserAfterSignUp)
s.Router = router
return s

View File

@ -1,6 +1,7 @@
package handlers
import (
"fmt"
"net/http"
"git.jamestombleson.com/jtom38/newsbot-portal/internal/domain"
@ -38,3 +39,22 @@ func (h *Handler) UserAfterLogin(c echo.Context) error {
return Render(c, http.StatusOK, users.AfterLogin("Login Successful!", true))
}
func (h *Handler) UserSignUp(c echo.Context) error {
return Render(c, http.StatusOK, users.SignUp())
}
func (h *Handler) UserAfterSignUp(c echo.Context) error {
user := c.FormValue("username")
password := c.FormValue("password")
resp, err := h.api.Users.SignUp(user, password)
if err != nil {
return Render(c, http.StatusBadRequest, users.AfterLogin(err.Error(), false))
}
if resp.Message != "OK" {
msg := fmt.Sprintf("Failed to create account. Message: %s", resp.Message)
return Render(c, http.StatusBadRequest, users.AfterLogin(msg, false))
}
return Render(c, http.StatusOK, users.AfterSignUp("Registration Successful!", true))
}

View File

@ -61,7 +61,7 @@ templ WithTemplate() {
<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<a class="button is-primary">
<a class="button is-primary" href="/users/signup">
<strong>Sign up</strong>
</a>
<a class="button is-light" href="/users/login">

View File

@ -0,0 +1,15 @@
package users
// This is returned after the user creates an account.
// It just returns a partial view because it will overlap with the existing template.
templ AfterSignUp(message string, success bool) {
if success {
<div class="notification is-success">
{ message }
</div>
} else {
<div class="notification is-error">
{ message }
</div>
}
}

View File

@ -0,0 +1,23 @@
package users
import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout"
templ SignUp() {
@layout.WithTemplate() {
<form hx-post="/users/signup">
<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>
}
}