diff options
Diffstat (limited to 'api/internal/controllers/auth.go')
-rw-r--r-- | api/internal/controllers/auth.go | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go index de9ed05..58653d0 100644 --- a/api/internal/controllers/auth.go +++ b/api/internal/controllers/auth.go | |||
@@ -14,6 +14,11 @@ import ( | |||
14 | "water/api/internal/database" | 14 | "water/api/internal/database" |
15 | ) | 15 | ) |
16 | 16 | ||
17 | |||
18 | |||
19 | // AuthHandler is a function that handles users' authentication. It checks if the request | ||
20 | // has valid credentials, authenticates the user and sets the user's session. | ||
21 | // If the authentication is successful, it will allow the user to access protected routes. | ||
17 | func AuthHandler (c *gin.Context) { | 22 | func AuthHandler (c *gin.Context) { |
18 | username, password, ok := c.Request.BasicAuth() | 23 | username, password, ok := c.Request.BasicAuth() |
19 | if !ok { | 24 | if !ok { |
@@ -55,7 +60,11 @@ func AuthHandler (c *gin.Context) { | |||
55 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) | 60 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) |
56 | } | 61 | } |
57 | 62 | ||
58 | // generatToken will g | 63 | |
64 | // generateToken is a helper function used in the AuthHandler. It generates a random token for API authentication. | ||
65 | // This function creates an empty byte slice of length 32 and fills it with cryptographic random data using the rand.Read function. | ||
66 | // If an error occurs during the generation, it will return an empty string. | ||
67 | // The generated cryptographic random data is then encoded into a base64 string and returned. | ||
59 | func generateToken() string { | 68 | func generateToken() string { |
60 | token := make([]byte, 32) | 69 | token := make([]byte, 32) |
61 | _, err := rand.Read(token) | 70 | _, err := rand.Read(token) |