func (s *Service) Login(ctx context.Context, username string, password string) error { // Authenticate user against database // ... // Set session cookie session, err := s.GetSession(ctx) if err != nil { return err } session.Set("username", username) session.Set("loggedIn", true) return nil }
func (s *Service) GetAuthenticatedUser(ctx context.Context) (*User, error) { session, err := s.GetSession(ctx) if err != nil { return nil, err } if !session.GetBool("loggedIn") { return nil, errors.New("user not authenticated") } username := session.GetString("username") user, err := s.GetUserByUsername(ctx, username) if err != nil { return nil, err } return user, nil }In both examples, the APIContext package is being used to manage session information and authentication credentials. The package provides methods to retrieve and set session data, making it easier to manage contextual information across different modules.