signins
This commit is contained in:
@@ -10,24 +10,31 @@ import (
|
||||
"github.com/alexedwards/argon2id"
|
||||
)
|
||||
|
||||
var DB map[string]interface{}
|
||||
type User struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
// Other fields as needed
|
||||
}
|
||||
|
||||
var DB map[string]User
|
||||
|
||||
type GetService struct{}
|
||||
var Get = GetService{}
|
||||
|
||||
func (g GetService) User(id string) (map[string]interface{}, error) {
|
||||
raw, ok := DB[id]
|
||||
if !ok {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
userData, ok := raw.(map[string]interface{})
|
||||
log.Println(userData)
|
||||
if !ok {
|
||||
return nil, errors.New("user data is not in expected format")
|
||||
}
|
||||
func (g GetService) UserByEmail(email string) (map[string]interface{}, error) {
|
||||
for key, value := range DB {
|
||||
if value.Email == email {
|
||||
log.Println("found")
|
||||
return map[string]interface{}{
|
||||
"key": key,
|
||||
"email": value.Email,
|
||||
"password": value.Password,
|
||||
}, nil
|
||||
}
|
||||
fmt.Printf("Key: %s, Value: %v\n", key, value)
|
||||
}
|
||||
|
||||
return userData, nil
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
|
||||
func InitDB() error {
|
||||
@@ -38,19 +45,12 @@ func InitDB() error {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var data interface{}
|
||||
|
||||
err = json.NewDecoder(file).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding JSON:", err)
|
||||
return errors.New("Failed to decode db")
|
||||
}
|
||||
|
||||
result, ok := data.(map[string]interface{})
|
||||
if !ok {
|
||||
fmt.Println("Data is not a JSON object (map)")
|
||||
return errors.New("Db is in the wrong format")
|
||||
}
|
||||
var result map[string]User
|
||||
err = json.NewDecoder(file).Decode(&result)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding JSON:", err)
|
||||
return errors.New("failed to decode db")
|
||||
}
|
||||
|
||||
DB = result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user