Skip to content

Commit

Permalink
refactor: login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jun 12, 2024
1 parent 1dca7e0 commit ec116d5
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 62 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# .github/workflows/release.yml

name: Release

on:
push:
branches:
- main
tags:
- 'v*'

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
test:
permissions:
Expand All @@ -15,7 +21,8 @@ jobs:
needs: [ test ]
steps:
- uses: actions/checkout@v4
- uses: go-semantic-release/action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/setup-go@v5
with:
go-version-file: ./go.mod
- run: make release
if: success()
16 changes: 16 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
before:
hooks:
- go mod tidy
builds:
- skip: true
release:
header: |
## Changelog ({{ .Date }})
Welcome to this new release! We hope you enjoy the changes we've made.
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
26 changes: 12 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
.DEFAULT_GOAL := build
.DEFAULT_GOAL := release

GO ?= go
GO_RUN_TOOLS ?= $(GO) run -modfile ./tools/go.mod
GO_TEST = $(GO_RUN_TOOLS) gotest.tools/gotestsum --format pkgname
GO_MOD ?= $(shell ${GO} list -m)
GO ?= go
GO_RUN_TOOLS ?= $(GO) run -modfile ./tools/go.mod
GO_TEST ?= $(GO_RUN_TOOLS) gotest.tools/gotestsum --format pkgname
GO_RELEASER ?= $(GO_RUN_TOOLS) github.com/goreleaser/goreleaser
GO_MOD ?= $(shell ${GO} list -m)

.PHONY: release
release: ## Release the project.
$(GO_RELEASER) release --clean

.PHONY: generate
generate: ## Generate code.
Expand All @@ -13,13 +18,6 @@ generate: ## Generate code.
fmt: ## Run go fmt against code.
$(GO_RUN_TOOLS) mvdan.cc/gofumpt -w .

.PHONY: generate
generate: ## Generate code.
$(GO) generate ./...
$(GO_RUN_TOOLS) github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config ./examples/api/config.models.yml ./examples/api/api.yml
$(GO_RUN_TOOLS) github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config ./examples/api/config.client.yml ./examples/api/api.yml
$(GO_RUN_TOOLS) github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen -config ./examples/api/config.server.yml ./examples/api/api.yml

.PHONY: vet
vet: ## Run go vet against code.
$(GO) vet ./...
Expand All @@ -38,7 +36,7 @@ clean: ## Remove previous build.
rm -rf .test .dist
find . -type f -name '*.gen.go' -exec rm {} +
git checkout go.mod

.PHONY: help
help: ## Display this help screen.
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@grep -E '^[a-z.A-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
32 changes: 24 additions & 8 deletions adapters/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

goth "github.com/zeiss/fiber-goth"
"github.com/zeiss/fiber-goth/adapters"

"github.com/google/uuid"
Expand Down Expand Up @@ -47,7 +48,7 @@ func New(db *gorm.DB) (*gormAdapter, error) {
func (a *gormAdapter) CreateUser(ctx context.Context, user adapters.GothUser) (adapters.GothUser, error) {
err := a.db.WithContext(ctx).FirstOrCreate(&user).Error
if err != nil {
return adapters.GothUser{}, err
return adapters.GothUser{}, goth.ErrMissingUser
}

return user, nil
Expand All @@ -58,7 +59,7 @@ func (a *gormAdapter) GetSession(ctx context.Context, sessionToken string) (adap
var session adapters.GothSession
err := a.db.WithContext(ctx).Preload("User").Where("session_token = ?", sessionToken).First(&session).Error
if err != nil {
return adapters.GothSession{}, err
return adapters.GothSession{}, goth.ErrMissingSession
}

return session, nil
Expand All @@ -69,7 +70,7 @@ func (a *gormAdapter) GetUser(ctx context.Context, id uuid.UUID) (adapters.GothU
var user adapters.GothUser
err := a.db.WithContext(ctx).Preload("Accounts").Where("id = ?", id).First(&user).Error
if err != nil {
return adapters.GothUser{}, err
return adapters.GothUser{}, goth.ErrMissingUser
}

return user, nil
Expand All @@ -80,33 +81,48 @@ func (a *gormAdapter) CreateSession(ctx context.Context, userID uuid.UUID, expir
session := adapters.GothSession{UserID: userID, SessionToken: uuid.NewString(), ExpiresAt: expires}
err := a.db.WithContext(ctx).Create(&session).Error
if err != nil {
return adapters.GothSession{}, err
return adapters.GothSession{}, goth.ErrBadSession
}

return session, nil
}

// 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.GothSession{}).Error
err := a.db.WithContext(ctx).Where("session_token = ?", sessionToken).Delete(&adapters.GothSession{}).Error
if err != nil {
return goth.ErrBadRequest
}

return nil
}

// RefreshSession is a helper function to refresh a session.
func (a *gormAdapter) RefreshSession(ctx context.Context, session adapters.GothSession) (adapters.GothSession, error) {
err := a.db.WithContext(ctx).Model(&adapters.GothSession{}).Where("session_token = ?", session.SessionToken).Updates(&session).Error
if err != nil {
return adapters.GothSession{}, err
return adapters.GothSession{}, goth.ErrBadSession
}

return session, nil
}

// DeleteUser is a helper function to delete a user by ID.
func (a *gormAdapter) DeleteUser(ctx context.Context, id uuid.UUID) error {
return a.db.WithContext(ctx).Where("id = ?", id).Delete(&adapters.GothUser{}).Error
err := a.db.WithContext(ctx).Where("id = ?", id).Delete(&adapters.GothUser{}).Error
if err != nil {
return goth.ErrBadRequest
}

return nil
}

// LinkAccount is a helper function to link an account to a user.
func (a *gormAdapter) LinkAccount(ctx context.Context, accountID, userID uuid.UUID) error {
return a.db.WithContext(ctx).Model(&adapters.GothAccount{}).Where("id = ?", accountID).Update("user_id", userID).Error
err := a.db.WithContext(ctx).Model(&adapters.GothAccount{}).Where("id = ?", accountID).Update("user_id", userID).Error
if err != nil {
return goth.ErrBadRequest
}

return nil
}
Loading

0 comments on commit ec116d5

Please sign in to comment.