diff options
Diffstat (limited to 'api/internal/controllers/auth.go')
-rw-r--r-- | api/internal/controllers/auth.go | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/api/internal/controllers/auth.go b/api/internal/controllers/auth.go index ab2fbbb..b06c6ef 100644 --- a/api/internal/controllers/auth.go +++ b/api/internal/controllers/auth.go | |||
@@ -5,21 +5,21 @@ import ( | |||
5 | "database/sql" | 5 | "database/sql" |
6 | "encoding/base64" | 6 | "encoding/base64" |
7 | "errors" | 7 | "errors" |
8 | "github.com/gin-gonic/gin" | ||
9 | "net/http" | 8 | "net/http" |
10 | "water/api/internal/models" | 9 | "water/api/internal/models" |
11 | 10 | ||
11 | "github.com/gin-gonic/gin" | ||
12 | |||
13 | "water/api/internal/database" | ||
14 | |||
12 | _ "github.com/mattn/go-sqlite3" | 15 | _ "github.com/mattn/go-sqlite3" |
13 | "golang.org/x/crypto/bcrypt" | 16 | "golang.org/x/crypto/bcrypt" |
14 | "water/api/internal/database" | ||
15 | ) | 17 | ) |
16 | 18 | ||
17 | |||
18 | |||
19 | // AuthHandler is a function that handles users' authentication. It checks if the request | 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. | 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. | 21 | // If the authentication is successful, it will allow the user to access protected routes. |
22 | func AuthHandler (c *gin.Context) { | 22 | func AuthHandler(c *gin.Context) { |
23 | username, password, ok := c.Request.BasicAuth() | 23 | username, password, ok := c.Request.BasicAuth() |
24 | if !ok { | 24 | if !ok { |
25 | c.Header("WWW-Authenticate", `Basic realm="Please enter your username and password."`) | 25 | c.Header("WWW-Authenticate", `Basic realm="Please enter your username and password."`) |
@@ -27,7 +27,11 @@ func AuthHandler (c *gin.Context) { | |||
27 | return | 27 | return |
28 | } | 28 | } |
29 | 29 | ||
30 | db := database.EstablishDBConnection() | 30 | db, err := database.EstablishDBConnection() |
31 | if err != nil { | ||
32 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
33 | return | ||
34 | } | ||
31 | defer func(db *sql.DB) { | 35 | defer func(db *sql.DB) { |
32 | err := db.Close() | 36 | err := db.Close() |
33 | if err != nil { | 37 | if err != nil { |
@@ -44,6 +48,8 @@ func AuthHandler (c *gin.Context) { | |||
44 | if errors.Is(err, sql.ErrNoRows) { | 48 | if errors.Is(err, sql.ErrNoRows) { |
45 | c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) | 49 | c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) |
46 | return | 50 | return |
51 | } else { | ||
52 | c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
47 | } | 53 | } |
48 | } | 54 | } |
49 | 55 | ||
@@ -64,7 +70,6 @@ func AuthHandler (c *gin.Context) { | |||
64 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) | 70 | c.JSON(http.StatusOK, gin.H{"token": apiToken, "user": user, "preferences": preference}) |
65 | } | 71 | } |
66 | 72 | ||
67 | |||
68 | // generateToken is a helper function used in the AuthHandler. It generates a random token for API authentication. | 73 | // generateToken is a helper function used in the AuthHandler. It generates a random token for API authentication. |
69 | // This function creates an empty byte slice of length 32 and fills it with cryptographic random data using the rand.Read function. | 74 | // This function creates an empty byte slice of length 32 and fills it with cryptographic random data using the rand.Read function. |
70 | // If an error occurs during the generation, it will return an empty string. | 75 | // If an error occurs during the generation, it will return an empty string. |
@@ -76,4 +81,4 @@ func generateToken() string { | |||
76 | return "" | 81 | return "" |
77 | } | 82 | } |
78 | return base64.StdEncoding.EncodeToString(token) | 83 | return base64.StdEncoding.EncodeToString(token) |
79 | } \ No newline at end of file | 84 | } |