knex-masto-auth

    1.0.1 • Public • Published

    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

    Param Type Description
    knex Knex Instance of the Knex library
    clientOptions Object Client options
    [options.catchError] function Optional function to call when an error occurs in the client registration process
    [options.tableName] function Name 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

    Param Type Description
    knex Knex Instance of the Knex library
    [options.catchError] function Optional function to call when an error occurs in the client registration process
    [options.tableName] function Name 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

    Param Type Description
    redirectUri string The URI to redirect the user to after they have authenticated on their mastodon instance.
    clientName string The name of your application

    knex-masto-auth~CatchError : function

    Kind: inner typedef of knex-masto-auth

    Param Type Description
    error Error The error that occured
    url string The 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

    Install

    npm i knex-masto-auth

    DownloadsWeekly Downloads

    0

    Version

    1.0.1

    License

    ISC

    Unpacked Size

    17.4 kB

    Total Files

    7

    Last publish

    Collaborators

    • paulkiddle