goth icon indicating copy to clipboard operation
goth copied to clipboard

How to use Goth with gRPC?

Open cyantarek opened this issue 5 years ago • 2 comments

cyantarek avatar Jun 24 '20 01:06 cyantarek

for anyone that finds this and is curious, you can find an example of it here: https://github.com/jrapoport/gothic

jrapoport avatar Mar 29 '21 00:03 jrapoport

Here a working example:

package auth

import (
	"fmt"
	"net/http"

	"github.com/gorilla/sessions"
	"github.com/markbates/goth"
	"github.com/markbates/goth/gothic"
	"github.com/markbates/goth/providers/google"
)

type AuthService struct {
}

func NewAuthService() *AuthService {
	return &AuthService{}
}

func (auth *AuthService) Setup() {
	key := "Secret-session-key" // Replace with your SESSION_SECRET or similar
	maxAge := 86400 * 30        // 30 days
	isProd := false             // Set to true when serving over https

	store := sessions.NewCookieStore([]byte(key))
	store.MaxAge(maxAge)
	store.Options.Path = "/"
	store.Options.HttpOnly = true // HttpOnly should always be enabled
	store.Options.Secure = isProd

	gothic.Store = store
}

func (auth *AuthService) Start() {
	go func() {
		http.HandleFunc("/auth/callback", auth.callback)
		http.HandleFunc("/auth", auth.auth)
		http.ListenAndServe(":8081", http.DefaultServeMux)
	}()
	goth.UseProviders(
		google.New("<APP_ID>", "<APP_SECRET>", "<CALLBACK>", "email"),
	)
}

func (auth *AuthService) Stop() {

}

func (auth *AuthService) callback(w http.ResponseWriter, r *http.Request) {
	user, err := gothic.CompleteUserAuth(w, r)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}
	w.Write([]byte(user.Email))
}

func (auth *AuthService) auth(w http.ResponseWriter, r *http.Request) {
	gothic.BeginAuthHandler(w, r)
}

pascalbros avatar Jun 26 '21 15:06 pascalbros