core/internal/server/handlers_admin.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package server import ( "encoding/json" "net/http" "github.com/wbrijesh/identity/internal/auth" "github.com/wbrijesh/identity/internal/models" "github.com/wbrijesh/identity/utils" ) func (s *Server) CreateAdminHandler(w http.ResponseWriter, r *http.Request) { var admin models.Admin if err := json.NewDecoder(r.Body).Decode(&admin); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } if err := utils.CheckNeceassaryFieldsExist(admin, []string{"Email", "PasswordHash", "FirstName", "LastName"}); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } createdAdmin, err := s.db.CreateAdmin(r.Context(), &admin) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } token, err := auth.GenerateAdminJWT(createdAdmin) if err != nil { http.Error(w, "Failed to generate token", http.StatusInternalServerError) return } response := map[string]interface{}{ "admin": createdAdmin, "token": token, } json.NewEncoder(w).Encode(response) } func (s *Server) LoginAdminHandler(w http.ResponseWriter, r *http.Request) { var creds struct { Email string `json:"email"` Password string `json:"password"` } if err := json.NewDecoder(r.Body).Decode(&creds); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } if err := utils.CheckNeceassaryFieldsExist(creds, []string{"Email", "Password"}); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } admin, err := s.db.AuthenticateAdmin(r.Context(), creds.Email, creds.Password) if err != nil { http.Error(w, "Invalid credentials"+err.Error(), http.StatusUnauthorized) return } token, err := auth.GenerateAdminJWT(admin) if err != nil { http.Error(w, "Failed to generate token", http.StatusInternalServerError) return } response := map[string]interface{}{ "admin": admin, "token": token, } json.NewEncoder(w).Encode(response) } |