mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-01 05:51:39 +00:00
feat: impl bearer auth middleware
This commit is contained in:
19
apps/backend/internal/user/err.go
Normal file
19
apps/backend/internal/user/err.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type NotFoundError struct {
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
func newNotFoundError(id uuid.UUID) *NotFoundError {
|
||||
return &NotFoundError{id}
|
||||
}
|
||||
|
||||
func (e *NotFoundError) Error() string {
|
||||
return fmt.Sprintf("user not found: %v", e.id)
|
||||
}
|
||||
32
apps/backend/internal/user/service.go
Normal file
32
apps/backend/internal/user/service.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func NewService(db *bun.DB) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) UserByID(ctx context.Context, id uuid.UUID) (*User, error) {
|
||||
var user User
|
||||
err := s.db.NewSelect().Model(&user).Where("id = ?", id).Scan(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, newNotFoundError(id)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
17
apps/backend/internal/user/user.go
Normal file
17
apps/backend/internal/user/user.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
bun.BaseModel `bun:"users"`
|
||||
|
||||
ID uuid.UUID `bun:",pk,type:uuid" json:"id"`
|
||||
DisplayName string `bun:"display_name,notnull" json:"displayName"`
|
||||
Email string `bun:"email,unique,notnull" json:"email"`
|
||||
Password string `bun:"password,notnull" json:"-"`
|
||||
StorageUsageBytes int64 `bun:"storage_usage_bytes,notnull" json:"storageUsageBytes"`
|
||||
StorageQuotaBytes int64 `bun:"storage_quota_bytes,notnull" json:"storageQuotaBytes"`
|
||||
}
|
||||
Reference in New Issue
Block a user