Skip to content

Commit

Permalink
doc: provide a custom session
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jan 29, 2024
1 parent 159a24f commit 125ffc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"html/template"
"log"
"os"
Expand All @@ -9,7 +10,9 @@ import (
fiber_goth "github.com/zeiss/fiber-goth"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/amazon"
"github.com/markbates/goth/providers/apple"
"github.com/markbates/goth/providers/auth0"
Expand Down Expand Up @@ -233,6 +236,13 @@ func main() {
log.Fatal(err)
}

// This is an example to configure to use a custom session.
sess := session.Config{
KeyLookup: fmt.Sprintf("cookie:%s", gothic.SessionName),
CookieHTTPOnly: true,
}
fiber_goth.ConfigDefault.Session = fiber_goth.NewSessionStore(session.New(sess))

app.Get("/login/:provider", fiber_goth.NewBeginAuthHandler())
app.Get("/auth/:provider/callback/", fiber_goth.NewCompleteAuthHandler())
app.Get("/logout", fiber_goth.NewLogoutHandler())
Expand Down
14 changes: 14 additions & 0 deletions goth.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type SessionStore interface {
Update(c *fiber.Ctx, key, value string) error
// Destroy ...
Destroy(c *fiber.Ctx) error
// Interface ...
Interface() any
}

var _ SessionStore = (*sessionStore)(nil)
Expand Down Expand Up @@ -154,6 +156,18 @@ func (s *sessionStore) Update(c *fiber.Ctx, key, value string) error {
return nil
}

// Store returns the raw interface.
func (s *sessionStore) Interface() any {
return s.store
}

// Return the raw store of the used default session
func DefaultSession() any {
cfg := configDefault()

return cfg.Session.Interface()
}

// ProviderFromContext returns the provider from the request context.
func ProviderFromContext(c *fiber.Ctx) string {
return c.Get(fmt.Sprint(providerKey))
Expand Down

0 comments on commit 125ffc3

Please sign in to comment.