Skip to content

Instantly share code, notes, and snippets.

@rabbah
Last active July 16, 2019 17:19
Show Gist options
  • Save rabbah/2f0487078a83a85381215d5a9e532e2b to your computer and use it in GitHub Desktop.
Save rabbah/2f0487078a83a85381215d5a9e532e2b to your computer and use it in GitHub Desktop.
actions using transient state

A login action, saves a session state token, redirects to OAuth sign-on.

const redis = require('nim').redis()

function login(auth0, args) {
    const state = generateSessionToken()
    const {code_verifier, code_challenge} = verifierAndChallenge()

    return redis
        .setAsync(state, code_verifier)
        .then(_ => {
            let url = getAuthorizationCode({
                ...auth0.config,
                state,
                code_challenge
            })

An authorization action as the OAuth call back, retrieves stored session state and confirms the callback is valid.

const redis = require('nim').redis()

function authorize(auth0, decode, args) {
    const state = args.state
    const authorization_code = args.code

    if (state) {
        return redis
            .getAsync(state)
            .then(code_verifier => {
                if (code_verifier && args.error !== 'unauthorized') {
                    return getToken({
                        ...auth0.config,
                        authorization_code,
                        code_verifier
                    }).then(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment