diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-07 19:16:07 -0500 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-03-07 19:16:07 -0500 |
commit | 6651daca670664f3de8af9c7bcb74b1e7c6c6be9 (patch) | |
tree | f1bb35ef8b0e1d498842a17b84c87c6ee996274e /api/internal/controllers/user.go | |
parent | 5fa57845052655883120ba4d19a85d8756fb8d8c (diff) |
Add CORS middleware and authentication middleware to the API server.
The `setupRouter` function in `main.go` now includes a CORS middleware and a token authentication middleware. The CORS middleware allows cross-origin resource sharing by setting the appropriate response headers. The token authentication middleware checks for the presence of an `Authorization` header with a valid bearer token. If the token is missing or invalid, an unauthorized response is returned.
In addition to these changes, a new test file `main_test.go` has been added to test the `/api/v1/auth` route. This test suite includes two test cases: one for successful authentication and one for failed authentication.
Update go.mod to include new dependencies.
The `go.mod` file has been modified to include two new dependencies: `github.com/spf13/viper` and `github.com/stretchr/testify`.
Ignore go.sum changes.
Ignore changes in the `go.sum` file, as they only include updates to existing dependencies.
Diffstat (limited to 'api/internal/controllers/user.go')
-rw-r--r-- | api/internal/controllers/user.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/api/internal/controllers/user.go b/api/internal/controllers/user.go new file mode 100644 index 0000000..1f3f813 --- /dev/null +++ b/api/internal/controllers/user.go | |||
@@ -0,0 +1,17 @@ | |||
1 | package controllers | ||
2 | |||
3 | import ( | ||
4 | "net/http" | ||
5 | "github.com/gin-gonic/gin" | ||
6 | ) | ||
7 | |||
8 | func GetUser(c *gin.Context) { | ||
9 | c.JSON(http.StatusOK, gin.H{"message": "User found"}) | ||
10 | } | ||
11 | func GetUserPreferences(c *gin.Context) { | ||
12 | c.JSON(http.StatusOK, gin.H{"message": "Preferences fetched successfully"}) | ||
13 | } | ||
14 | |||
15 | func UpdateUserPreferences(c *gin.Context) { | ||
16 | c.JSON(http.StatusOK, gin.H{"message": "Preferences updated successfully"}) | ||
17 | } \ No newline at end of file | ||