add logout button

This commit is contained in:
2025-12-23 15:42:02 +03:00
parent 797c53bfca
commit 869a513773
3 changed files with 76 additions and 1 deletions

View File

@@ -247,6 +247,31 @@ func main() {
sendJSON(w, AuthResponse{token}, 200)
})
mux.HandleFunc("GET /user", func(w http.ResponseWriter, r *http.Request) {
userId, err := getUserIdFromRequest(r)
if err != nil {
sendApiError(w, "invalid token", err, 401)
return
}
row := db.QueryRowx("SELECT * FROM users WHERE id = ?", userId)
if row.Err() != nil {
sendApiError(w, "user not found", row.Err(), 401)
return
}
var user User
if err := row.StructScan(&user); err != nil {
sendApiError(w, "user not found", err, 401)
return
}
if err := sendJSON(w, user, 200); err != nil {
sendApiError(w, "user not found", err, 401)
return
}
})
mux.HandleFunc("GET /articles", func(w http.ResponseWriter, r *http.Request) {
userId, err := getUserIdFromRequest(r)
if err != nil {