Skip to content

Commit 04669c2

Browse files
committed
admin auth implemented
1 parent e51e43b commit 04669c2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

cmd/portal-api/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313

1414
"github.com/krishkumar84/bdcoe-golang-portal/pkg/config"
1515
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/auth"
16+
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/test"
1617
"github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/users"
18+
"github.com/krishkumar84/bdcoe-golang-portal/pkg/middleware"
1719
"github.com/krishkumar84/bdcoe-golang-portal/pkg/storage/mongodb"
1820
// "github.com/krishkumar84/bdcoe-golang-portal/pkg/http/handler/users"
1921
)
@@ -33,8 +35,10 @@ func main() {
3335
}
3436
slog.Info("Database connected",cfg.DatabaseName)
3537

36-
//setup router
38+
// Initialize auth middleware
39+
authMiddleware := middleware.NewAuthMiddleware(cfg.JwtSecret)
3740

41+
// Setup routes
3842
router := http.NewServeMux()
3943

4044
router.HandleFunc("GET/health", func(w http.ResponseWriter, r *http.Request) {
@@ -47,6 +51,20 @@ func main() {
4751
w.Write([]byte("Welcome to BDCOE Portal API server is dockerized up and running"))
4852
})
4953

54+
router.Handle("GET /api/user/test",
55+
authMiddleware.Authenticate(
56+
http.HandlerFunc(test.TestUserRoute),
57+
),
58+
)
59+
60+
router.Handle("GET /api/admin/test",
61+
authMiddleware.Authenticate(
62+
authMiddleware.RequireAdmin(
63+
http.HandlerFunc(test.TestAdminRoute),
64+
),
65+
),
66+
)
67+
5068
router.HandleFunc("POST /api/signup",users.New(storage))
5169
router.HandleFunc("POST /api/login",auth.Login(storage,cfg.JwtSecret))
5270
// router.HandleFunc("GET /api/users/{id}",users.GetById(storage))

pkg/http/handler/test/test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/krishkumar84/bdcoe-golang-portal/pkg/utils/response"
7+
)
8+
9+
func TestUserRoute(w http.ResponseWriter, r *http.Request) {
10+
response.WriteJson(w, http.StatusOK, map[string]string{"message": "User route accessed successfully"})
11+
}
12+
13+
func TestAdminRoute(w http.ResponseWriter, r *http.Request) {
14+
response.WriteJson(w, http.StatusOK, map[string]string{"message": "Admin route accessed successfully"})
15+
}

0 commit comments

Comments
 (0)