Added buyer seller bolean
This commit is contained in:
@@ -3,6 +3,7 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -115,7 +116,7 @@ func UpdatePassword(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Login(c *gin.Context) {
|
func Login(c *gin.Context) {
|
||||||
var req models.User
|
var req models.LoginRequest
|
||||||
if err := c.BindJSON(&req); err != nil {
|
if err := c.BindJSON(&req); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Bad request"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Bad request"})
|
||||||
return
|
return
|
||||||
@@ -130,12 +131,13 @@ func Login(c *gin.Context) {
|
|||||||
if usr.CheckPassword(user.Password, req.Password) {
|
if usr.CheckPassword(user.Password, req.Password) {
|
||||||
if user.IsActive && user.LoginAttempts < 10 {
|
if user.IsActive && user.LoginAttempts < 10 {
|
||||||
// Proceed with creating JWT token and resetting login attempts
|
// Proceed with creating JWT token and resetting login attempts
|
||||||
// if len(user.Companies) == 0 {
|
log.Printf("Companies length %v", len(user.Companies))
|
||||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "User is not connected to a company"})
|
if len(user.Companies) == 0 {
|
||||||
// return
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "User is not connected to a company"})
|
||||||
// }
|
return
|
||||||
|
}
|
||||||
|
|
||||||
token, err := usr.CreateSessionToken(user.ID, 2)
|
token, err := usr.CreateSessionToken(user.ID, user.Companies[0].ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create JWT token"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create JWT token"})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ func SaveResetTokenToDB(userID uint, resetToken string) error {
|
|||||||
func GetUserByEmail(email string) (*models.User, error) {
|
func GetUserByEmail(email string) (*models.User, error) {
|
||||||
var user models.User
|
var user models.User
|
||||||
|
|
||||||
// Query the database for a user with the specified email
|
// Query the database for a user with the specified email and preload Companies
|
||||||
if err := shared.GetDb().Where("email = ?", email).First(&user).Error; err != nil {
|
if err := shared.GetDb().Preload("Companies").Where("email = ?", email).First(&user).Error; err != nil {
|
||||||
if gorm.IsRecordNotFoundError(err) {
|
if gorm.IsRecordNotFoundError(err) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
9
main.go
9
main.go
@@ -40,8 +40,15 @@ func main() {
|
|||||||
|
|
||||||
// Allow Admin to manage User resource
|
// Allow Admin to manage User resource
|
||||||
company := Admin.AddResource(&models.Company{})
|
company := Admin.AddResource(&models.Company{})
|
||||||
|
|
||||||
|
userResource := Admin.AddResource(&models.User{})
|
||||||
|
|
||||||
|
// Hide the Password field in the QOR admin UI
|
||||||
|
userResource.EditAttrs("-Password")
|
||||||
|
userResource.NewAttrs("-Password")
|
||||||
|
userResource.ShowAttrs("-Password")
|
||||||
// Add User and Device resources
|
// Add User and Device resources
|
||||||
Admin.AddResource(&models.User{})
|
// Admin.AddResource(&models.User{})
|
||||||
Admin.AddResource(&models.Device{})
|
Admin.AddResource(&models.Device{})
|
||||||
Admin.AddResource(&models.ProductTemplate{})
|
Admin.AddResource(&models.ProductTemplate{})
|
||||||
company.Meta(&admin.Meta{Name: "Users", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
company.Meta(&admin.Meta{Name: "Users", Config: &admin.SelectManyConfig{SelectMode: "bottom_sheet"}})
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type Company struct {
|
|||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
Users []User `gorm:"many2many:user_companies;"`
|
Users []User `gorm:"many2many:user_companies;"`
|
||||||
Devices []Device `json:"devices"`
|
Devices []Device `json:"devices"`
|
||||||
|
IsBuyer bool `json:"isBuyer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type School struct {
|
type School struct {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package models
|
|||||||
type User struct {
|
type User struct {
|
||||||
BaseModel
|
BaseModel
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"-"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
IsActive bool `json:"isActive" gorm:"default:false"`
|
IsActive bool `json:"isActive" gorm:"default:false"`
|
||||||
@@ -13,6 +13,11 @@ type User struct {
|
|||||||
LoginAttempts int `gorm:"default:0"`
|
LoginAttempts int `gorm:"default:0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LoginRequest struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
type ResetPasswordRequest struct {
|
type ResetPasswordRequest struct {
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user