Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions internal/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"strings"
"time"
"fmt"

"github.com/jean0t/EurekaFile/internal/auth"
"github.com/jean0t/EurekaFile/internal/database"
Expand All @@ -13,18 +14,25 @@ import (

func Login(w http.ResponseWriter, r *http.Request) {
var err error
var username string = r.FormValue("username")
var password string = r.FormValue("password")
var username string = strings.TrimSpace(r.FormValue("username"))
var password string = strings.TrimSpace(r.FormValue("password"))
var db *gorm.DB

if username == "" || password == "" {
http.Redirect(w, r, "/", http.StatusUnauthorized)
return
}

db, err= database.ConnectToDB()
if err != nil {
fmt.Println("Error connecting to DB")
http.Redirect(w, r, "/", http.StatusUnauthorized)
return
}

err = database.IsValidUser(db, username, strings.TrimSpace(password))
err = database.IsValidUser(db, username, password)
if err != nil {
fmt.Println("Error validating user")
http.Redirect(w, r, "/", http.StatusUnauthorized)
return
}
Expand All @@ -42,16 +50,16 @@ func Login(w http.ResponseWriter, r *http.Request) {
})

http.Redirect(w, r, "/upload", http.StatusSeeOther)
return
}

func Logout(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: "",
Path: "/",
Expires: time.Unix(0, 0),
HttpOnly: true,
Secure: true,
Expires: time.Unix(0, 0),
})

http.Redirect(w, r, "/", http.StatusSeeOther)
Expand Down
5 changes: 5 additions & 0 deletions internal/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func RegisterUser(db *gorm.DB, username, password string) error {
}

var result = db.Create(&user)

if result.Error != nil {
fmt.Println("[!] Error in register is: ", result.Error)
}

return result.Error
}

Expand Down
9 changes: 7 additions & 2 deletions internal/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ func WithAuth(next http.Handler) http.Handler {
var jwtKey []byte = []byte(os.Getenv("JWT_SECRET"))

return http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("auth")
if err != nil {
var (
cookie *http.Cookie
err error
)

cookie, err = r.Cookie("auth")
if err != nil || cookie.Value == "" {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func GetRouter() *http.ServeMux {
Router.HandleFunc("/", controllers.Index)
Router.Handle("/upload", middleware.WithAuth(http.HandlerFunc(controllers.Upload)))
Router.Handle("/files", middleware.WithAuth(http.HandlerFunc(controllers.Files)))
Router.Handle("/login", middleware.WithAuth(http.HandlerFunc(controllers.Login)))
Router.Handle("/login", http.HandlerFunc(controllers.Login))
Router.Handle("/logout", middleware.WithAuth(http.HandlerFunc(controllers.Logout)))

return Router
Expand Down