auth

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TokenCookieName is the name of the cookie used to store the auth token
	TokenCookieName = "friday_talks_auth_token"

	// TokenExpiry is the duration after which a token will expire
	TokenExpiry = 24 * time.Hour

	// ContextUserKey is the key used to store user info in request context
	ContextUserKey = "user"
)

Variables

This section is empty.

Functions

func UserFromContext

func UserFromContext(ctx context.Context) *models.User

UserFromContext extracts the user from the request context

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth handles authentication and authorization

func NewAuth

func NewAuth(jwtSecret string, userRepo models.UserRepository) *Auth

NewAuth creates a new Auth instance

func (*Auth) AuthMiddleware

func (a *Auth) AuthMiddleware(next http.Handler) http.Handler

AuthMiddleware creates middleware that validates JWT tokens and adds user to context

func (*Auth) Authenticate

func (a *Auth) Authenticate(ctx context.Context, email, password string) (*models.User, error)

Authenticate checks if the provided credentials are valid

func (*Auth) ClearTokenCookie

func (a *Auth) ClearTokenCookie(w http.ResponseWriter)

ClearTokenCookie clears the authentication token cookie

func (*Auth) GenerateToken

func (a *Auth) GenerateToken(userID int) (string, error)

GenerateToken creates a new JWT token for the given user

func (*Auth) RequireAuth

func (a *Auth) RequireAuth(next http.Handler) http.Handler

RequireAuth creates middleware that requires authentication

func (*Auth) SetTokenCookie

func (a *Auth) SetTokenCookie(w http.ResponseWriter, token string)

SetTokenCookie sets the authentication token as a cookie

func (*Auth) ValidateToken

func (a *Auth) ValidateToken(tokenString string) (int, error)

ValidateToken validates a JWT token and returns the user ID if valid

type Claims

type Claims struct {
	UserID int `json:"user_id"`
	jwt.RegisteredClaims
}

Claims represents JWT token claims

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL