package server import ( "fmt" "html/template" "net/http" "github.com/labstack/echo/v4" "github.com/markbates/goth/gothic" "github.com/rjNemo/rentease/internal/auth" "github.com/rjNemo/rentease/internal/view" ) var indexTemplate = `{{range $key,$value:=.Providers}}
Log in with {{index $.ProvidersMap $value}}
{{end}}` var userTemplate = `Name: {{.Name}} [{{.LastName}}, {{.FirstName}}]
Email: {{.Email}}
NickName: {{.NickName}}
Location: {{.Location}}
AvatarURL: {{.AvatarURL}}
Description: {{.Description}}
UserID: {{.UserID}}
AccessToken: {{.AccessToken}}
ExpiresAt: {{.ExpiresAt}}
RefreshToken: {{.RefreshToken}}
` func handleProviderCallback() echo.HandlerFunc { return func(c echo.Context) error { res := c.Response() req := c.Request() user, err := gothic.CompleteUserAuth(res, req) if err != nil { fmt.Fprintln(res, err) return nil } t, _ := template.New("foo").Parse(userTemplate) return t.Execute(res, user) } } func handleProviderLogout() echo.HandlerFunc { return func(c echo.Context) error { res := c.Response() req := c.Request() err := gothic.Logout(res, req) res.Header().Set("Location", "/") res.WriteHeader(http.StatusTemporaryRedirect) return err } } func handleProvider() echo.HandlerFunc { return func(c echo.Context) error { res := c.Response() req := c.Request() // try to get the user without re-authenticating if gothUser, err := gothic.CompleteUserAuth(res, req); err == nil { t, _ := template.New("foo").Parse(userTemplate) return t.Execute(res, gothUser) } else { gothic.BeginAuthHandler(res, req) return nil } } } func handleLoginPage(as *auth.Service) echo.HandlerFunc { return func(c echo.Context) error { return renderTempl(c, http.StatusOK, view.Login(as.GetProviderIndex())) } }