|
|
@@ -2,43 +2,53 @@ package controllers
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"net/http"
|
|
|
+ "pong-auth/args"
|
|
|
"pong-auth/db"
|
|
|
+ "pong-auth/middleware"
|
|
|
"pong-auth/models"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
+ "github.com/jackc/pgx/v5"
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
)
|
|
|
|
|
|
-var jwtSecret []byte = []byte("secret")
|
|
|
+func resWithError(statusCode int, err error, c *gin.Context) {
|
|
|
+ c.JSON(statusCode, gin.H{
|
|
|
+ "err": err.Error(),
|
|
|
+ })
|
|
|
+}
|
|
|
|
|
|
func UserSignup(c *gin.Context) {
|
|
|
dto := models.UserSignupDto{}
|
|
|
err := c.ShouldBindJSON(&dto)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
dto.Username = strings.TrimSpace(dto.Username)
|
|
|
if dto.Username == "" {
|
|
|
- c.JSON(http.StatusBadRequest, gin.H{
|
|
|
- "err": "Username was empty",
|
|
|
- })
|
|
|
+ resWithError(
|
|
|
+ http.StatusBadRequest,
|
|
|
+ errors.New("Username was empty"),
|
|
|
+ c,
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
|
|
|
hashed, err := bcrypt.GenerateFromPassword([]byte(dto.Password), bcrypt.MinCost)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
conn, err := db.Connect()
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
defer conn.Close(context.Background())
|
|
|
@@ -52,9 +62,7 @@ func UserSignup(c *gin.Context) {
|
|
|
user := models.User{}
|
|
|
err = rows.Scan(&user.Id, &user.Username, &user.Password)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
- "err": err.Error(),
|
|
|
- })
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -65,13 +73,13 @@ func UserLogin(c *gin.Context) {
|
|
|
dto := models.UserSignupDto{}
|
|
|
err := c.ShouldBindJSON(&dto)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
conn, err := db.Connect()
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
defer conn.Close(context.Background())
|
|
|
@@ -85,7 +93,7 @@ func UserLogin(c *gin.Context) {
|
|
|
user := models.User{}
|
|
|
err = row.Scan(&user.Id, &user.Username, &user.Password)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -94,9 +102,11 @@ func UserLogin(c *gin.Context) {
|
|
|
[]byte(dto.Password),
|
|
|
)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
- "err": "Wrong password",
|
|
|
- })
|
|
|
+ resWithError(
|
|
|
+ http.StatusUnauthorized,
|
|
|
+ errors.New("Wrong password"),
|
|
|
+ c,
|
|
|
+ )
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -105,9 +115,9 @@ func UserLogin(c *gin.Context) {
|
|
|
Subject: user.Id,
|
|
|
}
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
- tokenStr, err := token.SignedString(jwtSecret)
|
|
|
+ tokenStr, err := token.SignedString(args.Values.JwtSecret)
|
|
|
if err != nil {
|
|
|
- c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -121,5 +131,40 @@ func UserLogin(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func UserMe(c *gin.Context) {
|
|
|
+ userId, ok := middleware.UserIdGet(c)
|
|
|
+ if !ok {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ conn, err := db.Connect()
|
|
|
+ if err != nil {
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer conn.Close(context.Background())
|
|
|
+
|
|
|
+ row := conn.QueryRow(
|
|
|
+ context.Background(),
|
|
|
+ "SELECT * FROM users WHERE id = $1",
|
|
|
+ userId,
|
|
|
+ )
|
|
|
+
|
|
|
+ user := models.User{}
|
|
|
+ err = row.Scan(&user.Id, &user.Username, &user.Password)
|
|
|
+ if err != nil {
|
|
|
+ if errors.Is(err, pgx.ErrNoRows) {
|
|
|
+ resWithError(http.StatusNotFound, errors.New("User not found"), c)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ resWithError(http.StatusInternalServerError, err, c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ dto := models.UserMeDto{
|
|
|
+ Id: user.Id,
|
|
|
+ Username: user.Username,
|
|
|
+ }
|
|
|
|
|
|
+ c.JSON(http.StatusOK, dto)
|
|
|
}
|