-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71eff1f
commit b4dfd80
Showing
7 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/markbates/goth/gothic" | ||
"html/template" | ||
"net/http" | ||
) | ||
|
||
func (cfg *BookclubServer) handlerCallback(w http.ResponseWriter, r *http.Request) { | ||
r = r.WithContext(context.WithValue(context.Background(), "provider", "google")) //todo I don't understand that tbh | ||
|
||
user, err := gothic.CompleteUserAuth(w, r) | ||
if err != nil { | ||
fmt.Fprintln(w, err) | ||
return | ||
} | ||
fmt.Println(user) //todo delete | ||
t, _ := template.New("foo").Parse(userTemplate) | ||
t.Execute(w, user) | ||
} | ||
|
||
func (cfg *BookclubServer) handlerLogout(w http.ResponseWriter, r *http.Request) { | ||
gothic.Logout(w, r) | ||
w.Header().Set("Location", "/") | ||
w.WriteHeader(http.StatusTemporaryRedirect) | ||
} | ||
|
||
func (cfg *BookclubServer) handlerLogin(w http.ResponseWriter, r *http.Request) { | ||
r = r.WithContext(context.WithValue(context.Background(), "provider", "google")) //todo I don't understand that tbh | ||
// try to get the user without re-authenticating | ||
if gothUser, err := gothic.CompleteUserAuth(w, r); err == nil { | ||
t, _ := template.New("foo").Parse(userTemplate) | ||
t.Execute(w, gothUser) | ||
} else { | ||
gothic.BeginAuthHandler(w, r) | ||
} | ||
} | ||
|
||
func (cfg *BookclubServer) handlerProviders(w http.ResponseWriter, r *http.Request) { | ||
|
||
//providerIndex := &ProviderIndex{Providers: keys, ProvidersMap: m} | ||
|
||
t, _ := template.New("foo").Parse(indexTemplate) | ||
t.Execute(w, ProviderIndex{}) | ||
} | ||
|
||
type ProviderIndex struct { | ||
Providers []string | ||
ProvidersMap map[string]string | ||
} | ||
|
||
var indexTemplate = `{{range $key,$value:=.Providers}} | ||
<p><a href="/auth/{{$value}}">Log in with {{index $.ProvidersMap $value}}</a></p> | ||
{{end}}` | ||
|
||
var userTemplate = ` | ||
<p><a href="/logout/{{.Provider}}">logout</a></p> | ||
<p>Name: {{.Name}} [{{.LastName}}, {{.FirstName}}]</p> | ||
<p>Email: {{.Email}}</p> | ||
<p>NickName: {{.NickName}}</p> | ||
<p>Location: {{.Location}}</p> | ||
<p>AvatarURL: {{.AvatarURL}} <img src="{{.AvatarURL}}"></p> | ||
<p>Description: {{.Description}}</p> | ||
<p>UserID: {{.UserID}}</p> | ||
<p>AccessToken: {{.AccessToken}}</p> | ||
<p>ExpiresAt: {{.ExpiresAt}}</p> | ||
<p>RefreshToken: {{.RefreshToken}}</p> | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package auth | ||
|
||
import ( | ||
"github.com/gorilla/sessions" | ||
"github.com/joho/godotenv" | ||
"github.com/markbates/goth" | ||
"github.com/markbates/goth/gothic" | ||
"github.com/markbates/goth/providers/google" | ||
"log" | ||
"os" | ||
) | ||
|
||
const ( | ||
key = "jaywalker2-grab-scoop" | ||
MaxAge = 86400 * 30 | ||
IsProd = false | ||
) | ||
|
||
func NewAuth() { | ||
err := godotenv.Load() | ||
if err != nil { | ||
log.Fatal("Error loading .env file") | ||
} | ||
|
||
googleClientId := os.Getenv("GOOGLE_CLIENT_ID") | ||
googleClientSecret := os.Getenv("GOOGLE_CLIENT_SECRET") | ||
|
||
store := sessions.NewCookieStore([]byte(key)) | ||
store.MaxAge(MaxAge) | ||
|
||
store.Options.Path = "/" | ||
store.Options.HttpOnly = true | ||
store.Options.Secure = IsProd | ||
|
||
gothic.Store = store | ||
goth.UseProviders(google.New(googleClientId, googleClientSecret, "http://localhost:8080/auth/google/callback")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters