Skip to content

Commit

Permalink
comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Jan 4, 2024
1 parent 3faef77 commit 40ff43b
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 8 deletions.
19 changes: 17 additions & 2 deletions internal/handler/loginHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions internal/handler/staticHelperHendlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions internal/repository/dbrepo/sqllite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion internal/repository/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified mainDB.db
Binary file not shown.
Binary file added static/ava/fp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/ava/Варлок Котяра.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/post_images/fp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions template/main.layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<nav id="navbarTop" class="navbar navbar-expand-lg navbar-dark navBar">
<div class="container-fluid">
<img src="static\logo\10.png" class="logo_img" alt="logo"><a class="navbar-brand" href="/">Fan.Forge.Forum.</a>
<img src="static\logo\10.png" class="logo_img" alt="logo"><a class="navbar-brand" href="/home">Fan.Forge.Forum.</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down Expand Up @@ -54,9 +54,9 @@
<div>
{{$loggedAs := index .Data "loggedAs"}}
{{$loggedAsID := index .Data "loggedAsID"}}
{{ if $loggedAs }}
{{ if $loggedAsID }}
<a class="" href="/personal_cabinet?userID={{$loggedAsID}}">{{$loggedAs}}</a>
<a class="btn btn-light login" href="/login">Logout</a>
<a class="btn btn-light login" href="/login?id={{$loggedAsID}}">Logout</a>
{{ else }}
<a class="btn btn-light login" href="/login">Login</a>
<a class="btn btn-light register" href="/registration">Register</a>
Expand Down
2 changes: 1 addition & 1 deletion template/register.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1>Register</h1>
<input type="password" class="form-control {{with .Form.Errors.Get "passwordReg"}} is-invalid {{end}}" name="passwordReg" value="{{$res.Password}}" id="passwordReg">
</div>
<div class="mb-3">
<label for="avatar" class="col-form-label">Avatar Picture: (Max 1 MB)</label>
<label for="avatar" class="col-form-label">Avatar Picture: (Max 1 MB). Avatar Image is OBLIGATORY</label>


<input type="file" class="form-control" name="avatar" id="avatar">
Expand Down
2 changes: 1 addition & 1 deletion template/theme.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<tr>
<!-- Colling Modal -->
<div class="d-flex justify-content-start align-items-center">
<button type="button" class="btn btn-light me-2" data-bs-toggle="modal" data-bs-target="#createNewPostModal">Create New Post</button>
<button type="button" class="btn btn-secondary me-2" data-bs-toggle="modal" data-bs-target="#createNewPostModal">Create New Post</button>
</div>
<div class="d-flex justify-content-center">
</div>
Expand Down

0 comments on commit 40ff43b

Please sign in to comment.