Auth Login writed. Not checked.
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"api/src/models"
|
||||||
|
"api/src/storages/psql"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var jwtKey = []byte(os.Getenv("SECRET_KEY"))
|
||||||
|
|
||||||
|
func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var creds models.Crenetials
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// check user
|
||||||
|
var user models.User
|
||||||
|
if result := psql.PSQL_GORM_DB.Where("username = ?", creds.Email).First(&user); result.Error != nil || !checkPasswordHash(creds.Password, user.Password) {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// create jwt token
|
||||||
|
expirationtime := time.Now().Add(5 * time.Minute)
|
||||||
|
claims := &models.Claims{
|
||||||
|
RegisteredClaims: jwt.RegisteredClaims{
|
||||||
|
ExpiresAt: jwt.NewNumericDate(expirationtime),
|
||||||
|
},
|
||||||
|
Email: user.Email,
|
||||||
|
}
|
||||||
|
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
tokenString, err := token.SignedString(jwtKey)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.SetCookie(w, &http.Cookie{
|
||||||
|
Name: "token",
|
||||||
|
Value: tokenString,
|
||||||
|
Expires: expirationtime,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkPasswordHash(password, hash string) bool {
|
||||||
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
@@ -23,7 +23,12 @@ func Register(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := models.User{Email: Crenetials.Email, Password: hashedPassword}
|
user := models.User{
|
||||||
|
Name: Crenetials.Name,
|
||||||
|
Email: Crenetials.Email,
|
||||||
|
Password: hashedPassword,
|
||||||
|
Phone: Crenetials.Phone,
|
||||||
|
}
|
||||||
if result := psql.PSQL_GORM_DB.Create(&user); result.Error != nil {
|
if result := psql.PSQL_GORM_DB.Create(&user); result.Error != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
@@ -35,12 +40,3 @@ func hashPassword(password string) (string, error) {
|
|||||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||||
return string(bytes), err
|
return string(bytes), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkPasswordHash(password, hash string) bool {
|
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Login(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package initializers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"api/src/storages/psql"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gorm.io/driver/postgres"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func InitDBconnection() {
|
||||||
|
slog.Info("Init DB connection")
|
||||||
|
dsn := fmt.Sprintf(
|
||||||
|
"host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Yekaterinburg",
|
||||||
|
os.Getenv("PGHOST"),
|
||||||
|
os.Getenv("PGUSER"),
|
||||||
|
os.Getenv("PGPASSWORD"),
|
||||||
|
os.Getenv("PGDATABASE"),
|
||||||
|
os.Getenv("PGPORT"),
|
||||||
|
)
|
||||||
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||||
|
Logger: logger.Default.LogMode(logger.Info),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to connect database", "error", err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
psql.PSQL_GORM_DB = db
|
||||||
|
sql, err := db.DB()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to get database", "error", err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
err = sql.Ping()
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("failed to ping database", "error", err)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
slog.Info("connected to database")
|
||||||
|
}
|
||||||
@@ -2,16 +2,10 @@ package initializers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"api/src/auth"
|
"api/src/auth"
|
||||||
"api/src/storages/psql"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/driver/postgres"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"gorm.io/gorm/logger"
|
|
||||||
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
@@ -20,37 +14,6 @@ import (
|
|||||||
|
|
||||||
var Done = make(chan bool)
|
var Done = make(chan bool)
|
||||||
|
|
||||||
func InitDBconnection() {
|
|
||||||
slog.Info("Init DB connection")
|
|
||||||
dsn := fmt.Sprintf(
|
|
||||||
"host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Yekaterinburg",
|
|
||||||
os.Getenv("PGHOST"),
|
|
||||||
os.Getenv("PGUSER"),
|
|
||||||
os.Getenv("PGPASSWORD"),
|
|
||||||
os.Getenv("PGDATABASE"),
|
|
||||||
os.Getenv("PGPORT"),
|
|
||||||
)
|
|
||||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
|
||||||
Logger: logger.Default.LogMode(logger.Info),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("failed to connect database", "error", err)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
psql.PSQL_GORM_DB = db
|
|
||||||
sql, err := db.DB()
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("failed to get database", "error", err)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
err = sql.Ping()
|
|
||||||
if err != nil {
|
|
||||||
slog.Error("failed to ping database", "error", err)
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
slog.Info("connected to database")
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitChiRouting() {
|
func InitChiRouting() {
|
||||||
slog.Info("Init routing")
|
slog.Info("Init routing")
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
@@ -1,16 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var jwtKey = []byte(os.Getenv("SECRET_KEY"))
|
|
||||||
|
|
||||||
type Crenetials struct {
|
type Crenetials struct {
|
||||||
|
Name string `json:"name"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
|
Phone string `json:"phone"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Claims struct {
|
type Claims struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user