Skip to content

Commit

Permalink
feat: air support and adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Feb 26, 2024
1 parent e7fb084 commit d097cc4
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 31 deletions.
10 changes: 10 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# .air.toml
root = "."
tmp_dir = "tmp"
[build]
cmd = "go build -o ./tmp/main examples/main.go"
bin = "./tmp/main"
delay = 1000 # ms
exclude_dir = ["assets", "tmp", "vendor"]
include_ext = ["go", "tpl", "tmpl", "html"]
exclude_regex = ["_test\\.go"]
41 changes: 21 additions & 20 deletions adapters/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/google/uuid"
"gorm.io/gorm"
)

func init() {
Expand Down Expand Up @@ -35,11 +36,11 @@ const (
AccountTypeWebAuthn AccountType = "webauthn"
)

// Account ...
// Account represents an account in a third-party identity provider.
type Account struct {
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;column:id;default:gen_random_uuid();"`
Type AccountType `json:"type"`
Provider string `json:"provider"`
Type AccountType `json:"type" validate:"required"`
Provider string `json:"provider" validate:"required"`
ProviderAccountID *string `json:"provider_account_id"`
RefreshToken *string `json:"refresh_token"`
AccessToken *string `json:"access_token"`
Expand All @@ -51,24 +52,24 @@ type Account struct {
UserID *uuid.UUID `json:"user_id"`
User User `json:"user" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}

// User ...
// User is a user of the application.
type User struct {
ID uuid.UUID `json:"id" gorm:"primaryKey;unique;type:uuid;column:id;default:gen_random_uuid()"`
Name string `json:"name"`
Email string `json:"email"`
Name string `json:"name" validate:"required,max=255"`
Email string `json:"email" gorm:"uniqueIndex" validate:"required,email"`
EmailVerified *bool `json:"email_verified"`
Image *string `json:"image"`
Image *string `json:"image" validate:"url"`
Accounts []Account `json:"accounts" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
Sessions []Session `json:"sessions" gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}

// Session ...
Expand All @@ -79,13 +80,13 @@ type Session struct {
UserID uuid.UUID `json:"user_id"`
User User `json:"user"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}

// IsValid returns true if the session is valid.
func (s Session) IsValid() bool {
func (s *Session) IsValid() bool {
return s.ExpiresAt.After(time.Now())
}

Expand All @@ -95,9 +96,9 @@ type VerificationToken struct {
Identifier string `json:"identifier"`
ExpiresAt time.Time `json:"expires_at"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}

// Adapter is an interface that defines the methods for interacting with the underlying data storage.
Expand Down
10 changes: 5 additions & 5 deletions adapters/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (a *gormAdapter) CreateUser(ctx context.Context, user adapters.User) (adapt
return user, nil
}

// GetSession ...
// GetSession is a helper function to retrieve a session by session token.
func (a *gormAdapter) GetSession(ctx context.Context, sessionToken string) (adapters.Session, error) {
var session adapters.Session
err := a.db.WithContext(ctx).Preload("User").Where("session_token = ?", sessionToken).First(&session).Error
Expand All @@ -64,7 +64,7 @@ func (a *gormAdapter) GetSession(ctx context.Context, sessionToken string) (adap
return session, nil
}

// GetUser ...
// GetUser is a helper function to retrieve a user by ID.
func (a *gormAdapter) GetUser(ctx context.Context, id uuid.UUID) (adapters.User, error) {
var user adapters.User
err := a.db.WithContext(ctx).Preload("Accounts").Where("id = ?", id).First(&user).Error
Expand All @@ -75,7 +75,7 @@ func (a *gormAdapter) GetUser(ctx context.Context, id uuid.UUID) (adapters.User,
return user, nil
}

// CreateSession ...
// CreateSession is a helper function to create a new session.
func (a *gormAdapter) CreateSession(ctx context.Context, userID uuid.UUID, expires time.Time) (adapters.Session, error) {
session := adapters.Session{UserID: userID, SessionToken: uuid.NewString(), ExpiresAt: expires}
err := a.db.WithContext(ctx).Create(&session).Error
Expand All @@ -86,12 +86,12 @@ func (a *gormAdapter) CreateSession(ctx context.Context, userID uuid.UUID, expir
return session, nil
}

// DeleteSession ...
// DeleteSession is a helper function to delete a session by session token.
func (a *gormAdapter) DeleteSession(ctx context.Context, sessionToken string) error {
return a.db.WithContext(ctx).Where("session_token = ?", sessionToken).Delete(&adapters.Session{}).Error
}

// RefreshSession ...
// RefreshSession is a helper function to refresh a session.
func (a *gormAdapter) RefreshSession(ctx context.Context, session adapters.Session) (adapters.Session, error) {
err := a.db.WithContext(ctx).Model(&adapters.Session{}).Where("session_token = ?", session.SessionToken).Updates(&session).Error
if err != nil {
Expand Down
20 changes: 19 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,23 @@ func run(ctx context.Context) error {
log.Fatal(err)
}

gothConfig := goth.Config{Adapter: ga, Secret: goth.GenerateKey()}
gothConfig := goth.Config{
Adapter: ga,
Secret: goth.GenerateKey(),
CookieHTTPOnly: true,
}

app.Use(goth.NewProtectMiddleware(gothConfig))

app.Get("/", func(c *fiber.Ctx) error {
session, err := goth.SessionFromContext(c)
if err != nil {
return err
}

return c.JSON(session)
})

app.Get("/login", func(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return t.Execute(c.Response().BodyWriter(), providerIndex)
Expand Down Expand Up @@ -198,6 +214,8 @@ func main() {
}
}

var helloTemplate = `<div>Hello World</div>`

var indexTemplate = `{{range $key,$value:=.Providers}}
<p><a href="/login/{{$value}}">Log in with {{index $.ProvidersMap $value}}</a></p>
{{end}}`
Expand Down
Loading

0 comments on commit d097cc4

Please sign in to comment.