mirror of
https://github.com/rjNemo/auth
synced 2026-06-06 08:26:39 +00:00
refactor(auth): move auth package to service directory
Relocated internal/auth to internal/service/auth for improved project structure and separation of concerns. Updated all imports to reflect new paths. Split UserStore interface and error definitions into a dedicated store.go file. Moved generateUserID to user.go for better encapsulation.
This commit is contained in:
parent
d0311ccd24
commit
d642716fd0
8 changed files with 32 additions and 27 deletions
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/rjnemo/auth/internal/auth"
|
"github.com/rjnemo/auth/internal/service/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rjnemo/auth/internal/auth"
|
"github.com/rjnemo/auth/internal/service/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
const dashboardTimeDisplayLayout = "02 Jan 2006 15:04 MST"
|
const dashboardTimeDisplayLayout = "02 Jan 2006 15:04 MST"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rjnemo/auth/internal/auth"
|
"github.com/rjnemo/auth/internal/service/auth"
|
||||||
"github.com/rjnemo/auth/web"
|
"github.com/rjnemo/auth/web"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@ package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/base64"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -96,12 +94,3 @@ func (s *Service) Register(ctx context.Context, email UserEmail, password string
|
||||||
|
|
||||||
return &user, nil
|
return &user, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: could be UUID. return a dedicated type
|
|
||||||
func generateUserID() (string, error) {
|
|
||||||
buf := make([]byte, userIDByteLength)
|
|
||||||
if _, err := rand.Read(buf); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return base64.RawURLEncoding.EncodeToString(buf), nil
|
|
||||||
}
|
|
||||||
18
internal/service/auth/store.go
Normal file
18
internal/service/auth/store.go
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrUserNotFound signals no user exists for the provided lookup criteria.
|
||||||
|
var (
|
||||||
|
ErrUserNotFound = errors.New("auth: user not found")
|
||||||
|
ErrEmailRequired = errors.New("auth: email required")
|
||||||
|
)
|
||||||
|
|
||||||
|
// UserStore defines persistence expectations for user lookups.
|
||||||
|
type UserStore interface {
|
||||||
|
FindByEmail(ctx context.Context, email UserEmail) (*User, error)
|
||||||
|
Create(ctx context.Context, user User) error
|
||||||
|
}
|
||||||
|
|
@ -2,22 +2,9 @@ package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrUserNotFound signals no user exists for the provided lookup criteria.
|
|
||||||
var (
|
|
||||||
ErrUserNotFound = errors.New("auth: user not found")
|
|
||||||
ErrEmailRequired = errors.New("auth: email required")
|
|
||||||
)
|
|
||||||
|
|
||||||
// UserStore defines persistence expectations for user lookups.
|
|
||||||
type UserStore interface {
|
|
||||||
FindByEmail(ctx context.Context, email UserEmail) (*User, error)
|
|
||||||
Create(ctx context.Context, user User) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// MemoryStore is an in-memory implementation of UserStore for development and tests.
|
// MemoryStore is an in-memory implementation of UserStore for development and tests.
|
||||||
type MemoryStore struct {
|
type MemoryStore struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -45,3 +47,12 @@ func (e UserEmail) String() string {
|
||||||
func (e UserEmail) IsZero() bool {
|
func (e UserEmail) IsZero() bool {
|
||||||
return e == ""
|
return e == ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: could be UUID. return a dedicated type
|
||||||
|
func generateUserID() (string, error) {
|
||||||
|
buf := make([]byte, userIDByteLength)
|
||||||
|
if _, err := rand.Read(buf); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return base64.RawURLEncoding.EncodeToString(buf), nil
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue