Township Auth Client
township-client is a client-side JavaScript library designed to interact with Township authentication servers. It enables common user credential management operations such as registration, login, logout, and password changes. The library, currently at version 1.3.2, appears to have ceased active development around 2017, aligning with the last known activity on its companion `township` server project. Key features include the ability to manage user credentials across multiple Township servers, automatic persistence of login information to a configuration file (or `localStorage` in browsers), and a wrapper for making authenticated requests (`secureRequest`). This client primarily targets CommonJS environments, as suggested by its `require()`-based examples. Due to its age, it might not integrate seamlessly with modern ESM-first projects and relies on older dependency versions.
Common errors
-
TypeError: TownshipClient is not a constructor
cause Attempting to instantiate `TownshipClient` when it's not correctly imported as a constructor, often due to mismatched CommonJS/ESM import styles or transpilation issues.fixFor CommonJS, use `const TownshipClient = require('township-client'); const client = TownshipClient(...)`. If using ESM, and if the package has a default export compatible with `esModuleInterop`, `import TownshipClient from 'township-client'` might work, otherwise `import * as TownshipClient from 'township-client'; const client = TownshipClient.default(...)` may be necessary. -
Error: Server connection failed
cause The configured `opts.server` URL is incorrect, the Township server is unreachable, or there is a general network connectivity issue.fixVerify that the `server` URL passed to `TownshipClient` initialization is correct and the target Township authentication server is operational and accessible from the client's network environment. -
Register error Error: {"message":"email already exists"}cause An attempt was made to register a user with an email address that is already associated with an existing account on the Township server.fixUse a unique email address for new user registration or attempt to log in if the user is already expected to exist. -
Login error Error: {"message":"Invalid credentials"}cause The provided email or password for login does not match any registered user on the configured Township server.fixDouble-check the email and password for typos. If the password is forgotten, the library's `changePassword` method can be used, but requires a valid login token for the user already to be saved.
Warnings
- breaking The `township-client` library and its associated `township` server project appear to be abandoned, with no significant development activity since around 2017. This implies no further updates, bug fixes, or security patches will be provided.
- gotcha The library relies on outdated versions of underlying HTTP clients (like `nets`), which may contain known security vulnerabilities, lack modern features, or have performance issues compared to contemporary alternatives.
- gotcha Primarily designed for CommonJS (`require`) environments, integrating this library into modern ES Module (`import`) projects might require specific build configurations (e.g., `esModuleInterop` in TypeScript) or result in compatibility issues.
- gotcha The library does not ship with TypeScript type definitions. Developers using TypeScript will need to create their own declaration files (`.d.ts`) to achieve type safety or use it as a dynamically imported JavaScript module.
Install
-
npm install township-client -
yarn add township-client -
pnpm add township-client
Imports
- TownshipClient
import TownshipClient from 'township-client'
const TownshipClient = require('township-client') - client.register
client.register({ email: '...', password: '...' }, callback) - client.login
client.login({ email: '...', password: '...' }, callback)
Quickstart
const TownshipClient = require('township-client');
const client = TownshipClient({
server: 'https://api.township.site', // Set default server on init
config: {
filename: '.townshiprc'
// config file stored in user homedir.
// uses localstorage if in browser
}
});
client.register({
email: 'joe@hand.email',
password: 'Iheartcoffee'
}, function (err, res, body) {
if (err) return console.error('Register error', err);
console.log('Registered successfully with ', body.key, body.token);
client.login({
email: 'joe@hand.email',
password: 'Iheartcoffee'
}, function (err, res, body) {
if (err) return console.error('Login error', err);
console.log('Logged in successfully!');
console.log('User login info:', client.getLogin());
});
});