Skip to content

Instantly share code, notes, and snippets.

@john-osullivan
Last active November 7, 2019 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-osullivan/a6aa13f5ef90f7e8d6ae9367d5e45289 to your computer and use it in GitHub Desktop.
Save john-osullivan/a6aa13f5ef90f7e8d6ae9367d5e45289 to your computer and use it in GitHub Desktop.
Dev Diary #3 - Require Auth Data with Middleware
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