Skip to content

Commit

Permalink
fix: whoami endpoint should return 200 (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonlimlianjie authored May 11, 2021
1 parent b8b3972 commit 1af8bbb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
16 changes: 15 additions & 1 deletion middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,24 @@ const verifyJwt = (req, res, next) => {
return next('router')
}

// Extracts access_token if any, else set access_token to null
const whoamiAuth = (req, res, next) => {
let access_token
try {
const { isomercms } = req.cookies
access_token = jwtUtils.verifyToken(isomercms).access_token
} catch (err) {
access_token = undefined
} finally {
req.accessToken = access_token
return next('router')
}
}

// Login and logout
auth.get('/v1/auth', noVerify)
auth.get('/v1/auth/logout', noVerify)
auth.get('/v1/auth/whoami', verifyJwt)
auth.get('/v1/auth/whoami', whoamiAuth)

// Index
auth.get('/v1', noVerify)
Expand Down
23 changes: 14 additions & 9 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,20 @@ async function whoami(req, res) {
// Make a call to github
const endpoint = 'https://api.github.com/user'

const resp = await axios.get(endpoint, {
headers: {
Authorization: `token ${accessToken}`,
"Content-Type": "application/json"
}
})

const { login: userId } = resp.data
res.status(200).json({ userId })
let userId
try {
const resp = await axios.get(endpoint, {
headers: {
Authorization: `token ${accessToken}`,
"Content-Type": "application/json"
}
})
userId = resp.data.login
} catch (err) {
userId = undefined
} finally {
res.status(200).json({ userId })
}
}

router.get('/', attachReadRouteHandlerWrapper(githubAuth));
Expand Down

0 comments on commit 1af8bbb

Please sign in to comment.