Skip to content

Commit 0282118

Browse files
committed
Backend: rename db.Authenticate to db.AuthenticateByCredentials.
1 parent d0c1948 commit 0282118

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

backend/db/session.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ var (
1616
InternalServerError = errors.New("internal server error")
1717
)
1818

19-
func Authenticate(username string, password string) (int, error) {
19+
type Credentials struct {
20+
Username string `json:"username"`
21+
Password string `json:"password"`
22+
}
23+
24+
func AuthenticateByCredentials(credentials Credentials) (int, error) {
2025
var userID int
2126
var hashedPassword string
2227

2328
query := `SELECT id, password FROM users WHERE username = ?`
24-
err := db.QueryRow(query, username).Scan(&userID, &hashedPassword)
29+
err := db.QueryRow(query, credentials.Username).Scan(&userID, &hashedPassword)
2530
if err != nil {
2631
if err == sql.ErrNoRows {
2732
return -1, Unathorized
@@ -30,7 +35,7 @@ func Authenticate(username string, password string) (int, error) {
3035
return -1, InternalServerError
3136
}
3237

33-
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
38+
err = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(credentials.Password))
3439
if err != nil {
3540
return -1, Unathorized
3641
}

backend/server/session.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ const (
1313
sessionKeyPresentCookieName = "flowey_session_key_present"
1414
)
1515

16-
type Credentials struct {
17-
Username string `json:"username"`
18-
Password string `json:"password"`
19-
}
20-
2116
type sessionHandler struct{}
2217

2318
func (handler *sessionHandler) handlePost(writer http.ResponseWriter, request *http.Request) {
@@ -27,14 +22,14 @@ func (handler *sessionHandler) handlePost(writer http.ResponseWriter, request *h
2722
return
2823
}
2924

30-
var credentials Credentials
25+
var credentials db.Credentials
3126
err = json.Unmarshal(body, &credentials)
3227
if err != nil {
3328
http.Error(writer, "couldn't parse the body as a JSON object", http.StatusBadRequest)
3429
return
3530
}
3631

37-
userID, err := db.Authenticate(credentials.Username, credentials.Password)
32+
userID, err := db.AuthenticateByCredentials(credentials)
3833
if err == db.Unathorized {
3934
http.Error(writer, err.Error(), http.StatusUnauthorized)
4035
return

0 commit comments

Comments
 (0)