Files
sonr/app/gateway/handlers/register.go
T
Prad NukalaandGitHub d69c2a9d53 feature/refactor ui (#1205)
* fix: correct HTTP error handling in gateway

* refactor: migrate database and ORM to internal modules

* feat: introduce taskfile build system for improved workflow management

* refactor: update taskfiles to use relative paths

* feat: add profile status field

* refactor: move rendering logic to context package

* fix: improve error handling in credentials retrieval

* refactor: optimize HTTP request handling in Wasm environment

* refactor: refactor config loading in motr command

* chore: add process-compose for service management

* chore: remove default task and update gum format command

* fix: update project dependencies

* refactor: improve code readability and maintainability

* refactor: consolidate error handling components

* refactor: update index handler to use new context package

* refactor: consolidate database scripts and move to deploy directory

* feat: Update flake.nix with development tools and environment configuration

* fix: ignore flake.lock file

* refactor: migrate build process to use taskfiles for improved modularity and maintainability

* refactor: improve GatewayContext and reorganize handlers

* refactor: Remove unused profile creation functions

* (chore): templ generation

* test: add test file for vaults.go

* maintenance: remove defunct Discord server link

* docs: update checks workflow documentation

* test: remove obsolete vaults test file

* refactor: move version bumping logic to release workflow
2024-12-22 17:01:11 -05:00

112 lines
3.6 KiB
Go

package handlers
import (
"encoding/json"
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/app/gateway/context"
"github.com/onsonr/sonr/app/gateway/islands"
"github.com/onsonr/sonr/app/gateway/views"
hwayorm "github.com/onsonr/sonr/internal/database/hwayorm"
"github.com/onsonr/sonr/pkg/common"
)
func RegisterHandler(g *echo.Group) {
g.GET("/", renderProfileForm)
g.POST("/profile", validateProfileForm)
g.GET("/passkey", renderPasskeyForm)
g.POST("/passkey", validatePasskeyForm)
g.GET("/vault", renderVaultStatus)
}
// ╭──────────────────────────────────────────────────────╮
// │ Registration Views │
// ╰──────────────────────────────────────────────────────
func renderProfileForm(c echo.Context) error {
params := context.CreateProfileParams{
FirstNumber: 6,
LastNumber: 3,
}
return context.Render(c, views.RegisterProfileView(params.FirstNumber, params.LastNumber))
}
func renderPasskeyForm(c echo.Context) error {
cc, err := context.GetGateway(c)
if err != nil {
return err
}
handle := c.FormValue("handle")
origin := c.FormValue("origin")
name := c.FormValue("name")
cc.InsertProfile(context.BG(), hwayorm.InsertProfileParams{
Handle: handle,
Origin: origin,
Name: name,
})
params, err := cc.Spawn(handle, origin)
if err != nil {
return context.RenderError(c, err)
}
return context.Render(c, views.RegisterPasskeyView(params.Address, params.Handle, params.Name, params.Challenge, params.CreationBlock))
}
func renderVaultStatus(c echo.Context) error {
return context.Render(c, views.LoadingView())
}
// ╭─────────────────────────────────────────────────────────╮
// │ Validation Components │
// ╰─────────────────────────────────────────────────────────╯
func validateProfileForm(c echo.Context) error {
cc, err := context.GetGateway(c)
if err != nil {
return context.RenderError(c, err)
}
handle := c.FormValue("handle")
if handle == "" {
return context.Render(c, islands.InputHandleError(handle, "Please enter a 4-16 character handle"))
}
notok, err := cc.CheckHandleExists(context.BG(), handle)
if err != nil {
return err
}
if notok {
return context.Render(c, islands.InputHandleError(handle, "Handle is already taken"))
}
cc.WriteCookie(common.UserHandle, handle)
return context.Render(c, islands.InputHandleSuccess(handle))
}
func validatePasskeyForm(c echo.Context) error {
cc, err := context.GetGateway(c)
if err != nil {
return context.RenderError(c, err)
}
handle := context.GetProfileHandle(c)
origin := c.Request().Host
credentialJSON := c.FormValue("credential")
cred := &context.CredentialDescriptor{}
// Unmarshal the credential JSON
err = json.Unmarshal([]byte(credentialJSON), cred)
if err != nil {
return context.RenderError(c, err)
}
md := cred.ToModel(handle, origin)
_, err = cc.InsertCredential(context.BG(), hwayorm.InsertCredentialParams{
Handle: md.Handle,
CredentialID: md.CredentialID,
Origin: md.Origin,
Type: md.Type,
Transports: md.Transports,
})
if err != nil {
return context.RenderError(c, err)
}
return context.Render(c, views.LoadingView())
}