Модуль авторизаций
This commit is contained in:
parent
d80643f9e8
commit
bafeb1c100
2
go-auth/.gitignore
vendored
Normal file
2
go-auth/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.env
|
||||
go.mod
|
3
go-auth/README.md
Normal file
3
go-auth/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# PIbd-42_Kashin_M.I_FinalQualifyingWork
|
||||
|
||||
Модуль "Авторизация"
|
138
go-auth/controllers/authController.go
Normal file
138
go-auth/controllers/authController.go
Normal file
@ -0,0 +1,138 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"log"
|
||||
"main/database"
|
||||
"main/models"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// Глобальная переменная для хранения ключа
|
||||
var SecretKey string
|
||||
|
||||
func init() {
|
||||
// Загружаем .env файл
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка загрузки .env файла")
|
||||
}
|
||||
|
||||
// Читаем ключ из переменной окружения
|
||||
SecretKey = os.Getenv("JWT_SECRET_KEY")
|
||||
if SecretKey == "" {
|
||||
log.Fatal("JWT_SECRET_KEY не задан в .env")
|
||||
}
|
||||
}
|
||||
|
||||
// Регистрируем пользователя
|
||||
func Register(c *fiber.Ctx) error {
|
||||
var data map[string]string
|
||||
if err := c.BodyParser(&data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if data["password"] == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Password is required"})
|
||||
}
|
||||
|
||||
// Проверка на существующего пользователя
|
||||
var existingUser models.User
|
||||
if err := database.DB.Where("email = ?", data["email"]).First(&existingUser).Error; err == nil {
|
||||
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"message": "Email already taken"})
|
||||
}
|
||||
|
||||
password, _ := bcrypt.GenerateFromPassword([]byte(data["password"]), bcrypt.DefaultCost)
|
||||
user := models.User{
|
||||
Name: data["name"],
|
||||
Email: data["email"],
|
||||
Password: password,
|
||||
}
|
||||
|
||||
database.DB.Create(&user)
|
||||
|
||||
return c.JSON(user)
|
||||
}
|
||||
|
||||
// Логиним пользователя
|
||||
func Login(c *fiber.Ctx) error {
|
||||
var data map[string]string
|
||||
if err := c.BodyParser(&data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var user models.User
|
||||
database.DB.Where("email = ?", data["email"]).First(&user)
|
||||
|
||||
if user.Id == 0 {
|
||||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"message": "User not found"})
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword(user.Password, []byte(data["password"])); err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Incorrect password"})
|
||||
}
|
||||
|
||||
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
|
||||
Issuer: strconv.Itoa(int(user.Id)),
|
||||
ExpiresAt: time.Now().Add(time.Hour * 12).Unix(),
|
||||
})
|
||||
|
||||
token, err := claims.SignedString([]byte(SecretKey))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"message": "Token invalid"})
|
||||
}
|
||||
|
||||
cookie := fiber.Cookie{
|
||||
Name: "jwt",
|
||||
Value: token,
|
||||
Expires: time.Now().Add(time.Hour * 12),
|
||||
HTTPOnly: true,
|
||||
SameSite: "Strict",
|
||||
}
|
||||
c.Cookie(&cookie)
|
||||
|
||||
return c.JSON(fiber.Map{"message": "Login success"})
|
||||
}
|
||||
|
||||
// Получаем пользователя по токену
|
||||
func User(c *fiber.Ctx) error {
|
||||
cookie := c.Cookies("jwt")
|
||||
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(SecretKey), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Unauthorized"})
|
||||
}
|
||||
|
||||
claims := token.Claims.(*jwt.StandardClaims)
|
||||
if claims.ExpiresAt < time.Now().Unix() {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"message": "Token expired"})
|
||||
}
|
||||
|
||||
var user models.User
|
||||
database.DB.Where("id = ?", claims.Issuer).First(&user)
|
||||
|
||||
return c.JSON(user)
|
||||
}
|
||||
|
||||
// Логаут
|
||||
func Logout(c *fiber.Ctx) error {
|
||||
cookie := fiber.Cookie{
|
||||
Name: "jwt",
|
||||
Value: "",
|
||||
Expires: time.Now().Add(-time.Hour),
|
||||
HTTPOnly: true,
|
||||
SameSite: "Strict",
|
||||
}
|
||||
|
||||
c.Cookie(&cookie)
|
||||
|
||||
return c.JSON(fiber.Map{"message": "Logout success"})
|
||||
}
|
32
go-auth/database/connection.go
Normal file
32
go-auth/database/connection.go
Normal file
@ -0,0 +1,32 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"github.com/joho/godotenv"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func Connect() {
|
||||
// Загружаем .env
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка загрузки .env")
|
||||
}
|
||||
|
||||
dsn := os.Getenv("DB_URL")
|
||||
if dsn == "" {
|
||||
log.Fatal("Переменная DB_URL не задана")
|
||||
}
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка подключения к БД:", err)
|
||||
}
|
||||
|
||||
DB = db
|
||||
log.Println("Подключение к БД успешно!")
|
||||
}
|
32
go-auth/main.go
Normal file
32
go-auth/main.go
Normal file
@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/joho/godotenv"
|
||||
"log"
|
||||
"main/database"
|
||||
"main/routes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// Загружаем .env
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("Ошибка загрузки .env")
|
||||
}
|
||||
|
||||
database.Connect()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(cors.New(cors.Config{
|
||||
AllowCredentials: true,
|
||||
AllowOrigins: "http://localhost:8000",
|
||||
}))
|
||||
|
||||
routes.Setup(app)
|
||||
|
||||
app.Listen(":8000")
|
||||
}
|
8
go-auth/models/user.go
Normal file
8
go-auth/models/user.go
Normal file
@ -0,0 +1,8 @@
|
||||
package models
|
||||
|
||||
type User struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email" gorm:"unique"`
|
||||
Password []byte `json:"-"`
|
||||
}
|
13
go-auth/routes/routes.go
Normal file
13
go-auth/routes/routes.go
Normal file
@ -0,0 +1,13 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"main/controllers"
|
||||
)
|
||||
|
||||
func Setup(app *fiber.App) {
|
||||
app.Post("/api/register", controllers.Register)
|
||||
app.Post("/api/login", controllers.Login)
|
||||
app.Get("/api/user", controllers.User)
|
||||
app.Post("/api/logout", controllers.Logout)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user