diff --git a/internal/handler/loginHandler.go b/internal/handler/loginHandler.go index 1d089bf..6c5a77d 100644 --- a/internal/handler/loginHandler.go +++ b/internal/handler/loginHandler.go @@ -13,6 +13,20 @@ import ( // LoginHandler handles both GET and POST requests for the login page. func (m *Repository) LoginHandler(w http.ResponseWriter, r *http.Request) { + userIDstring := r.URL.Query().Get("id") + if userIDstring != "" { + userID, err := strconv.Atoi(userIDstring) + if err != nil { + setErrorAndRedirect(w, r, "wrong URL", "/error-page") + return + } + err = m.DB.DelSessionByUserID(userID) + if err != nil { + setErrorAndRedirect(w, r, err.Error(), "/error-page") + return + } + } + if r.Method == http.MethodGet { var emptyLogin models.User data := make(map[string]interface{}) @@ -62,8 +76,9 @@ func (m *Repository) LoginHandler(w http.ResponseWriter, r *http.Request) { } cookie := &http.Cookie{ - Name: strconv.Itoa(userID), - Value: m.App.UserLogin.String(), + Name: strconv.Itoa(userID), + Value: m.App.UserLogin.String(), + HttpOnly: true, } http.SetCookie(w, cookie) diff --git a/internal/handler/staticHelperHendlers.go b/internal/handler/staticHelperHendlers.go index 8cab6dc..074026b 100644 --- a/internal/handler/staticHelperHendlers.go +++ b/internal/handler/staticHelperHendlers.go @@ -88,6 +88,12 @@ func (m *Repository) PrivatPolicyHandler(w http.ResponseWriter, r *http.Request) // PersonaCabinetHandler hanles the personal cabinet of selected user. func (m *Repository) PersonaCabinetHandler(w http.ResponseWriter, r *http.Request) { + sessionUserID := m.GetLoggedUser(w, r) + if sessionUserID == 0 { + setErrorAndRedirect(w, r, "unautorized", "/error-page") + return + } + if r.Method == http.MethodGet { userID, _ := strconv.Atoi(r.URL.Query().Get("userID")) var personalInfo models.User @@ -108,6 +114,7 @@ func (m *Repository) PersonaCabinetHandler(w http.ResponseWriter, r *http.Reques data := make(map[string]interface{}) data["personal"] = personalInfo data["totalPosts"] = totalPosts + data["loggedAsID"] = sessionUserID renderer.RendererTemplate(w, "personal.page.html", &models.TemplateData{ Data: data, diff --git a/internal/repository/dbrepo/sqllite.go b/internal/repository/dbrepo/sqllite.go index 998d78e..1a4a9ce 100644 --- a/internal/repository/dbrepo/sqllite.go +++ b/internal/repository/dbrepo/sqllite.go @@ -708,3 +708,20 @@ func (m *SqliteBDRepo) EditUserType(user models.User) error { } return nil } + +func (m *SqliteBDRepo) DelSessionByUserID(userID int) error { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + stmt := `delete from sessionId + where userID = $1 + ` + _, err := m.DB.ExecContext(ctx, stmt, + userID, + ) + + if err != nil { + return err + } + return nil +} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 969f299..326768b 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -31,4 +31,5 @@ type DatabaseInt interface { GetAllPostsByUserID(userID int) ([]models.Post, error) GetAllLikedPostsByUserID(userID int) ([]models.Post, error) EditUserType(user models.User) error + DelSessionByUserID(userID int) error } diff --git a/internal/repository/sql.go b/internal/repository/sql.go index 187ac1a..007871f 100644 --- a/internal/repository/sql.go +++ b/internal/repository/sql.go @@ -61,7 +61,7 @@ var addClassificationToThread = `ALTER TABLE thread ADD COLUMN classification VARCHAR(50) DEFAULT '';` var addUserType = `ALTER TABLE users -ADD COLUMN type VARCHAR(50) DEFAULT '';` +ADD COLUMN type VARCHAR(50) DEFAULT 'user';` func getQuerys() []string { var sqlQuerys []string diff --git a/mainDB.db b/mainDB.db index 34d2ade..2a02da6 100644 Binary files a/mainDB.db and b/mainDB.db differ diff --git a/static/ava/fp.png b/static/ava/fp.png new file mode 100644 index 0000000..9b7c488 Binary files /dev/null and b/static/ava/fp.png differ diff --git "a/static/ava/\320\222\320\260\321\200\320\273\320\276\320\272 \320\232\320\276\321\202\321\217\321\200\320\260.png" "b/static/ava/\320\222\320\260\321\200\320\273\320\276\320\272 \320\232\320\276\321\202\321\217\321\200\320\260.png" new file mode 100644 index 0000000..22165b1 Binary files /dev/null and "b/static/ava/\320\222\320\260\321\200\320\273\320\276\320\272 \320\232\320\276\321\202\321\217\321\200\320\260.png" differ diff --git a/static/post_images/fp.png b/static/post_images/fp.png new file mode 100644 index 0000000..9b7c488 Binary files /dev/null and b/static/post_images/fp.png differ diff --git a/template/main.layout.html b/template/main.layout.html index a6b4aca..3a0be80 100644 --- a/template/main.layout.html +++ b/template/main.layout.html @@ -18,7 +18,7 @@