aboutsummaryrefslogtreecommitdiff
path: root/http_auth.go
blob: 4868aa45e7e8ecb821c2c1560a9c4ab0f71d1521 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileContributor: Runxi Yu <https://runxiyu.org>

package main

import (
	"net/http"
)

func get_user_info_from_request(r *http.Request) (id int, username string, err error) {
	var session_cookie *http.Cookie

	if session_cookie, err = r.Cookie("session"); err != nil {
		return
	}

	err = database.QueryRow(
		r.Context(),
		"SELECT user_id, COALESCE(username, '') FROM users u JOIN sessions s ON u.id = s.user_id WHERE s.session_id = $1;",
		session_cookie.Value,
	).Scan(&id, &username)

	return
}