Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 1.58 KB

README.md

File metadata and controls

65 lines (49 loc) · 1.58 KB

dggoauth

this package implements the oauth flow from here

Example

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/tensei/dggoauth"
)

func main() {

    // ClientID and ClientSecret from here
    // https://www.destiny.gg/profile/developer
    dgg, err := dggoauth.NewClient(&dggoauth.Options{
        ClientID:     "xxxxxxxxxxxxxxxx", // required
        ClientSecret: "xxxxxxxxxxxxxxxx", // required
        RedirectURI:  "https://linktomysites/callback", // required
    })
    if err != nil {
        panic(err)
    }

    stateVerifierStore := make(map[string]string)

    r := gin.Default()
    r.GET("/login", func(c *gin.Context) {
        state := "randomlyGeneratedState"
        redirectURL, verifier := dgg.GetAuthorizationURL(state)

        // store state and verifier together
        stateVerifierStore[state] = verifier

        // redirect the user to the redirectURL
        c.Redirect(http.StatusFound, redirectURL)
    })

    // after they login they get sent back to your redirect URL
    r.GET("/callback", func(c *gin.Context) {
        // same state you provided above
        state := c.Query("state")
        code := c.Query("code")

        // get your verifier from the store
        verifier := stateVerifierStore[state]

        // get the access token
        at, err := dgg.GetAccessToken(code, verifier)
        if err != nil {
            panic(err)
        }

        // get userinfo with access token or whatever

        c.Redirect(http.StatusFound, "/")
    })
    r.Run()
}