New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

township-client

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

township-client - npm Package Compare versions

Comparing version 0.1.0 to 0.3.0

47

index.js

@@ -17,3 +17,3 @@ var assert = require('assert')

: 'https://' + opts.server
: self.config.get('currentLogin').server
: self.config.getLogin().server

@@ -31,4 +31,4 @@ self.routes = opts.routes || {

opts = opts || {}
if (!opts.password) return cb(new Error('password is required to register'))
if (!opts.email) return cb(new Error('email is required to register'))
if (!opts.email) return cb(new Error('Email is required to register'))
if (!opts.password) return cb(new Error('Password is required to register'))

@@ -40,2 +40,3 @@ var self = this

method: 'POST',
server: server,
url: server + self.routes.register,

@@ -58,3 +59,4 @@ json: {

opts = opts || {}
if (!opts.email) return cb(new Error('email is required to login'))
if (!opts.email) return cb(new Error('Email is required to login'))
if (!opts.password) return cb(new Error('Password is required to login'))

@@ -66,2 +68,3 @@ var self = this

method: 'POST',
server: server,
url: server + self.routes.login,

@@ -84,5 +87,5 @@ json: {

opts = opts || {}
if (!opts.email) return cb(new Error('email is required to change password'))
if (!opts.password) return cb(new Error('new password is required to change password'))
if (!opts.newPassword) return cb(new Error('old password is required to change password'))
if (!opts.email) return cb(new Error('Email is required to change password'))
if (!opts.password) return cb(new Error('Password is required to change password'))
if (!opts.newPassword) return cb(new Error('New password is required to change password'))

@@ -92,8 +95,6 @@ var self = this

opts.token = opts.token || self.config.getLogin(server).token
return self._request({
method: 'POST',
server: server,
url: server + self.routes.updatePassword,
headers: {authorization: 'Bearer ' + opts.token},
json: {

@@ -114,9 +115,33 @@ email: opts.email,

TownshipClient.prototype.getLogin = function (server) {
var self = this
server = self._getServer({server: server})
return self.config.getLogin(server)
}
TownshipClient.prototype._getServer = function (opts) {
opts = opts || {}
assert.ok(opts.server || this.server, 'server must be specified before making auth request')
assert.ok(opts.server || this.server, 'Server must be specified to make an auth request')
return opts.server || this.server
}
TownshipClient.prototype.secureRequest = function (opts, cb) {
var self = this
var server = self._getServer(opts)
if (!self.getLogin(server)) return cb(new Error('Must login to server for secure request.'))
if (!opts.server) opts.server = server
return self._request(opts, cb)
}
TownshipClient.prototype._request = function (opts, cb) {
var self = this
opts.token = opts.token || self.getLogin(opts.server).token
if (opts.token) {
opts.withCredentials = true
opts.headers = { authorization: 'Bearer ' + opts.token }
}
return request(opts, function (err, res, body) {

@@ -123,0 +148,0 @@ if (err) return cb(err)

{
"name": "township-client",
"version": "0.1.0",
"version": "0.3.0",
"description": "Township auth client library",

@@ -20,5 +20,7 @@ "main": "index.js",

"memdb": "^1.3.1",
"tape": "^4.6.2",
"standard": "^8.6.0",
"tap-spec": "^4.1.1",
"tape": "^4.6.3",
"township": "^1.0.0"
}
}

@@ -5,4 +5,38 @@ # Township Auth Client Module

Client library to for users to login, register, change password with a [township](https://github.com/township/township) auth server.
Client library to login, register, and change passwords for users using [township](https://github.com/township/township) auth server(s).
#### Features
* Manage user credentials for many Township servers.
* Login, Register, and Change Password auth requests.
* Login information is persisted to a configuration file.
## Usage
```js
var TownshipClient = require('township-client')
var client = TownshipClient({
server: 'https://api.township.site' // Set default server on init
config: {
filename: '.townshiprc' // config file stored in user homedir
}
})
client.register({
email: 'joe@hand.email',
password: 'Iheartcoffee'
}, function (err) {
if (err) return console.error('Register error', err)
console.log('Registered successfully!')
})
client.login({
email: 'joe@hand.email',
password: 'Iheartcoffee'
}, function (err) {
if (err) return console.error('Login error', err)
console.log('Logged in successfully!')
})
```
## API

@@ -19,32 +53,37 @@

filename: '.townshiprc', // configuration filename (stored in os homedir)
filepath: '~/.townshiprc' // full config file path
filepath: '~/.townshiprc' // specify a full config file path
}
routes: { // routes for ALL township servers used by client
register: '/register',
login: '/login',
updatePassword: '/updatepassword'
}
}
```
### `township.register(opts, cb)`
`opts.server` can be set once on initialization or during each request. The client can handle multiple servers. `opts.server` can be passed with each request if the request should go to a different server than the client was initialized with.
`opts.email` and `opts.password` required.
### Auth Requests
### `township.login(opts, cb)`
#### `township.register(opts, cb)`
`opts.email` required
Register a user and receive a token. `opts.email` and `opts.password` required.
### `township.changePassword(opts, cb)`
#### `township.login(opts, cb)`
Reset password. `opts.email`, `opts.password`, and `opts.newPassword` required.
Login a registered user. `opts.email` and `opts.password` required
### `var config = township.config`
#### `township.changePassword(opts, cb)`
Get current login info, configuration, etc.
Reset password for a registered user. `opts.email`, `opts.password`, and `opts.newPassword` required. User must have a valid login token saved before changing password.
### `config.get('currentLogin')`
#### `township.secureRequest(opts, cb)`
Get the current login information.
Make a secure request to the server. `opts` should include `opts.method` and `opts.url`, passed to the nets module.
#### `config.getLogin([server])`
### `township.getLogin([server])`
Get current login info, or login info for a `server`.
Get login information for a `server`. If no server is specified, returns current login info (from the most recent login or register action).
## License
[MIT](LICENSE.md)

@@ -33,3 +33,3 @@ var fs = require('fs')

client.register({email: 'joe', password: 'verysecret'}, function (err) {
t.error(err)
t.error(err, 'no error')
t.pass('registers')

@@ -42,3 +42,3 @@ t.end()

client.login({email: 'joe', password: 'verysecret'}, function (err) {
t.error(err)
t.error(err, 'no error')
t.pass('login okay')

@@ -69,3 +69,3 @@ t.end()

}, function (err) {
t.error(err)
t.error(err, 'no error')
t.pass('okay')

@@ -81,3 +81,3 @@ t.end()

}, function (err) {
t.error(err)
t.error(err, 'no error')
t.pass('okay')

@@ -88,2 +88,16 @@ t.end()

test('register another user', function (t) {
client.register({email: 'joe@hand.email', password: 'verysecret'}, function (err) {
t.error(err, 'no error')
t.pass('registers')
t.end()
})
})
test('secure request', function (t) {
// client.secureRequest(opts, cb)
t.skip('TODO')
t.end()
})
test.onFinish(function () {

@@ -90,0 +104,0 @@ server.close(function () {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc