Documentation
¶
Overview ¶
Package socialite provides OAuth2-based social authentication (similar to Laravel Socialite). It ships with built-in providers for Google, GitHub, Discord, and Apple, and allows registering custom providers.
Quick start ¶
s := socialite.New(socialite.Config{
Providers: map[string]socialite.ProviderConfig{
"github": {
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
RedirectURL: "http://localhost:3333/auth/github/callback",
},
},
})
// Redirect to provider
app.GET("/auth/:provider", s.RedirectHandler())
// Handle callback
app.GET("/auth/:provider/callback", s.CallbackHandler(myCallback))
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AppleProvider ¶
type AppleProvider struct {
// contains filtered or unexported fields
}
func (*AppleProvider) AuthURL ¶
func (p *AppleProvider) AuthURL(state string, scopes []string) string
func (*AppleProvider) Exchange ¶
func (p *AppleProvider) Exchange(ctx context.Context, code string) (*SocialUser, error)
func (*AppleProvider) Name ¶
func (p *AppleProvider) Name() string
type CallbackFunc ¶
type CallbackFunc func(c *nhttp.Context, user *SocialUser) error
CallbackFunc is called after a successful OAuth callback with the social user. The handler should create or find the local user, log them in, and redirect.
type Config ¶
type Config struct {
// Providers maps provider name → credentials.
Providers map[string]ProviderConfig
}
Config holds the socialite configuration.
type DiscordProvider ¶
type DiscordProvider struct {
// contains filtered or unexported fields
}
func (*DiscordProvider) AuthURL ¶
func (p *DiscordProvider) AuthURL(state string, scopes []string) string
func (*DiscordProvider) Exchange ¶
func (p *DiscordProvider) Exchange(ctx context.Context, code string) (*SocialUser, error)
func (*DiscordProvider) Name ¶
func (p *DiscordProvider) Name() string
type GitHubProvider ¶
type GitHubProvider struct {
// contains filtered or unexported fields
}
func (*GitHubProvider) AuthURL ¶
func (p *GitHubProvider) AuthURL(state string, scopes []string) string
func (*GitHubProvider) Exchange ¶
func (p *GitHubProvider) Exchange(ctx context.Context, code string) (*SocialUser, error)
func (*GitHubProvider) Name ¶
func (p *GitHubProvider) Name() string
type GoogleProvider ¶
type GoogleProvider struct {
// contains filtered or unexported fields
}
func (*GoogleProvider) AuthURL ¶
func (p *GoogleProvider) AuthURL(state string, scopes []string) string
func (*GoogleProvider) Exchange ¶
func (p *GoogleProvider) Exchange(ctx context.Context, code string) (*SocialUser, error)
func (*GoogleProvider) Name ¶
func (p *GoogleProvider) Name() string
type Provider ¶
type Provider interface {
// Name returns the provider name (e.g. "github", "google").
Name() string
// AuthURL returns the URL the user should be redirected to for authentication.
AuthURL(state string, scopes []string) string
// Exchange trades an authorization code for user information.
Exchange(ctx context.Context, code string) (*SocialUser, error)
}
Provider defines the contract for an OAuth provider.
type ProviderConfig ¶
type ProviderConfig struct {
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
RedirectURL string `json:"redirect_url"`
Scopes []string `json:"scopes"`
}
ProviderConfig holds OAuth2 credentials for a provider.
type SocialUser ¶
type SocialUser struct {
Provider string `json:"provider"`
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Avatar string `json:"avatar"`
AccessToken string `json:"access_token"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Raw map[string]any `json:"raw,omitempty"`
}
SocialUser represents the authenticated user returned by an OAuth provider.
type Socialite ¶
type Socialite struct {
// contains filtered or unexported fields
}
Socialite manages OAuth providers.
func New ¶
New creates a new Socialite manager with the given config. Built-in providers (github, google, discord, apple) are auto-registered if their config is present.
func (*Socialite) CallbackHandler ¶
func (s *Socialite) CallbackHandler(fn CallbackFunc) router.HandlerFunc
CallbackHandler returns a handler that processes the OAuth callback from the provider. It verifies the state parameter, exchanges the code for user information, and calls the provided callback function.
Example route: app.GET("/auth/:provider/callback", socialite.CallbackHandler(myCallback))
func (*Socialite) RedirectHandler ¶
func (s *Socialite) RedirectHandler() router.HandlerFunc
RedirectHandler returns a handler that redirects the user to the OAuth provider. The provider name is read from the :provider route param.
Example route: app.GET("/auth/:provider", socialite.RedirectHandler())
type SocialitePlugin ¶
type SocialitePlugin struct {
nimbus.BasePlugin
Manager *Socialite
Callback CallbackFunc
}
SocialitePlugin wraps Socialite as a Nimbus plugin, making it installable via `nimbus plugin install socialite` and selectable during `nimbus new`.
func NewPlugin ¶
func NewPlugin(cfg Config, callback CallbackFunc) *SocialitePlugin
NewPlugin creates a new Socialite plugin. Pass a callback to handle the authenticated social user.
func (*SocialitePlugin) Boot ¶
func (p *SocialitePlugin) Boot(app *nimbus.App) error
Boot is a no-op for Socialite.
func (*SocialitePlugin) DefaultConfig ¶
func (p *SocialitePlugin) DefaultConfig() map[string]any
DefaultConfig returns default Socialite configuration.
func (*SocialitePlugin) Register ¶
func (p *SocialitePlugin) Register(app *nimbus.App) error
Register binds the Socialite manager into the container.
func (*SocialitePlugin) RegisterRoutes ¶
func (p *SocialitePlugin) RegisterRoutes(r *router.Router)
RegisterRoutes mounts the OAuth redirect and callback routes.