mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
fixes on structuring
This commit is contained in:
@@ -22,15 +22,15 @@ func NewLoginController(rs *services.UsersService, s *gin.RouterGroup) *LoginCon
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *LoginController) Post(c *gin.Context) {
|
func (rc *LoginController) Post(c *gin.Context) {
|
||||||
loginBody := new(models.LoginModel)
|
body := new(models.LoginModel)
|
||||||
if err := c.ShouldBindJSON(&loginBody); err != nil {
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
returnedUser, returnException := rc.UsersService.Login(loginBody)
|
returnedUser, exceptionReturn := rc.UsersService.Login(body)
|
||||||
|
|
||||||
if returnException.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
c.JSON(returnException.StatusCode, returnException)
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, returnedUser)
|
c.JSON(200, returnedUser)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/services"
|
"wallet-api/pkg/services"
|
||||||
"wallet-api/pkg/utl/common"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -23,23 +22,18 @@ func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *Regis
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *RegisterController) Post(c *gin.Context) {
|
func (rc *RegisterController) Post(c *gin.Context) {
|
||||||
registerBody := createUserModel()
|
body := new(models.UserModel)
|
||||||
if err := c.ShouldBindJSON(®isterBody); err != nil {
|
body.Init()
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
returnedUser, returnException := rc.UsersService.Create(®isterBody)
|
returnedUser, exceptionReturn := rc.UsersService.Create(body)
|
||||||
|
|
||||||
if returnException.Message != "" {
|
if exceptionReturn.Message != "" {
|
||||||
c.JSON(returnException.StatusCode, returnException)
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
} else {
|
} else {
|
||||||
c.JSON(200, returnedUser.Payload())
|
c.JSON(200, returnedUser.Payload())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUserModel() models.UserModel {
|
|
||||||
commonModel := common.CreateDbModel()
|
|
||||||
userModel := models.UserModel{CommonModel: commonModel}
|
|
||||||
return userModel
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,58 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/utl/configs"
|
||||||
|
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
func Auth(c *gin.Context) {
|
func Auth(c *gin.Context) {
|
||||||
|
exceptionReturn := new(models.ExceptionModel)
|
||||||
|
tokenString := ExtractToken(c)
|
||||||
|
secret := os.Getenv("ACCESS_SECRET")
|
||||||
|
if secret == "" {
|
||||||
|
secret = configs.Secret
|
||||||
|
}
|
||||||
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
||||||
|
println(ok)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||||
|
}
|
||||||
|
return []byte(secret), nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
exceptionReturn.ErrorCode = "401001"
|
||||||
|
exceptionReturn.StatusCode = 401
|
||||||
|
exceptionReturn.Message = "Invalid token"
|
||||||
|
c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
|
}
|
||||||
|
claims, ok := token.Claims.(jwt.MapClaims)
|
||||||
|
if ok && token.Valid {
|
||||||
|
userId, _ := claims["id"].(string)
|
||||||
|
|
||||||
|
authModel := new(models.AuthModel)
|
||||||
|
authModel.Id = userId
|
||||||
|
|
||||||
|
c.Set("auth", authModel)
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExtractToken(c *gin.Context) string {
|
||||||
|
bearerToken := c.GetHeader("Authorization")
|
||||||
|
tokenArr := strings.Split(bearerToken, " ")
|
||||||
|
if len(tokenArr) == 2 {
|
||||||
|
bearerCheck := strings.ToLower(tokenArr[0])
|
||||||
|
if bearerCheck == "bearer" {
|
||||||
|
return tokenArr[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,3 +8,7 @@ type LoginModel struct {
|
|||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AuthModel struct {
|
||||||
|
Id string
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,20 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
type CommonModel struct {
|
type CommonModel struct {
|
||||||
Id string `json:"id" pg:"id"`
|
Id string `json:"id" pg:"id,pk"`
|
||||||
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
|
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
|
||||||
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
|
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cm *CommonModel) Init() {
|
||||||
|
date := time.Now()
|
||||||
|
cm.Id = uuid.NewString()
|
||||||
|
cm.DateCreated = date
|
||||||
|
cm.DateUpdated = date
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package models
|
|||||||
type UserModel struct {
|
type UserModel struct {
|
||||||
tableName struct{} `pg:"users,alias:users"`
|
tableName struct{} `pg:"users,alias:users"`
|
||||||
CommonModel
|
CommonModel
|
||||||
Username string `json:"username"`
|
Username string `json:"username" pg:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password" pg:"password"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email" pg:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserReturnInfoModel struct {
|
type UserReturnInfoModel struct {
|
||||||
@@ -15,11 +15,11 @@ type UserReturnInfoModel struct {
|
|||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (um *UserModel) Payload() UserReturnInfoModel {
|
func (um *UserModel) Payload() *UserReturnInfoModel {
|
||||||
payload := UserReturnInfoModel{
|
payload := new(UserReturnInfoModel)
|
||||||
CommonModel: um.CommonModel,
|
payload.CommonModel = um.CommonModel
|
||||||
Username: um.Username,
|
payload.Username = um.Username
|
||||||
Email: um.Email,
|
payload.Email = um.Email
|
||||||
}
|
|
||||||
return payload
|
return payload
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/utl/common"
|
"wallet-api/pkg/utl/common"
|
||||||
|
"wallet-api/pkg/utl/configs"
|
||||||
|
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
@@ -16,16 +17,16 @@ type UsersService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, models.ExceptionModel) {
|
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, *models.ExceptionModel) {
|
||||||
var checkModel models.UserModel
|
check := new(models.UserModel)
|
||||||
var exceptionReturn models.ExceptionModel
|
exceptionReturn := new(models.ExceptionModel)
|
||||||
|
|
||||||
us.Db.Model(&checkModel).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||||
if checkModel.Username != "" || checkModel.Email != "" {
|
if check.Username != "" || check.Email != "" {
|
||||||
exceptionReturn.Message = "User already exists"
|
exceptionReturn.Message = "User already exists"
|
||||||
exceptionReturn.ErrorCode = "400101"
|
exceptionReturn.ErrorCode = "400101"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
return &checkModel, exceptionReturn
|
return check, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||||
@@ -43,27 +44,27 @@ func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserMode
|
|||||||
return registerBody, exceptionReturn
|
return registerBody, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Login(loginBody *models.LoginModel) (models.TokenModel, models.ExceptionModel) {
|
func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel, *models.ExceptionModel) {
|
||||||
var checkModel models.UserModel
|
check := new(models.UserModel)
|
||||||
var exceptionReturn models.ExceptionModel
|
exceptionReturn := new(models.ExceptionModel)
|
||||||
var tokenPayload models.TokenModel
|
tokenPayload := new(models.TokenModel)
|
||||||
|
|
||||||
us.Db.Model(&checkModel).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||||
if checkModel.Email == "" {
|
if check.Email == "" {
|
||||||
exceptionReturn.Message = "Email not found"
|
exceptionReturn.Message = "Email not found"
|
||||||
exceptionReturn.ErrorCode = "400103"
|
exceptionReturn.ErrorCode = "400103"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
if bcrypt.CompareHashAndPassword([]byte(checkModel.Password), []byte(loginBody.Password)) != nil {
|
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
||||||
exceptionReturn.Message = "Incorrect password"
|
exceptionReturn.Message = "Incorrect password"
|
||||||
exceptionReturn.ErrorCode = "400104"
|
exceptionReturn.ErrorCode = "400104"
|
||||||
exceptionReturn.StatusCode = 400
|
exceptionReturn.StatusCode = 400
|
||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := CreateToken(checkModel)
|
token, err := CreateToken(check)
|
||||||
common.CheckError(err)
|
common.CheckError(err)
|
||||||
|
|
||||||
tokenPayload.Token = token
|
tokenPayload.Token = token
|
||||||
@@ -71,15 +72,15 @@ func (us *UsersService) Login(loginBody *models.LoginModel) (models.TokenModel,
|
|||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateToken(user models.UserModel) (string, error) {
|
func CreateToken(user *models.UserModel) (string, error) {
|
||||||
atClaims := jwt.MapClaims{}
|
atClaims := jwt.MapClaims{}
|
||||||
atClaims["authorized"] = true
|
atClaims["authorized"] = true
|
||||||
atClaims["id"] = user.Id
|
atClaims["id"] = user.Id
|
||||||
atClaims["exp"] = time.Now().Add(time.Minute * 15).Unix()
|
atClaims["exp"] = time.Now().Add(time.Minute).Unix()
|
||||||
|
|
||||||
secret := os.Getenv("ACCESS_SECRET")
|
secret := os.Getenv("ACCESS_SECRET")
|
||||||
if secret == "" {
|
if secret == "" {
|
||||||
secret = "Dond3sta"
|
secret = configs.Secret
|
||||||
}
|
}
|
||||||
|
|
||||||
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
|
||||||
|
|||||||
@@ -2,10 +2,6 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CheckError(err error) {
|
func CheckError(err error) {
|
||||||
@@ -13,13 +9,3 @@ func CheckError(err error) {
|
|||||||
log.Fatalf("Error occured. %v", err)
|
log.Fatalf("Error occured. %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDbModel() models.CommonModel {
|
|
||||||
date := time.Now()
|
|
||||||
dbModel := models.CommonModel{
|
|
||||||
Id: uuid.NewString(),
|
|
||||||
DateCreated: date,
|
|
||||||
DateUpdated: date,
|
|
||||||
}
|
|
||||||
return dbModel
|
|
||||||
}
|
|
||||||
|
|||||||
17
pkg/utl/common/embeds.go
Normal file
17
pkg/utl/common/embeds.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateEmbed(qr *orm.Query, embed string) *orm.Query{
|
||||||
|
if embed != "" {
|
||||||
|
rels := strings.Split(embed, ",")
|
||||||
|
for _, rel := range rels {
|
||||||
|
qr = qr.Relation(rel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return qr
|
||||||
|
}
|
||||||
@@ -3,4 +3,5 @@ package configs
|
|||||||
const (
|
const (
|
||||||
Version = "0.0.1"
|
Version = "0.0.1"
|
||||||
Prefix = "v1"
|
Prefix = "v1"
|
||||||
|
Secret = "Donde4sta"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user