summaryrefslogtreecommitdiff
path: root/api/handlers/handlers.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/handlers/handlers.go')
-rw-r--r--api/handlers/handlers.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/api/handlers/handlers.go b/api/handlers/handlers.go
new file mode 100644
index 0000000..7d5fd10
--- /dev/null
+++ b/api/handlers/handlers.go
@@ -0,0 +1,68 @@
+package handlers
+
+import (
+ "encoding/json"
+ "net/http"
+
+ "github.com/boozec/rahanna/api/auth"
+ "github.com/boozec/rahanna/api/database"
+ "golang.org/x/crypto/bcrypt"
+)
+
+func RegisterUser(w http.ResponseWriter, r *http.Request) {
+ var user database.User
+ err := json.NewDecoder(r.Body).Decode(&user)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ user.Password = string(hashedPassword)
+
+ db, _ := database.GetDb()
+
+ result := db.Create(&user)
+ if result.Error != nil {
+ http.Error(w, result.Error.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.WriteHeader(http.StatusCreated)
+}
+
+func LoginUser(w http.ResponseWriter, r *http.Request) {
+ var inputUser database.User
+ err := json.NewDecoder(r.Body).Decode(&inputUser)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ var storedUser database.User
+
+ db, _ := database.GetDb()
+ result := db.Where("username = ?", inputUser.Username).First(&storedUser)
+ if result.Error != nil {
+ http.Error(w, "Invalid credentials", http.StatusUnauthorized)
+ return
+ }
+
+ err = bcrypt.CompareHashAndPassword([]byte(storedUser.Password), []byte(inputUser.Password))
+ if err != nil {
+ http.Error(w, "Invalid credentials", http.StatusUnauthorized)
+ return
+ }
+
+ token, err := auth.GenerateJWT(storedUser.ID)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ json.NewEncoder(w).Encode(map[string]string{"token": token})
+}