Last active
November 7, 2019 21:44
-
-
Save john-osullivan/a6aa13f5ef90f7e8d6ae9367d5e45289 to your computer and use it in GitHub Desktop.
Dev Diary #3 - Require Auth Data with Middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import User from "@eximchain/dappbot-types/spec/user"; | |
export const requireAuthData:MiddlewareFunction<UniversalArgs> = (args) => { | |
// This function will show the user an error and then | |
// exit the process, preventing the command handler | |
// from ever actually being called. | |
function authErr() { | |
render( | |
<ErrorBox errMsg={"This command requires you to log in; please call 'dappbot signup' or 'dappbot login'."} /> | |
) | |
process.exit(1); | |
} | |
// Make sure there is an auth file | |
if (!args.authFile) { | |
authErr(); | |
return; | |
} | |
const authData = JSON.parse(args.authFile); | |
// Make sure it has the appropriate shape | |
if (!User.isAuthData(authData)) { | |
authErr(); | |
return; | |
} | |
// Our authStatus helper function tells us | |
// whether a valid AuthData object is active, | |
// stale, or empty. We can refresh if stale, | |
// but if it's empty bail out. | |
if (User.authStatus(authData).isEmpty) { | |
authErr() | |
return; | |
} | |
// If all these checks pass successfully, | |
// we either have an active login or we | |
// have a stale one which will be | |
// refreshed upon <App> mount. | |
// Good to go! | |
return args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment