Skip to content

Commit af31532

Browse files
committed
auth roles
1 parent 4956597 commit af31532

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

cmd/portal-api/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ func main() {
3737

3838
router := http.NewServeMux()
3939

40-
router.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
40+
router.HandleFunc("GET/health", func(w http.ResponseWriter, r *http.Request) {
4141
w.WriteHeader(http.StatusOK)
4242
w.Write([]byte("OK"))
4343
})
4444

45-
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
45+
router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
4646
w.WriteHeader(http.StatusOK)
4747
w.Write([]byte("Welcome to BDCOE Portal API server is dockerized up and running"))
4848
})

pkg/http/handler/users/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func New(storage storage.Storage) http.HandlerFunc{
3939
return
4040
}
4141

42-
lastId, err := storage.CreateUser(user.Name, user.Email, user.Password, user.StudentId)
42+
lastId, err := storage.CreateUser(user.Name, user.Email, user.Password, user.StudentId,types.RoleUser)
4343

4444
slog.Info("User created sucessfully",slog.String("userId",fmt.Sprint(lastId)))
4545
if err != nil {

pkg/storage/mongodb/mongodb.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func New(cfg *config.Config) (*MongoDB, error) {
4141
}
4242

4343
// User operations
44-
func (m *MongoDB) CreateUser(name, email, password, studentId string) (string, error) {
44+
func (m *MongoDB) CreateUser(name, email, password, studentId string, role types.Role) (string, error) {
4545
collection := m.db.Collection("users")
4646
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
4747
defer cancel()
@@ -69,6 +69,7 @@ func (m *MongoDB) CreateUser(name, email, password, studentId string) (string, e
6969
StudentId: studentId,
7070
Password: password,
7171
CreatedAt: time.Now(),
72+
Role: role,
7273
}
7374

7475
result, err := collection.InsertOne(ctx, user)

pkg/storage/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ type Storage interface {
66
// CreateUser(name string, email string, age int)(int64, error)
77
// GetUserById(id int64)(types.User, error)
88
// GetAllUsers()([]types.User, error)
9-
CreateUser(name string, email string, password string, studentId string) (string, error)
9+
CreateUser(name string, email string, password string, studentId string, role types.Role) (string, error)
1010
GetUserByEmail(email string) (*types.User, error)
1111
}

pkg/types/models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import (
55

66
"go.mongodb.org/mongo-driver/bson/primitive"
77
)
8+
type Role string
9+
const (
10+
RoleUser Role = "user"
11+
RoleAdmin Role = "admin"
12+
)
813

914
type User struct {
1015
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
@@ -13,6 +18,7 @@ type User struct {
1318
Password string `bson:"password" json:"password" validate:"required"`
1419
StudentId string `bson:"studentId" json:"studentId" validate:"required"`
1520
CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
21+
Role Role `bson:"role" json:"role" default:"user"`
1622
}
1723

1824
type Contest struct {

0 commit comments

Comments
 (0)