Skip to content

Commit 12934e3

Browse files
committed
Backend: add a method to authorize a user by a session key.
1 parent 67ef5a5 commit 12934e3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

backend/db/session.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ func Authenticate(username string, password string) (int, error) {
3838
return userID, nil
3939
}
4040

41+
func Authorize(sessionKey string) error {
42+
query := `SELECT session_key FROM sessions WHERE session_key = ?`
43+
err := db.QueryRow(query, sessionKey).Scan(&sessionKey)
44+
if err != nil {
45+
if err == sql.ErrNoRows {
46+
return Unathorized
47+
}
48+
log.Println(err)
49+
return InternalServerError
50+
}
51+
52+
return nil
53+
}
54+
4155
func CreateSessionKey(userID int) (string, error) {
4256
byteSessionKey := make([]byte, 40)
4357
if _, err := rand.Read(byteSessionKey); err != nil {

0 commit comments

Comments
 (0)