1.1.5 • Published 11 months ago

@optum/openid-client-server v1.1.5

Weekly downloads
45
License
Apache-2.0
Repository
github
Last release
11 months ago

Install

with npm

$ npm install @optum/openid-client-server

with yarn

$ yarn add @optum/openid-client-server

Usage

Options

The resolveOptions function will leverage environmental variables to auto-build all options with defaults. It can be required in the server setup module via import {resolveOptions} from '@optum/openid-client-server.

For more info see the .env.example file

clientServer

Use the clientServer function to create a http server with an integrated openid-client and all features in @optum/openid-client-server.

With a Promise

import {IncomingMessage, ServerResponse} from 'http'
import {clientServer} from '@optum/openid-client-server'

import handle from 'serve-handler'

const port = parseInt(process.env.NEXT_SERVER_PORT ?? '8080', 10)

const serveHandler = async (
    req: IncomingMessage,
    res: ServerResponse
): Promise<void> => {
    handle(req, res, {
        headers: [
            {
                source: '**/*.*',
                headers: [
                    {
                        key: 'Cache-Control',
                        value: 'max-age=0'
                    }
                ]
            }
        ]
    })
}

clientServer({
    contentHandler: serveHandler
})
    .then(server =>
        server.listen(port, () => {
            console.log(`> Ready on http://localhost:${port}`)
        })
    )
    .catch(error => {
        console.log('Static content server failed to start')
        console.error(error)
    })

With a Async Await

import {IncomingMessage, ServerResponse} from 'http'
import {clientServer} from '@optum/openid-client-server'

import handle from 'serve-handler'

const port = parseInt(process.env.NEXT_SERVER_PORT ?? '8080', 10)

;(async (): Promise<void> => {
    try {
        const serveHandler = async (
            req: IncomingMessage,
            res: ServerResponse
        ): Promise<void> => {
            handle(req, res, {
                headers: [
                    {
                        source: '**/*.*',
                        headers: [
                            {
                                key: 'Cache-Control',
                                value: 'max-age=0'
                            }
                        ]
                    }
                ]
            })
        }

        const server = await clientServer({contentHandler: serveHandler})

        server.listen(port, () => {
            console.log(`> Ready on http://localhost:${port}`)
        })
    } catch (error) {
        console.log('Static content server failed to start')
        console.error(error)
    }
})()

For a Next.js example, see: examples/nextjs file

Background

The original goal of this module was to provide as easy way to implement OpenID flows with Next.js applications via a custom Next.js server. There were issues leveraging frameworks like Koa.js for "easy wins" in session management and out-of-the-box middleware, so tides turned to using Node's core http module. The result ended up working for any Web UI that could be served by Node.js, so here we are.

Development

Environment

Editors

VS Code

IntelliJ

1.1.5

11 months ago

1.1.4

1 year ago

1.1.1

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.0

1 year ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

0.1.0

4 years ago