|
@@ -2,19 +2,36 @@ package controllers
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"context"
|
|
"context"
|
|
|
- "fmt"
|
|
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"pong-auth/db"
|
|
"pong-auth/db"
|
|
|
"pong-auth/models"
|
|
"pong-auth/models"
|
|
|
|
|
+ "strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
+ "github.com/golang-jwt/jwt/v5"
|
|
|
|
|
+ "golang.org/x/crypto/bcrypt"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+var jwtSecret []byte = []byte("secret")
|
|
|
|
|
+
|
|
|
func UserSignup(c *gin.Context) {
|
|
func UserSignup(c *gin.Context) {
|
|
|
dto := models.UserSignupDto{}
|
|
dto := models.UserSignupDto{}
|
|
|
err := c.ShouldBindJSON(&dto)
|
|
err := c.ShouldBindJSON(&dto)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Println("bind")
|
|
|
|
|
|
|
+ c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ dto.Username = strings.TrimSpace(dto.Username)
|
|
|
|
|
+ if dto.Username == "" {
|
|
|
|
|
+ c.JSON(http.StatusBadRequest, gin.H{
|
|
|
|
|
+ "err": "Username was empty",
|
|
|
|
|
+ })
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ hashed, err := bcrypt.GenerateFromPassword([]byte(dto.Password), bcrypt.MinCost)
|
|
|
|
|
+ if err != nil {
|
|
|
c.JSON(http.StatusInternalServerError, err.Error())
|
|
c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -27,15 +44,14 @@ func UserSignup(c *gin.Context) {
|
|
|
defer conn.Close(context.Background())
|
|
defer conn.Close(context.Background())
|
|
|
rows := conn.QueryRow(
|
|
rows := conn.QueryRow(
|
|
|
context.Background(),
|
|
context.Background(),
|
|
|
- "INSERT INTO users (email, password) VALUES ($1, $2) RETURNING *",
|
|
|
|
|
- dto.Email,
|
|
|
|
|
- dto.Password,
|
|
|
|
|
|
|
+ "INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *",
|
|
|
|
|
+ dto.Username,
|
|
|
|
|
+ string(hashed),
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
user := models.User{}
|
|
user := models.User{}
|
|
|
- err = rows.Scan(&user.Id, &user.Email, &user.Password)
|
|
|
|
|
|
|
+ err = rows.Scan(&user.Id, &user.Username, &user.Password)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Printf("scan: %+v\n", err)
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
"err": err.Error(),
|
|
"err": err.Error(),
|
|
|
})
|
|
})
|
|
@@ -46,5 +62,64 @@ func UserSignup(c *gin.Context) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func UserLogin(c *gin.Context) {
|
|
func UserLogin(c *gin.Context) {
|
|
|
|
|
+ dto := models.UserSignupDto{}
|
|
|
|
|
+ err := c.ShouldBindJSON(&dto)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ conn, err := db.Connect()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ defer conn.Close(context.Background())
|
|
|
|
|
+
|
|
|
|
|
+ row := conn.QueryRow(
|
|
|
|
|
+ context.Background(),
|
|
|
|
|
+ "SELECT * FROM users WHERE username = $1",
|
|
|
|
|
+ dto.Username,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ user := models.User{}
|
|
|
|
|
+ err = row.Scan(&user.Id, &user.Username, &user.Password)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ err = bcrypt.CompareHashAndPassword(
|
|
|
|
|
+ []byte(user.Password),
|
|
|
|
|
+ []byte(dto.Password),
|
|
|
|
|
+ )
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
|
|
+ "err": "Wrong password",
|
|
|
|
|
+ })
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ claims := jwt.RegisteredClaims{
|
|
|
|
|
+ Issuer: "pong-auth-server",
|
|
|
|
|
+ Subject: user.Id,
|
|
|
|
|
+ }
|
|
|
|
|
+ token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
|
+ tokenStr, err := token.SignedString(jwtSecret)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ c.JSON(http.StatusOK, models.UserLoginOutDto{
|
|
|
|
|
+ Me: models.UserMeDto{
|
|
|
|
|
+ Id: user.Id,
|
|
|
|
|
+ Username: user.Username,
|
|
|
|
|
+ },
|
|
|
|
|
+ Jwt: tokenStr,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func UserMe(c *gin.Context) {
|
|
|
|
|
|
|
|
}
|
|
}
|