auth/AuthUnit.js

/**
 * @module flitter-auth/AuthUnit
 */

const Unit = require('libflitter/Unit')
const ncp = require('ncp')
const path = require('path')
const flapper = require('flitter-flap/FlapHelper')(false)

const FlitterProvider = require('./flitter/FlitterProvider')

/**
 * Registers functionality provided by flitter-auth.
 * @extends module:libflitter/Unit~Unit
 */
class AuthUnit extends Unit {

    /**
     * Get the name of this unit.
     * @returns {string} "auth"
     */
    name(){
        return "auth"
    }
    
    async go(app, context){
        
        context.bind('register_provider', this.register_provider)
        context.bind('get_provider', this.get_provider)
        
        if ( app.d.config.get('auth') ) {
            const flitter_provider_config = app.d.config.get('auth.sources.flitter')

            context.bind('providers', {
                ...((flitter_provider_config && flitter_provider_config.enable) && {flitter: new FlitterProvider(app, flitter_provider_config)})
            })
        }
        else context.bind('providers', {})
        
    }

    /**
     * Deploy the resources provided by flitter-auth. That is, the controllers/views/routes/models/middleware/etc.
     * @returns {Promise<void>}
     */
    async deploy() {
        const package_dir = __dirname
        const base_dir = path.dirname(require.main.filename)
        _flitter.log("Auth deploy from: " + package_dir, 2)
        _flitter.log("To: " + path.dirname(require.main.filename), 2)

        function do_copy(from, to) {
            return new Promise(
                (resolve, reject) => {
                    ncp(from, to, (error) => {
                        if (error) reject(error)

                        resolve()
                    })
                }
            )
        }
        
        
        // controllers
        await do_copy(path.resolve(package_dir + '/deploy/controllers'), _flitter.directories.controllers)

        // models
        await do_copy(path.resolve(package_dir + '/deploy/models'), _flitter.directories.models)

        // routing - middleware
        await do_copy(path.resolve(package_dir + "/deploy/routing/middleware"), path.resolve(_flitter.directories.middleware, 'auth'))

        // routing - routers
        await do_copy(path.resolve(package_dir + "/deploy/routing/routers"), _flitter.directories.routers)

        // views
        await do_copy(path.resolve(package_dir + "/deploy/views/auth"), path.resolve(_flitter.directories.views, 'auth'))
        
        // config
        await do_copy(path.resolve(package_dir + "/deploy/config"), _flitter.directories.config)
        
        // assets
        await do_copy(path.resolve(package_dir + "/deploy/assets"), path.resolve(_flitter.directories.assets, 'auth'))
        
        // add global middleware
        await flapper.insert_line(_flitter.directories.global_middleware, /\s*=\s*\[/, '    "auth:Utility",')

    }

    /**
     * Get the fully-qualified path to the migrations provided by this unit.
     * @returns {string}
     */
    migrations(){
        return path.resolve(__dirname, 'migrations')
    }
    
    register_provider(name, provider_class){
        const provider_config = this.d.config.get('auth.sources')[name]
        const provider = new provider_class(this, provider_config)
        this.d.auth.providers[name] = provider
    }
    
    get_provider(name){
        return this.d.auth.providers[name]
    }
}

module.exports = exports = AuthUnit