Skip to content

Commit

Permalink
check for valid token.
Browse files Browse the repository at this point in the history
  • Loading branch information
bradgarropy committed Dec 21, 2019
1 parent 23a6ee4 commit ad87671
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/github.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {getOctokit} = require("./octokit")
const {createOctokit, getOctokit} = require("./octokit")

const getRateLimit = async () => {
const octokit = getOctokit()
Expand All @@ -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,
Expand Down Expand Up @@ -61,6 +73,7 @@ const createLabels = async (labels, owner, repo) => {

module.exports = {
getRateLimit,
validToken,
getLabels,
deleteLabels,
createLabels,
Expand Down
15 changes: 9 additions & 6 deletions src/handlers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const conf = require("conf")
const clone = require("./clone")
const {validToken} = require("./github")
const {createOctokit} = require("./octokit")

const config = new conf()
Expand All @@ -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 = () => {
Expand Down

0 comments on commit ad87671

Please sign in to comment.