Skip to content

Commit

Permalink
feat(auth): 🔒 add gitlab pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
laurin-wolf authored and baptisteArno committed May 17, 2022
1 parent bda4116 commit b39e892
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions apps/builder/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,20 @@ const updateLastActivityDate = async (user: User) => {
const getUserGroups = async (account: Account): Promise<string[]> => {
switch (account.provider) {
case 'gitlab': {
const res = await fetch(
`${process.env.GITLAB_BASE_URL || 'https://gitlab.com'}/api/v4/groups`,
{ headers: { Authorization: `Bearer ${account.access_token}` } }
)
const userGroups = await res.json()
return userGroups.map((group: { full_path: string }) => group.full_path)
const getGitlabGroups = async (accessToken: string, page: number = 1): Promise<any[]> => {
const res = await fetch(
`${process.env.GITLAB_BASE_URL || 'https://gitlab.com'}/api/v4/groups?per_page=100&page=${page}`,
{ headers: { Authorization: `Bearer ${accessToken}` } }
)
const groups: any[] = await res.json()
const nextPage = parseInt(res.headers.get('X-Next-Page') || '')
if (nextPage) {
groups.push(...await getGitlabGroups(accessToken, nextPage))
}
return groups
}
const groups = await getGitlabGroups(account.access_token!)
return groups.map((group: { full_path: string }) => group.full_path)
}
default:
return []
Expand Down

0 comments on commit b39e892

Please sign in to comment.