From ad87671fb168f92574d22e97ad661b776917bbb4 Mon Sep 17 00:00:00 2001 From: Brad Garropy Date: Fri, 20 Dec 2019 21:30:18 -0600 Subject: [PATCH] check for valid token. --- src/github.js | 15 ++++++++++++++- src/handlers.js | 15 +++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/github.js b/src/github.js index 21e31b9..dd24662 100644 --- a/src/github.js +++ b/src/github.js @@ -1,4 +1,4 @@ -const {getOctokit} = require("./octokit") +const {createOctokit, getOctokit} = require("./octokit") const getRateLimit = async () => { const octokit = getOctokit() @@ -8,6 +8,18 @@ const getRateLimit = async () => { console.log(`${remaining}/${limit} remaining`) } +const validToken = async token => { + const octokit = createOctokit(token) + + try { + await octokit.users.getAuthenticated() + } catch (error) { + return false + } + + return true +} + const getLabels = async (owner, repo) => { const parameters = { owner, @@ -61,6 +73,7 @@ const createLabels = async (labels, owner, repo) => { module.exports = { getRateLimit, + validToken, getLabels, deleteLabels, createLabels, diff --git a/src/handlers.js b/src/handlers.js index e8b18d8..0872a73 100644 --- a/src/handlers.js +++ b/src/handlers.js @@ -1,5 +1,6 @@ const conf = require("conf") const clone = require("./clone") +const {validToken} = require("./github") const {createOctokit} = require("./octokit") const config = new conf() @@ -13,15 +14,17 @@ const loginHandler = async argv => { return } - const octokit = createOctokit(token) + const valid = await validToken(token) - try { - await octokit.users.getAuthenticated() - config.set({username, token}) - console.log("Login successful!") - } catch (error) { + if (!valid) { console.log("Login failed! Please try again.") + return } + + config.set({username, token}) + console.log("Login successful!") + + return } const logoutHandler = () => {