ee21f512a9
Phone becomes the required, unique login identifier; email and name turn optional. Login now accepts either via a single identifier field, refresh re-checks tenant membership, and the dev seed provides phone numbers. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
140 lines
3.9 KiB
Go
140 lines
3.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/repository/generated"
|
|
)
|
|
|
|
type UserRecord struct {
|
|
ID int64
|
|
Email *string
|
|
Phone string
|
|
PasswordHash string
|
|
Name *string
|
|
AvatarURL *string
|
|
Status string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type MembershipRecord struct {
|
|
ID int64
|
|
TenantID int64
|
|
UserID int64
|
|
PrimaryWorkspaceID int64
|
|
TenantRole string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
type AuthRepository interface {
|
|
GetUserByEmail(ctx context.Context, email string) (*UserRecord, error)
|
|
GetUserByLoginIdentifier(ctx context.Context, identifier string) (*UserRecord, error)
|
|
GetUserByID(ctx context.Context, id int64) (*UserRecord, error)
|
|
GetTenantMembership(ctx context.Context, userID int64) (*MembershipRecord, error)
|
|
GetTenantMembershipByTenantAndUser(ctx context.Context, tenantID, userID int64) (*MembershipRecord, error)
|
|
}
|
|
|
|
type authRepository struct {
|
|
q generated.Querier
|
|
}
|
|
|
|
func NewAuthRepository(db generated.DBTX) AuthRepository {
|
|
return &authRepository{q: newQuerier(db)}
|
|
}
|
|
|
|
func (r *authRepository) GetUserByEmail(ctx context.Context, email string) (*UserRecord, error) {
|
|
row, err := r.q.GetUserByEmail(ctx, pgtype.Text{String: email, Valid: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &UserRecord{
|
|
ID: row.ID,
|
|
Email: nullableText(row.Email),
|
|
Phone: row.Phone,
|
|
PasswordHash: row.PasswordHash,
|
|
Name: nullableText(row.Name),
|
|
AvatarURL: nullableText(row.AvatarUrl),
|
|
Status: row.Status,
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
|
}, nil
|
|
}
|
|
|
|
func (r *authRepository) GetUserByLoginIdentifier(ctx context.Context, identifier string) (*UserRecord, error) {
|
|
row, err := r.q.GetUserByLoginIdentifier(ctx, pgtype.Text{String: identifier, Valid: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &UserRecord{
|
|
ID: row.ID,
|
|
Email: nullableText(row.Email),
|
|
Phone: row.Phone,
|
|
PasswordHash: row.PasswordHash,
|
|
Name: nullableText(row.Name),
|
|
AvatarURL: nullableText(row.AvatarUrl),
|
|
Status: row.Status,
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
|
}, nil
|
|
}
|
|
|
|
func (r *authRepository) GetUserByID(ctx context.Context, id int64) (*UserRecord, error) {
|
|
row, err := r.q.GetUserByID(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &UserRecord{
|
|
ID: row.ID,
|
|
Email: nullableText(row.Email),
|
|
Phone: row.Phone,
|
|
PasswordHash: row.PasswordHash,
|
|
Name: nullableText(row.Name),
|
|
AvatarURL: nullableText(row.AvatarUrl),
|
|
Status: row.Status,
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
UpdatedAt: timeFromTimestamp(row.UpdatedAt),
|
|
}, nil
|
|
}
|
|
|
|
func (r *authRepository) GetTenantMembership(ctx context.Context, userID int64) (*MembershipRecord, error) {
|
|
row, err := r.q.GetTenantMembership(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MembershipRecord{
|
|
ID: row.ID,
|
|
TenantID: row.TenantID,
|
|
UserID: row.UserID,
|
|
PrimaryWorkspaceID: row.PrimaryWorkspaceID,
|
|
TenantRole: row.TenantRole,
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
}, nil
|
|
}
|
|
|
|
func (r *authRepository) GetTenantMembershipByTenantAndUser(ctx context.Context, tenantID, userID int64) (*MembershipRecord, error) {
|
|
row, err := r.q.GetTenantMembershipByTenantAndUser(ctx, generated.GetTenantMembershipByTenantAndUserParams{
|
|
TenantID: tenantID,
|
|
UserID: userID,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MembershipRecord{
|
|
ID: row.ID,
|
|
TenantID: row.TenantID,
|
|
UserID: row.UserID,
|
|
PrimaryWorkspaceID: row.PrimaryWorkspaceID,
|
|
TenantRole: row.TenantRole,
|
|
CreatedAt: timeFromTimestamp(row.CreatedAt),
|
|
}, nil
|
|
}
|