feat: fetch foreign user by reference
This commit is contained in:
parent
f606164d88
commit
ad1c53a611
|
|
@ -33,7 +33,7 @@ func (auth *AuthMiddleware) ExpectUser(r *http.Request) *db.User {
|
||||||
log.Fatalln("Failed to get username")
|
log.Fatalln("Failed to get username")
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := db.GetUser(username)
|
user, err := db.GetUserByName(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to get user: %s\n", err)
|
log.Fatalf("Failed to get user: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ func (auth *AuthMiddleware) LoginPost(w http.ResponseWriter, r *http.Request) {
|
||||||
username := r.Form.Get("username")
|
username := r.Form.Get("username")
|
||||||
password := r.Form.Get("password")
|
password := r.Form.Get("password")
|
||||||
|
|
||||||
user, err := db.GetUser(username)
|
user, err := db.GetUserByName(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
http.Error(w, "Username or password invalid", http.StatusUnauthorized)
|
http.Error(w, "Username or password invalid", http.StatusUnauthorized)
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ func (auth *AuthMiddleware) RegisterPost(w http.ResponseWriter, r *http.Request)
|
||||||
newPassword := r.Form.Get("newPassword")
|
newPassword := r.Form.Get("newPassword")
|
||||||
confirmPassword := r.Form.Get("confirmPassword")
|
confirmPassword := r.Form.Get("confirmPassword")
|
||||||
|
|
||||||
existingUser, _ := db.GetUser(username)
|
existingUser, _ := db.GetUserByName(username)
|
||||||
if existingUser != nil {
|
if existingUser != nil {
|
||||||
http.Error(w, "Username is taken", http.StatusBadRequest)
|
http.Error(w, "Username is taken", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,13 @@ type ForeignWishlistProps struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) {
|
func (ctx *Context) ViewForeignWishlist(w http.ResponseWriter, r *http.Request) {
|
||||||
otherUsername := r.PathValue("username")
|
userReference := r.PathValue("userReference")
|
||||||
user := ctx.Auth.ExpectUser(r)
|
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)
|
http.Error(w, "You can't view your own list, silly ;)", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
otherUser, err := db.GetUser(otherUsername)
|
otherUser, err := db.GetUserByReference(userReference)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("An error occurred while fetching a user: %s\n", err)
|
log.Printf("An error occurred while fetching a user: %s\n", err)
|
||||||
http.Error(w, "An error occurred while fetching this user :(", http.StatusInternalServerError)
|
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)
|
http.Error(w, "An error occurred while fetching this user's wishlist :(", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p := ForeignWishlistProps{Username: otherUsername, Gifts: gifts}
|
p := ForeignWishlistProps{Username: otherUser.Name, Gifts: gifts}
|
||||||
templates.Execute(w, "foreign_wishlist.gotmpl", p)
|
templates.Execute(w, "foreign_wishlist.gotmpl", p)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type HomeProps struct {
|
type HomeProps struct {
|
||||||
Gifts []db.Gift
|
Gifts []db.Gift
|
||||||
|
Reference string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) Home(w http.ResponseWriter, r *http.Request) {
|
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)
|
http.Error(w, "An error occurred while fetching your wishlist :(", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
p := HomeProps{Gifts: gifts}
|
p := HomeProps{Gifts: gifts, Reference: user.Reference}
|
||||||
templates.Execute(w, "home.gotmpl", p)
|
templates.Execute(w, "home.gotmpl", p)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
db/user.go
26
db/user.go
|
|
@ -8,8 +8,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id string
|
Id string
|
||||||
Name string
|
Name string
|
||||||
|
Reference string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Gift struct {
|
type Gift struct {
|
||||||
|
|
@ -17,23 +18,34 @@ type Gift struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUser(username string) (*User, error) {
|
func queryForUser(query string, args ...any) (*User, error) {
|
||||||
stmt := "SELECT user.id, user.name FROM user WHERE user.name = ?"
|
|
||||||
var id string
|
var id string
|
||||||
var name 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 {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
user := User{
|
user := User{
|
||||||
Id: id,
|
Id: id,
|
||||||
Name: name,
|
Name: name,
|
||||||
|
Reference: reference,
|
||||||
}
|
}
|
||||||
return &user, nil
|
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) {
|
func CreateUser(username string, passHash []byte) (*User, error) {
|
||||||
stmt := "INSERT INTO user (name, motto, reference, password_hash) VALUES (?, '', ?, ?)"
|
stmt := "INSERT INTO user (name, motto, reference, password_hash) VALUES (?, '', ?, ?)"
|
||||||
reference, err := uuid.NewRandom()
|
reference, err := uuid.NewRandom()
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -35,7 +35,7 @@ func main() {
|
||||||
publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
|
publicMux.HandleFunc("POST /", authMiddleware.LoginPost)
|
||||||
|
|
||||||
protectedMux.HandleFunc("GET /", ctx.Home)
|
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/add", ctx.WishlistAdd)
|
||||||
protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete)
|
protectedMux.HandleFunc("POST /wishlist/delete", ctx.WishlistDelete)
|
||||||
protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost)
|
protectedMux.HandleFunc("POST /logout", authMiddleware.LogoutPost)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,12 @@
|
||||||
<input type="submit" value="Logout">
|
<input type="submit" value="Logout">
|
||||||
</form>
|
</form>
|
||||||
<h1>Lishwist</h1>
|
<h1>Lishwist</h1>
|
||||||
|
<dl>
|
||||||
|
<dt>User reference:</dt>
|
||||||
|
<dd>
|
||||||
|
<pre>{{.Reference}}</pre>
|
||||||
|
</dd>
|
||||||
|
</dl>
|
||||||
<h2>Your list</h2>
|
<h2>Your list</h2>
|
||||||
<ul>
|
<ul>
|
||||||
{{range .Gifts}}
|
{{range .Gifts}}
|
||||||
|
|
@ -18,4 +24,4 @@
|
||||||
<input name="gift_name" required>
|
<input name="gift_name" required>
|
||||||
<input type="submit">
|
<input type="submit">
|
||||||
</form>
|
</form>
|
||||||
{{end}}
|
{{end}}
|
||||||
Loading…
Reference in New Issue