Profiles

This commit is contained in:
LittleSheep 2024-01-28 16:17:38 +08:00
parent a26fc8fb6e
commit d4aef5277f
8 changed files with 88 additions and 38 deletions

12
.idea/dataSources.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="hy_passport@localhost" uuid="49a1c31c-500d-4f9f-bbf4-b4ddc9f3dc56">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://localhost:5432/hy_passport</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>

View File

@ -9,6 +9,7 @@ func RunMigration(source *gorm.DB) error {
if err := source.AutoMigrate(
&models.Account{},
&models.AuthFactor{},
&models.AccountProfile{},
&models.AccountContact{},
&models.AuthSession{},
&models.AuthChallenge{},

View File

@ -19,10 +19,12 @@ type Account struct {
Name string `json:"name" gorm:"uniqueIndex"`
Nick string `json:"nick"`
State AccountState `json:"state"`
Profile AccountProfile `json:"profile"`
Session []AuthSession `json:"sessions"`
Challenges []AuthChallenge `json:"challenges"`
Factors []AuthFactor `json:"factors"`
Contacts []AccountContact `json:"contacts"`
ConfirmedAt *time.Time `json:"confirmed_at"`
Permissions datatypes.JSONType[[]string] `json:"permissions"`
}

14
pkg/models/profiles.go Normal file
View File

@ -0,0 +1,14 @@
package models
import "time"
type AccountProfile struct {
BaseModel
FirstName string `json:"first_name"`
MiddleName string `json:"middle_name"`
LastName string `json:"last_name"`
Experience uint64 `json:"experience"`
Birthday *time.Time `json:"birthday"`
AccountID uint `json:"account_id"`
}

View File

@ -3,10 +3,24 @@ package server
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"code.smartsheep.studio/hydrogen/passport/pkg/security"
"code.smartsheep.studio/hydrogen/passport/pkg/services"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm/clause"
)
func getPrincipal(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data models.Account
if err := database.C.Where(&models.Account{
BaseModel: models.BaseModel{ID: user.ID},
}).Preload(clause.Associations).First(&data).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(data)
}
func doRegister(c *fiber.Ctx) error {
var data struct {
Name string `json:"name"`
@ -19,28 +33,14 @@ func doRegister(c *fiber.Ctx) error {
return err
}
user := models.Account{
Name: data.Name,
Nick: data.Nick,
State: models.PendingAccountState,
Factors: []models.AuthFactor{
{
Type: models.PasswordAuthFactor,
Secret: security.HashPassword(data.Password),
},
},
Contacts: []models.AccountContact{
{
Type: models.EmailAccountContact,
Content: data.Email,
VerifiedAt: nil,
},
},
}
if err := database.C.Create(&user).Error; err != nil {
if user, err := services.CreateAccount(
data.Name,
data.Nick,
data.Email,
data.Password,
); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(user)
}
return c.JSON(user)
}

View File

@ -1,12 +0,0 @@
package server
import (
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"github.com/gofiber/fiber/v2"
)
func aboutMe(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
return c.JSON(user)
}

View File

@ -21,7 +21,7 @@ func NewServer() {
api := A.Group("/api").Name("API")
{
api.Get("/users/me", auth, aboutMe)
api.Get("/users/me", auth, getPrincipal)
api.Post("/users", doRegister)
api.Put("/auth", startChallenge)

View File

@ -1,10 +1,11 @@
package services
import (
"fmt"
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"code.smartsheep.studio/hydrogen/passport/pkg/security"
"fmt"
"gorm.io/datatypes"
)
func GetAccount(id uint) (models.Account, error) {
@ -36,3 +37,35 @@ func LookupAccount(id string) (models.Account, error) {
return account, fmt.Errorf("account was not found")
}
func CreateAccount(name, nick, email, password string) (models.Account, error) {
user := models.Account{
Name: name,
Nick: nick,
State: models.PendingAccountState,
Profile: models.AccountProfile{
Experience: 100,
},
Factors: []models.AuthFactor{
{
Type: models.PasswordAuthFactor,
Secret: security.HashPassword(password),
},
},
Contacts: []models.AccountContact{
{
Type: models.EmailAccountContact,
Content: email,
VerifiedAt: nil,
},
},
Permissions: datatypes.NewJSONType(make([]string, 0)),
ConfirmedAt: nil,
}
if err := database.C.Create(&user).Error; err != nil {
return user, err
} else {
return user, nil
}
}