mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-02 17:31:39 +00:00
* refactor: update import paths in gateway handlers * refactor: remove obsolete devtools Makefile and README * build: optimize build process for improved efficiency * refactor: remove obsolete pkl files related to Matrix and Sonr network configurations * refactor: move embed code to x/dwn/types
112 lines
3.6 KiB
Go
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"
|
|
"github.com/onsonr/sonr/internal/common"
|
|
hwayorm "github.com/onsonr/sonr/internal/database/hwayorm"
|
|
)
|
|
|
|
func RegisterHandler(g *echo.Group) {
|
|
g.GET("/start", 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())
|
|
}
|