diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:55:21 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-06 02:08:58 +0800 |
commit | faa5ca8fab23176d390e9522f1485d467851545b (patch) | |
tree | d3b1d081e0ea5e7f71a94dc1d301e2540a8abcc8 /internal/unsorted/http_auth.go | |
parent | Slight refactor on NewServer (diff) | |
download | forge-0.1.28.tar.gz forge-0.1.28.tar.zst forge-0.1.28.zip |
Move stuff into internal/unsortedv0.1.28
Diffstat (limited to 'internal/unsorted/http_auth.go')
-rw-r--r-- | internal/unsorted/http_auth.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/internal/unsorted/http_auth.go b/internal/unsorted/http_auth.go new file mode 100644 index 0000000..b0afa05 --- /dev/null +++ b/internal/unsorted/http_auth.go @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> + +package unsorted + +import ( + "net/http" +) + +// getUserFromRequest returns the user ID and username associated with the +// session cookie in a given [http.Request]. +func (s *Server) getUserFromRequest(request *http.Request) (id int, username string, err error) { + var sessionCookie *http.Cookie + + if sessionCookie, err = request.Cookie("session"); err != nil { + return + } + + err = s.database.QueryRow( + request.Context(), + "SELECT user_id, COALESCE(username, '') FROM users u JOIN sessions s ON u.id = s.user_id WHERE s.session_id = $1;", + sessionCookie.Value, + ).Scan(&id, &username) + + return +} |