auth

package
v0.0.0-...-dd3cbee Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthRequired

func AuthRequired() gin.HandlerFunc

AuthRequired is a middleware that validates JWT tokens from cookies

func CheckPassword

func CheckPassword(password, hash string) bool

CheckPassword compares a password with its hash

func ClearAuthCookies

func ClearAuthCookies(c *gin.Context)

ClearAuthCookies clears authentication cookies

func GenerateRandomToken

func GenerateRandomToken() (string, error)

GenerateRandomToken generates a secure random token for refresh tokens

func GenerateTokenPair

func GenerateTokenPair(userID string) (accessToken string, refreshToken string, err error)

GenerateTokenPair creates access and refresh tokens for a user

func GetJWTSecret

func GetJWTSecret() string

GetJWTSecret retrieves the JWT secret from environment

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes a password using bcrypt

func HashRefreshToken

func HashRefreshToken(token string) string

HashRefreshToken creates a SHA256 hash of the refresh token for database storage

func Login

func Login(c *gin.Context)

Login handles user login

func Logout

func Logout(c *gin.Context)

Logout handles user logout

func Me

func Me(c *gin.Context)

Me returns the current user's information

func Refresh

func Refresh(c *gin.Context)

Refresh handles token refresh

func Register

func Register(c *gin.Context)

Register handles user registration

func SetAuthCookies

func SetAuthCookies(c *gin.Context, accessToken, refreshToken string)

SetAuthCookies sets HTTP-only authentication cookies

func ValidateEmail

func ValidateEmail(email string) bool

ValidateEmail checks if email format is valid

func ValidateRefreshToken

func ValidateRefreshToken(tokenString string) (string, error)

ValidateRefreshToken validates a refresh token and returns the user ID

Types

type Claims

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

JWT Claims structure

func ValidateAccessToken

func ValidateAccessToken(tokenString string) (*Claims, error)

ValidateAccessToken validates an access token and returns the claims

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required"`
}

type RegisterRequest

type RegisterRequest struct {
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required,min=8"`
}

type User

type User struct {
	ID           uuid.UUID `json:"id"`
	Email        string    `json:"email"`
	PasswordHash string    `json:"-"` // Never expose password hash in JSON
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
}

Jump to

Keyboard shortcuts

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