1.0.1 • Published 4 years ago

knex-masto-auth v1.0.1

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
4 years ago

Knex Masto Auth

Mastodon authentication using a knex instance for data persistance. Automatically registers a client with previously unknown instances and persists them to your database for future use.

import KnexMastoAuth, { migrate } from 'knex-masto-auth';
import Knex from 'knex';

// Bring your own knex instance
const db = new Knex({
	client: 'sqlite3',
  connection: {
    filename: './db.sqlite3',
  },
});

// Create the database tables if they're absent (idempotent operation)
await migrate(db);

// Get the instance of MastoAuth
const auth = new KnexMastoAuth(db, {
	clientName: 'My App',
	redirectUri: 'http://my-app.example/exchange-token'
});


// Example server - for full details of how to use the `auth` instance, see the [masto-auth library](https://gitlab.com/paulkiddle/masto-auth#module_masto-auth.default)
export default async (req, res) => {
	const { pathname, searchParams } = new URL('file://' + req.url);

	if(pathname === '/login') {
		// Get the instance to login to, e.g. through form submission
		const instance = searchParams.get('instance') || 'https://kith.kitchen';

		// Get the redirect URL - this will also handle issuer lookup and client registration if needed
		const instanceAuthPage = await auth.getRedirectUrl(instance);

		// Redirect to instance log in page
		res.setHeader('location', instanceAuthPage);
		res.statusCode = 303;
		res.end();
	} else if(pathname === '/exchange-token') {
		// User will be redirected here after authentication
		// Get the user info and do what you want with it
		res.end(JSON.stringify(await auth.getUserFromCallback(req)))
	} else {
		res.statusCode = 404;
		res.end('Not found');
	}
}

Dependencies

  • knex-settings: ^2.0.1
  • masto-auth: ^1.1.0

knex-masto-auth

Knex Masto Auth

See: default

knex-masto-auth.default

Kind: static class of knex-masto-auth

new module.exports(knex, clientOptions)

Create a new instance of KnexMastoAuth

ParamTypeDescription
knexKnexInstance of the Knex library
clientOptionsObjectClient options
options.catchErrorfunctionOptional function to call when an error occurs in the client registration process
options.tableNamefunctionName of the sql table to use to store mastodon client data. Defaults to 'mastodon_clients'

default.migrate(knex) ⇒ MastoAuth

Create the storage table if it doesn't exist and return a new instance of KnexMastoAuth

Kind: static method of default

ParamTypeDescription
knexKnexInstance of the Knex library
options.catchErrorfunctionOptional function to call when an error occurs in the client registration process
options.tableNamefunctionName of the sql table to use to store mastodon client data. Defaults to 'mastodon_clients'

knex-masto-auth.migrate()

Create the storage table if it doesn't exist

Kind: static method of knex-masto-auth
See: module:knex-masto-auth.default#migrate

knex-masto-auth~ClientOptions

Kind: inner typedef of knex-masto-auth

ParamTypeDescription
redirectUristringThe URI to redirect the user to after they have authenticated on their mastodon instance.
clientNamestringThe name of your application

knex-masto-auth~CatchError : function

Kind: inner typedef of knex-masto-auth

ParamTypeDescription
errorErrorThe error that occured
urlstringThe issuer URL that the error occured for

knex-masto-auth~Knex

Instance of the knex library

Kind: inner external of knex-masto-auth
See: https://knexjs.org/

knex-masto-auth~MastoAuth

Instance of the MastoAuth library

Kind: inner external of knex-masto-auth
See: https://gitlab.com/paulkiddle/masto-auth#module_masto-auth.default