Skip to content

Commit

Permalink
change oauth package to improve readability and ensure individual res…
Browse files Browse the repository at this point in the history
…ponsibility
  • Loading branch information
caioeverest committed Jan 29, 2024
1 parent f60736d commit edf0075
Show file tree
Hide file tree
Showing 17 changed files with 496 additions and 395 deletions.
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/marcopollivier/techagenda/lib/ssr"
"github.com/marcopollivier/techagenda/pkg/event"
"github.com/marcopollivier/techagenda/pkg/lending"
"github.com/marcopollivier/techagenda/pkg/oauth"
"github.com/marcopollivier/techagenda/pkg/static"
"github.com/marcopollivier/techagenda/pkg/user"
)
Expand All @@ -27,12 +28,13 @@ var rootCmd = &cobra.Command{
fx.New(
fx.Provide(database.NewDB),
fx.Provide(server.NewHTTPServer),
oauth.Module(),
ssr.Module(),
static.Module(),
user.Module(),
event.Module(),
// attendee.Module(),

lending.Module(),
static.Module(),
).Run()
},
}
Expand Down
10 changes: 5 additions & 5 deletions migrations/20240113234311_create_user_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func upCreateUsersTable(ctx context.Context, tx *sql.Tx) error {
created_at TIMESTAMP DEFAULT now(),
updated_at TIMESTAMP DEFAULT now(),
deleted_at TIMESTAMP,
UNIQUE(email)
);
CREATE INDEX idx_users_role on users (role);
`); err != nil {
return err
Expand All @@ -39,16 +39,16 @@ func upCreateUsersTable(ctx context.Context, tx *sql.Tx) error {
// OAuth table
if _, err := tx.ExecContext(ctx, `
CREATE TYPE provider AS ENUM ('github');
CREATE TABLE IF NOT EXISTS oauths (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
user_id BIGINT NOT NULL REFERENCES users(id),
provider PROVIDER NOT NULL,
identifier TEXT NOT NULL,
created_at TIMESTAMP DEFAULT now(),
updated_at TIMESTAMP DEFAULT now(),
deleted_at TIMESTAMP,
CONSTRAINT fk_oauths_user_id FOREIGN KEY (user_id) REFERENCES users(id),
UNIQUE(user_id, provider, identifier)
);
Expand Down
8 changes: 4 additions & 4 deletions pkg/event/model_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/lending/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/marcopollivier/techagenda/lib/ssr"
"github.com/marcopollivier/techagenda/pkg/event"
"github.com/marcopollivier/techagenda/pkg/user"
"github.com/marcopollivier/techagenda/pkg/oauth"
)

type QueryParams struct {
Expand Down Expand Up @@ -45,7 +45,7 @@ func NewLendingHandler(server *echo.Echo, eventService event.Service, engine *ss
},
Props: &ssr.Props{
Events: events,
User: user.GetUserFromCtx(ctx),
User: oauth.GetUserFromCtx(ctx),
},
})
return c.HTML(http.StatusOK, string(page))
Expand Down
11 changes: 11 additions & 0 deletions pkg/oauth/fx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oauth

import "go.uber.org/fx"

func Module() fx.Option {
return fx.Module("oauth",
fx.Provide(NewOAuthService),
fx.Provide(NewOAuthHandler),
fx.Invoke(SetOAuthAPIRoutes),
)
}
25 changes: 17 additions & 8 deletions pkg/user/handler_oauth.go → pkg/oauth/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package user
package oauth

import (
"fmt"
Expand All @@ -8,22 +8,31 @@ import (
"github.com/labstack/echo/v4"
"github.com/marcopollivier/techagenda/lib/server"
"github.com/marcopollivier/techagenda/lib/session"
"github.com/marcopollivier/techagenda/pkg/user"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/samber/lo"
)

func (h *UserHandler) AuthLogin(c echo.Context) (err error) {
type OAuthHandler struct {
service Service
}

func NewOAuthHandler(service Service) *OAuthHandler {
return &OAuthHandler{service: service}
}

func (h *OAuthHandler) AuthLogin(c echo.Context) (err error) {
var (
ctx = c.Request().Context()
res = c.Response()
req = c.Request()
authUser goth.User
userData User
userData user.User
token string
)

if _, ok := c.Request().Context().Value(MiddlewareUserKey).(User); ok {
if _, ok := c.Request().Context().Value(MiddlewareUserKey).(user.User); ok {
res.Header().Set("Location", getReferer(req))
res.WriteHeader(http.StatusTemporaryRedirect)
return
Expand Down Expand Up @@ -55,7 +64,7 @@ func (h *UserHandler) AuthLogin(c echo.Context) (err error) {
return
}

func (h *UserHandler) AuthLogout(c echo.Context) (err error) {
func (h *OAuthHandler) AuthLogout(c echo.Context) (err error) {
var (
res = c.Response()
req = c.Request()
Expand All @@ -73,18 +82,18 @@ func (h *UserHandler) AuthLogout(c echo.Context) (err error) {
return
}

func (h *UserHandler) AuthCallback(c echo.Context) (err error) {
func (h *OAuthHandler) AuthCallback(c echo.Context) (err error) {
var (
ctx = c.Request().Context()
res = c.Response()
req = c.Request()
authUser goth.User
userData User
userData user.User
token string
provider string
)

if _, ok := c.Request().Context().Value(MiddlewareUserKey).(User); ok {
if _, ok := c.Request().Context().Value(MiddlewareUserKey).(user.User); ok {
res.Header().Set("Location", getReferer(req))
res.WriteHeader(http.StatusTemporaryRedirect)
return
Expand Down
11 changes: 6 additions & 5 deletions pkg/user/middleware.go → pkg/oauth/middleware.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package user
package oauth

import (
"context"
"log/slog"

"github.com/labstack/echo/v4"
"github.com/marcopollivier/techagenda/lib/session"
"github.com/marcopollivier/techagenda/pkg/user"
)

type MiddlewareCtxKey string
Expand All @@ -19,7 +20,7 @@ func AuthMiddleware(service Service) echo.MiddlewareFunc {
return func(c echo.Context) (err error) {
var (
ctx = c.Request().Context()
user User
user user.User
req = c.Request()
res = c.Response()
userSession session.UserSession
Expand All @@ -38,9 +39,9 @@ func AuthMiddleware(service Service) echo.MiddlewareFunc {
}
}

func GetUserFromCtx(ctx context.Context) *User {
var userPtr *User
if userData, ok := ctx.Value(MiddlewareUserKey).(User); ok {
func GetUserFromCtx(ctx context.Context) *user.User {
var userPtr *user.User
if userData, ok := ctx.Value(MiddlewareUserKey).(user.User); ok {
userPtr = &userData
}
return userPtr
Expand Down
19 changes: 19 additions & 0 deletions pkg/oauth/oauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package oauth

import (
"gorm.io/gorm"
)

//go:generate go-enum --marshal --sql -f oauth.go

type OAuth struct {
gorm.Model
UserID uint
Provider Provider
Identifier string
}

func (OAuth) TableName() string { return "oauths" }

// ENUM(github)
type Provider int
142 changes: 142 additions & 0 deletions pkg/oauth/oauth_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/user/oauth_providers.go → pkg/oauth/providers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package user
package oauth

import (
"fmt"
Expand Down
13 changes: 13 additions & 0 deletions pkg/oauth/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package oauth

import "github.com/labstack/echo/v4"

func SetOAuthAPIRoutes(server *echo.Echo, handler *OAuthHandler) {
registerProviders()
server.Use(AuthMiddleware(handler.service))
auth := server.Group("/auth")

auth.GET("", handler.AuthLogin)
auth.GET("/logout", handler.AuthLogout)
auth.GET("/callback", handler.AuthCallback)
}
Loading

0 comments on commit edf0075

Please sign in to comment.