Skip to content

henriqueatila/golang-gin-best-practices / skills/golang-gin-auth

golang-gin-auth

Verified

JWT auth and RBAC for Go Gin APIs. Use when adding login, signup, JWT tokens, role checks, protected routes, or password hashing to a Gin app.

references/oauth2-github-profile.md

View on GitHub
# OAuth2 — GitHub Profile Fetcher

See also: `oauth2-flows.md`, `oauth2-google-and-security.md`

## FetchGitHubProfile

Fetches the authenticated user's profile from the GitHub API using the OAuth2 access token.

```go
// internal/auth/github_profile.go
package auth

import (
    "context"
    "encoding/json"
    "fmt"
    "net/http"
)

type GitHubProfile struct {
    ID    int64  `json:"id"`
    Login string `json:"login"`
    Email string `json:"email"`
}

func FetchGitHubProfile(ctx context.Context, accessToken string) (*GitHubProfile, error) {
    req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/user", nil)
    if err != nil {
        return nil, err
    }
    req.Header.Set("Authorization", "Bearer "+accessToken)
    req.Header.Set("Accept", "application/vnd.github+json")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf("github api returned %d", resp.StatusCode)
    }

    var profile GitHubProfile
    return &profile, json.NewDecoder(resp.Body).Decode(&profile)
}
```

**Note:** GitHub may not return the email field if the user has set it to private. In that case, call `GET /user/emails` with the `user:email` scope to retrieve verified email addresses.
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall