finished login/registration
This commit is contained in:
@@ -5,7 +5,9 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.haelnorr.com/h/golib/cookies"
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/golib/hwsauth"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
@@ -17,6 +19,7 @@ import (
|
||||
|
||||
func Register(
|
||||
server *hws.Server,
|
||||
auth *hwsauth.Authenticator[*db.User, bun.Tx],
|
||||
conn *bun.DB,
|
||||
cfg *config.Config,
|
||||
store *store.Store,
|
||||
@@ -56,7 +59,6 @@ func Register(
|
||||
return
|
||||
}
|
||||
details, ok := store.GetRegistrationSession(sessionCookie.Value)
|
||||
ok = false
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
@@ -73,20 +75,27 @@ func Register(
|
||||
defer tx.Rollback()
|
||||
method := r.Method
|
||||
if method == "GET" {
|
||||
unique, err := db.IsUsernameUnique(ctx, tx, details.DiscordUser.Username)
|
||||
if err != nil {
|
||||
throwInternalServiceError(server, w, r, "Database query failed", err)
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
page.Register(details.DiscordUser.Username, unique).Render(r.Context(), w)
|
||||
page.Register(details.DiscordUser.Username).Render(r.Context(), w)
|
||||
return
|
||||
}
|
||||
if method == "POST" {
|
||||
// TODO: register the user
|
||||
|
||||
// get the form data
|
||||
//
|
||||
username := r.FormValue("username")
|
||||
user, err := registerUser(ctx, tx, username, details)
|
||||
if err != nil {
|
||||
throwInternalServiceError(server, w, r, "Registration failed", err)
|
||||
}
|
||||
tx.Commit()
|
||||
if user == nil {
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
} else {
|
||||
err = auth.Login(w, r, user, true)
|
||||
if err != nil {
|
||||
throwInternalServiceError(server, w, r, "Login failed", err)
|
||||
}
|
||||
pageFrom := cookies.CheckPageFrom(w, r)
|
||||
w.Header().Set("HX-Redirect", pageFrom)
|
||||
}
|
||||
return
|
||||
}
|
||||
},
|
||||
@@ -124,3 +133,27 @@ func IsUsernameUnique(
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func registerUser(
|
||||
ctx context.Context,
|
||||
tx bun.Tx,
|
||||
username string,
|
||||
details *store.RegistrationSession,
|
||||
) (*db.User, error) {
|
||||
unique, err := db.IsUsernameUnique(ctx, tx, username)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "db.IsUsernameUnique")
|
||||
}
|
||||
if !unique {
|
||||
return nil, nil
|
||||
}
|
||||
user, err := db.CreateUser(ctx, tx, username, details.DiscordUser)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "db.CreateUser")
|
||||
}
|
||||
err = db.UpdateDiscordToken(ctx, tx, user, details.Token)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "db.UpdateDiscordToken")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user