feat: fetch foreign user by reference

This commit is contained in:
Teajey 2024-05-07 19:43:43 +12:00
parent f606164d88
commit ad1c53a611
Signed by: Teajey
GPG Key ID: 970E790FE834A713
8 changed files with 37 additions and 18 deletions

View File

@ -33,7 +33,7 @@ func (auth *AuthMiddleware) ExpectUser(r *http.Request) *db.User {
log.Fatalln("Failed to get username")
}
user, err := db.GetUser(username)
user, err := db.GetUserByName(username)
if err != nil {
log.Fatalf("Failed to get user: %s\n", err)
}

View File

@ -18,7 +18,7 @@ func (auth *AuthMiddleware) LoginPost(w http.ResponseWriter, r *http.Request) {
username := r.Form.Get("username")
password := r.Form.Get("password")
user, err := db.GetUser(username)
user, err := db.GetUserByName(username)
if err != nil {
time.Sleep(time.Second)
http.Error(w, "Username or password invalid", http.StatusUnauthorized)

View File

@ -18,7 +18,7 @@ func (auth *AuthMiddleware) RegisterPost(w http.ResponseWriter, r *http.Request)
newPassword := r.Form.Get("newPassword")
confirmPassword := r.Form.Get("confirmPassword")
existingUser, _ := db.GetUser(username)
existingUser, _ := db.GetUserByName(username)
if existingUser != nil {
http.Error(w, "Username is taken", http.StatusBadRequest)
return

View File

@ -13,13 +13,13 @@ type ForeignWishlistProps struct {
}
func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) {
otherUsername := r.PathValue("username")
userReference := r.PathValue("userReference")
user := ctx.Auth.ExpectUser(r)
if user.Name == otherUsername {
if user.Reference == userReference {
http.Error(w, "You can't view your own list, silly ;)", http.StatusForbidden)
return
}
otherUser, err := db.GetUser(otherUsername)
otherUser, err := db.GetUserByReference(userReference)
if err != nil {
log.Printf("An error occurred while fetching a user: %s\n", err)
http.Error(w, "An error occurred while fetching this user :(", http.StatusInternalServerError)
@ -34,6 +34,6 @@ func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request)
http.Error(w, "An error occurred while fetching this user's wishlist :(", http.StatusInternalServerError)
return
}
p := ForeignWishlistProps{Username: otherUsername, Gifts: gifts}
p := ForeignWishlistProps{Username: otherUser.Name, Gifts: gifts}
templates.Execute(w, "foreign_wishlist.gotmpl", p)
}

View File

@ -9,6 +9,7 @@ import (
type HomeProps struct {
Gifts []db.Gift
Reference string
}
func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
@ -18,6 +19,6 @@ func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
http.Error(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError)
return
}
p := HomeProps{Gifts: gifts}
p := HomeProps{Gifts: gifts, Reference: user.Reference}
templates.Execute(w, "home.gotmpl", p)
}

View File

@ -10,6 +10,7 @@ import (
type User struct {
Id string
Name string
Reference string
}
type Gift struct {
@ -17,11 +18,11 @@ type Gift struct {
Name string
}
func GetUser(username string) (*User, error) {
stmt := "SELECT user.id, user.name FROM user WHERE user.name = ?"
func queryForUser(query string, args ...any) (*User, error) {
var id string
var name string
err := database.QueryRow(stmt, username).Scan(&id, &name)
var reference string
err := database.QueryRow(query, args...).Scan(&id, &name, &reference)
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
@ -30,10 +31,21 @@ func GetUser(username string) (*User, error) {
user := User{
Id: id,
Name: name,
Reference: reference,
}
return &user, nil
}
func GetUserByName(username string) (*User, error) {
stmt := "SELECT user.id, user.name, user.reference FROM user WHERE user.name = ?"
return queryForUser(stmt, username)
}
func GetUserByReference(reference string) (*User, error) {
stmt := "SELECT user.id, user.name, user.reference FROM user WHERE user.reference = ?"
return queryForUser(stmt, reference)
}
func CreateUser(username string, passHash []byte) (*User, error) {
stmt := "INSERT INTO user (name, motto, reference, password_hash) VALUES (?, '', ?, ?)"
reference, err := uuid.NewRandom()

View File

@ -35,7 +35,7 @@ func main() {
publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
protectedMux.HandleFunc("GET /", ctx.Home)
protectedMux.HandleFunc("GET /{username}", ctx.ViewForeignWishlist)
protectedMux.HandleFunc("GET /{userReference}", ctx.ViewForeignWishlist)
protectedMux.HandleFunc("POST /wishlist/add", ctx.WishlistAdd)
protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete)
protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost)

View File

@ -3,6 +3,12 @@
<input type="submit" value="Logout">
</form>
<h1>Lishwist</h1>
<dl>
<dt>User reference:</dt>
<dd>
<pre>{{.Reference}}</pre>
</dd>
</dl>
<h2>Your list</h2>
<ul>
{{range .Gifts}}