Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: whoami endpoint should return 200 #171

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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