-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: /signup work correctly * WIP: /signup work correctly * WIP: /login work correctly * fixed mistakes * some comments deleted, renamed folder and package * used gorilla/mux * fixed mistakes * fixed mistakes * fixing * merged dev+auth --------- Co-authored-by: timurIsaevIY <118661906+timurIsaevIY@users.noreply.github.com> Co-authored-by: timurIsaevIY <isaevtimur2016@gmail.com>
- Loading branch information
1 parent
5cba629
commit d4c5666
Showing
14 changed files
with
390 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
module TripAdvisor | ||
go 1.23.0 | ||
require github.com/gorilla/mux v1.8.1 | ||
module 2024_2_ThereWillBeName | ||
|
||
go 1.23.1 | ||
|
||
require ( | ||
github.com/dgrijalva/jwt-go v3.2.0+incompatible | ||
github.com/gorilla/mux v1.8.1 | ||
github.com/lib/pq v1.10.9 | ||
golang.org/x/crypto v0.27.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= | ||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= | ||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= | ||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= | ||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= | ||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= | ||
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A= | ||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type User struct { | ||
ID int64 `json:"id" db:"id"` | ||
Login string `json:"login" db:"login"` | ||
Password string `json:"-" db:"password"` | ||
CreatedAt time.Time `json:"created_at" db:"created_at"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package http | ||
|
||
import ( | ||
"2024_2_ThereWillBeName/internal/models" | ||
"2024_2_ThereWillBeName/internal/pkg/auth" | ||
"2024_2_ThereWillBeName/internal/pkg/jwt" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type Handler struct { | ||
usecase auth.AuthUsecase | ||
jwt *jwt.JWT | ||
} | ||
|
||
func NewAuthHandler(usecase auth.AuthUsecase, jwt *jwt.JWT) *Handler { | ||
return &Handler{ | ||
usecase: usecase, | ||
jwt: jwt, | ||
} | ||
} | ||
|
||
func (h *Handler) SignUp(w http.ResponseWriter, r *http.Request) { | ||
var credentials struct { | ||
Login string `json:"login"` | ||
Password string `json:"password"` | ||
} | ||
|
||
if err := json.NewDecoder(r.Body).Decode(&credentials); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
user := models.User{ | ||
Login: credentials.Login, | ||
Password: credentials.Password, | ||
} | ||
|
||
if err := h.usecase.SignUp(context.Background(), user); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
} | ||
|
||
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) { | ||
var credentials struct { | ||
Login string `json:"login"` | ||
Password string `json:"password"` | ||
} | ||
if err := json.NewDecoder(r.Body).Decode(&credentials); err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
|
||
token, err := h.usecase.Login(context.Background(), credentials.Login, credentials.Password) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
http.SetCookie(w, &http.Cookie{ | ||
Name: "token", | ||
Value: token, | ||
Path: "/", | ||
HttpOnly: true, | ||
Secure: false, | ||
}) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) { | ||
http.SetCookie(w, &http.Cookie{ | ||
Name: "token", | ||
Value: "", | ||
Path: "/", | ||
HttpOnly: true, | ||
Secure: false, | ||
}) | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package auth | ||
|
||
import ( | ||
"2024_2_ThereWillBeName/internal/models" | ||
"context" | ||
) | ||
|
||
type AuthUsecase interface { | ||
SignUp(ctx context.Context, user models.User) error | ||
Login(ctx context.Context, login, password string) (string, error) // Возвращает JWT токен | ||
} | ||
|
||
type AuthRepo interface { | ||
CreateUser(ctx context.Context, user models.User) error | ||
GetUserByLogin(ctx context.Context, login string) (models.User, error) | ||
UpdateUser(ctx context.Context, user models.User) error | ||
DeleteUser(ctx context.Context, id string) error | ||
GetUsers(ctx context.Context, count, offset int64) ([]models.User, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package repo | ||
|
||
import ( | ||
"2024_2_ThereWillBeName/internal/models" | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
type RepositoryImpl struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewAuthRepository(db *sql.DB) *RepositoryImpl { | ||
return &RepositoryImpl{db: db} | ||
} | ||
|
||
func (r *RepositoryImpl) CreateUser(ctx context.Context, user models.User) error { | ||
query := "INSERT INTO users (login, password, created_at) VALUES ($1, $2, NOW())" | ||
_, err := r.db.ExecContext(ctx, query, user.Login, user.Password) | ||
return err | ||
} | ||
|
||
func (r *RepositoryImpl) GetUserByLogin(ctx context.Context, login string) (models.User, error) { | ||
var user models.User | ||
query := "SELECT id, login, password, created_at FROM users WHERE login = $1" | ||
row := r.db.QueryRowContext(ctx, query, login) | ||
err := row.Scan(&user.ID, &user.Login, &user.Password, &user.CreatedAt) | ||
if err != nil { | ||
if err == sql.ErrNoRows { | ||
return models.User{}, fmt.Errorf("user not found with login: %s", login) | ||
} | ||
return models.User{}, err | ||
} | ||
return user, nil | ||
} | ||
|
||
func (r *RepositoryImpl) UpdateUser(ctx context.Context, user models.User) error { | ||
query := "UPDATE users SET login = $1, password = $2 WHERE id = $3" | ||
_, err := r.db.ExecContext(ctx, query, user.Login, user.Password, user.ID) | ||
return err | ||
} | ||
|
||
func (r *RepositoryImpl) DeleteUser(ctx context.Context, id string) error { | ||
query := "DELETE FROM users WHERE id = $1" | ||
_, err := r.db.ExecContext(ctx, query, id) | ||
return err | ||
} | ||
|
||
func (r *RepositoryImpl) GetUsers(ctx context.Context, count, offset int64) ([]models.User, error) { | ||
query := "SELECT id, login, created_at FROM users LIMIT $1 OFFSET $2" | ||
rows, err := r.db.QueryContext(ctx, query, count, offset) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
var users []models.User | ||
for rows.Next() { | ||
var user models.User | ||
if err := rows.Scan(&user.ID, &user.Login, &user.CreatedAt); err != nil { | ||
return nil, err | ||
} | ||
users = append(users, user) | ||
} | ||
return users, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package usecase | ||
|
||
import ( | ||
"2024_2_ThereWillBeName/internal/models" | ||
"2024_2_ThereWillBeName/internal/pkg/auth" | ||
"2024_2_ThereWillBeName/internal/pkg/jwt" | ||
"context" | ||
"log" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
type AuthUsecaseImpl struct { | ||
repo auth.AuthRepo | ||
jwt *jwt.JWT | ||
} | ||
|
||
func NewAuthUsecase(repo auth.AuthRepo, jwt *jwt.JWT) *AuthUsecaseImpl { | ||
return &AuthUsecaseImpl{ | ||
repo: repo, | ||
jwt: jwt, | ||
} | ||
} | ||
|
||
func (a *AuthUsecaseImpl) SignUp(ctx context.Context, user models.User) error { | ||
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost) | ||
user.Password = string(hashedPassword) | ||
return a.repo.CreateUser(ctx, user) | ||
} | ||
|
||
func (a *AuthUsecaseImpl) Login(ctx context.Context, login, password string) (string, error) { | ||
user, err := a.repo.GetUserByLogin(ctx, login) | ||
|
||
if err != nil { | ||
log.Printf("Error retrieving user: %v\n", err) | ||
return "", err | ||
} | ||
|
||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil { | ||
log.Printf("Password mismatch: %v\n", err) | ||
return "", err | ||
} else { | ||
log.Println("Password match!") | ||
} | ||
|
||
return a.jwt.GenerateToken(uint(user.ID), user.Login) | ||
} |
Oops, something went wrong.