sdad / controllers /auth.go
zerolin1024's picture
Upload 16 files
2196bfe verified
package controllers
import (
"net/http"
"os"
"time"
"uptime/backend/database"
"uptime/backend/models"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
"golang.org/x/crypto/bcrypt"
)
func Login(c *gin.Context) {
var body struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := c.BindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to read body"})
return
}
var user models.User
database.DB.First(&user, "username = ?", body.Username)
if user.ID == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid username or password"})
return
}
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(body.Password))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid username or password"})
return
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": user.ID,
"exp": time.Now().Add(time.Hour * 24 * 30).Unix(),
})
tokenString, err := token.SignedString([]byte(os.Getenv("SECRET")))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to create token"})
return
}
c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie("Authorization", tokenString, 3600*24*30, "/", "", false, true)
c.JSON(http.StatusOK, gin.H{})
}