Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefalultSessionConfig = SessionConfig{ CookieName: "session_id", Expiry: 2 * time.Hour, Factory: func(id string) Session { return NewDefaultSession(id, 2*time.Hour) }, }
var DefaultJWTConfig = JWTConfig{ Secret: utils.GenerateRandomString(64), Expiry: 24 * time.Hour, SigningMethod: jwt.SigningMethodHS256, }
var DefaultPasswordConfig = PasswordConfig{
HashCost: 10,
}
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config[T User] struct { DatabaseConfig DatabaseConfig[T] JWT *JWTConfig Session *SessionConfig Password *PasswordConfig OAuthProviders []Provider UseSessions bool }
type Database ¶
type Database[T User] interface { // FindByPK retrieves a user object from the database FindByPK(val any) (T, error) // SaveUser persists a user object to the database SaveUser(user *models.User) (*models.User, error) // UpdateUser updates a user object in the database. User should be instance of models.User. UpdateUser(user *models.User) (*models.User, error) // DeleteUser removes a user object from the database. User should be instance of models.User. DeleteUser(user *models.User) error // GetAllUsers retrieves all users from the database. GetAllUsers() ([]T, error) // SaveSession stores a session in the database with its expiration time. SaveSession(session Session, expiresAt time.Time) error // GetSession retrieves a session by ID, returning an error if not found or expired. GetSession(sessionID string) (Session, error) // DeleteSession removes a session by ID. DeleteSession(sessionID string) error }
Database is an interface that defines the methods for interacting with a database.
type DatabaseConfig ¶
type DatabaseConfig[T User] struct { Name DatabaseName DB *sql.DB UserTable string PrimaryKey string FindUserFn FindUserFn UserModel User UseDefaultUser bool }
DatabaseConfig defines configuration for database connection and user model/table.
type DatabaseName ¶
type DatabaseName string
DatabaseName is a string type that represents the name of the database.
const ( SQLite DatabaseName = "sqlite" Postgres DatabaseName = "postgres" )
type FindUserFn ¶
FindUser is a customized function type that takes a value of any type and returns a User type The database interface will use this function if provided, instead of retrieving the user by type reflection.
type PasswordConfig ¶
type PasswordConfig struct {
HashCost int
}
type Provider ¶
type Provider interface { // Name returns the name of the provider Name() string // GetEndpoint returns the OAuth endpoint GetEndpoint() oauth2.Endpoint // GetScopes returns the scopes provided for the OAuth. GetScopes() []string // GetConfig returns the oauth2 config GetConfig() *oauth2.Config // FetchUserInfo retrieves user information from the OAuth provider and returns UserInfo type. FetchUserInfo(client *http.Client, ctx context.Context, token *oauth2.Token) (models.UserInfo, error) }
Provider defines the interface for OAuth providers like Google or Facebook. It provides methods to retrieve provider-specific OAuth endpoints, scopes, and user information.
type Session ¶
type Session interface { // Set inserts a key with a value in the session Set(key, value any) error // Get returns a value associated with a key in the session Get(key any) any // Delete removes a key from the session Delete(key any) error // SessionID returns the unique session ID SessionID() string // MarshalJSON serializes the session data to JSON. MarshalJSON() ([]byte, error) // UnmarshalJSON deserializes JSON data into the session. UnmarshalJSON(data []byte) error }
type SessionConfig ¶
type SessionConfig struct { CookieName string Expiry time.Duration Factory SessionFactory }
type SessionFactory ¶
SessionFactory is a type alias for a function that creates a new Session instance with the given ID.
type SessionStore ¶
type SessionStore interface { // SaveSession stores a session with its expiration time. SaveSession(session Session, expiresAt time.Time) error // GetSession retrieves a session by ID, returning an error if not found or expired. GetSession(sessionID string) (Session, error) // DeleteSession removes a session by ID. DeleteSession(sessionID string) error }
SessionStore defines the interface for storing and retrieving sessions.
type User ¶
func FindUserByEmailPG ¶
FindUserByEmailPG is a function that retrieves a user by email from a PostgreSQL database. It will be automatically passed to the database if the default user model is used.
func FindUserByEmailSQLite ¶
FindUserByEmailSQLite is a function that retrieves a user by email from a SQLite database. It will be automatically passed to the database if the default user model is used.