Browse Source

working login

main
Stephanie Gredell 6 months ago
parent
commit
0ddb6210ba
  1. 5
      go.mod
  2. 2
      go.sum
  3. 51
      handlers/auth.go
  4. 5
      handlers/welcome.go
  5. 30
      main.go
  6. 1
      templates/index.html
  7. 16
      templates/welcome.html
  8. 2
      tmp/build-errors.log
  9. BIN
      tmp/main
  10. 4
      todo.md

5
go.mod

@ -2,7 +2,10 @@ module codegrillathon @@ -2,7 +2,10 @@ module codegrillathon
go 1.22.2
require github.com/markbates/goth v1.81.0
require (
github.com/joho/godotenv v1.5.1
github.com/markbates/goth v1.81.0
)
require (
github.com/go-chi/chi/v5 v5.1.0 // indirect

2
go.sum

@ -17,6 +17,8 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC @@ -17,6 +17,8 @@ github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyC
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.1.1 h1:YMDmfaK68mUixINzY/XjscuJ47uXFWSSHzFbBQM0PrE=
github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/markbates/goth v1.81.0 h1:XVcCkeGWokynPV7MXvgb8pd2s3r7DS40P7931w6kdnE=
github.com/markbates/goth v1.81.0/go.mod h1:+6z31QyUms84EHmuBY7iuqYSxyoN3njIgg9iCF/lR1k=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

51
handlers/auth.go

@ -1,23 +1,58 @@ @@ -1,23 +1,58 @@
package handlers
import (
"github.com/markbates/goth/gothic"
"fmt"
"net/http"
"os"
"github.com/gorilla/sessions"
"github.com/markbates/goth/gothic"
)
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
err := h.Template.ExecuteTemplate(w, "login.html", nil)
if err != nil {
http.Error(w, "Template rendering error", http.StatusInternalServerError)
}
func (h *Handler) Auth(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
q.Add("provider", "twitch")
r.URL.RawQuery = q.Encode()
gothic.BeginAuthHandler(w, r)
}
func (h *Handler) Callback(w http.ResponseWriter, r *http.Request) {
user, err := gothic.CompleteUserAuth(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Println("Call back happened")
fmt.Println("we are creating the user store")
key := os.Getenv("SESSION_SECRET") // 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
session, _ := gothic.Store.Get(r, "user-session")
session.Values["user_name"] = user.Name
session.Values["avatar_url"] = user.AvatarURL
fmt.Println("we are saving the session")
err = session.Save(r, w)
if err != nil {
fmt.Printf("error saving the session: %v", err)
}
fmt.Println("we are about to redirect")
http.Redirect(w, r, "/welcome", http.StatusFound)
}
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
session, err := gothic.Store.Get(r, "_gothic-session")
session, err := gothic.Store.Get(r, "user-session")
if err != nil {
return
}
@ -31,5 +66,5 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) { @@ -31,5 +66,5 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
return
}
//w.Redirect(http.StatusTemporaryRedirect, "/")
http.Redirect(w, r, "/", http.StatusFound)
}

5
handlers/welcome.go

@ -1,11 +1,14 @@ @@ -1,11 +1,14 @@
package handlers
import (
"fmt"
"net/http"
)
func (h *Handler) Welcome(w http.ResponseWriter, r *http.Request) {
err := h.Template.ExecuteTemplate(w, "index.html", nil)
fmt.Printf("we have reached the welcome route")
err := h.Template.ExecuteTemplate(w, "welcome.html", nil)
if err != nil {
http.Error(w, "Template rendering error", http.StatusInternalServerError)
}

30
main.go

@ -4,39 +4,47 @@ import ( @@ -4,39 +4,47 @@ import (
"codegrillathon/handlers"
"context"
"fmt"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/twitch"
"html/template"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/joho/godotenv"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/twitch"
)
func main() {
err := godotenv.Load()
if err != nil {
panic("HELP ME LOAD SOME ENV FILE")
}
tmpl, err := template.ParseGlob("templates/*.html")
if err != nil {
panic(fmt.Errorf("failed to parse templates: %w\n", err))
}
goth.UseProviders(
twitch.New(os.Getenv("TWITCH_CLIENT_ID"), os.Getenv("TWITCH_SECRET"), "https://localhost:8080/auth/twitch/callback"),
github.New(os.Getenv("GITHUB_KEY"), os.Getenv("GITHUB_SECRET"), "https://localhost:8080/auth/github/callback"),
)
h := handlers.Handler{Template: *tmpl}
mux := http.NewServeMux()
fs := http.FileServer(http.Dir("assets"))
mux.Handle("/static/", http.StripPrefix("/static/", fs))
goth.UseProviders(
twitch.New(os.Getenv("TWITCH_CLIENT_ID"), os.Getenv("TWITCH_SECRET"), "http://localhost:8080/auth/twitch/callback"),
github.New(os.Getenv("GITHUB_KEY"), os.Getenv("GITHUB_SECRET"), "http://localhost:3000/auth/github/callback"),
)
// auth routes
mux.HandleFunc("/auth/login/:provider", auth.Login)
mux.HandleFunc("/auth/:provider/callback", auth.Callback)
mux.HandleFunc("/auth/logout/:provider", auth.Logout)
mux.HandleFunc("/auth/twitch", h.Auth)
mux.HandleFunc("/auth/twitch/callback", h.Callback)
mux.HandleFunc("/logout/", h.Logout)
// pages route
mux.HandleFunc("/", h.Home)
mux.HandleFunc("/welcome", h.Welcome)
@ -47,7 +55,7 @@ func main() { @@ -47,7 +55,7 @@ func main() {
go func() {
fmt.Println("Server started...")
if err := server.ListenAndServe(); err != http.ErrServerClosed {
if err := server.ListenAndServeTLS("server.crt", "server.key"); err != http.ErrServerClosed {
fmt.Printf("Serve error: %v\n", err)
}
}()

1
templates/index.html

@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
<li><a href="#features">Features</a></li>
<li><a href="#how-it-works">How It Works</a></li>
<li><a href="#submit">Submit Project</a></li>
<li><a href="/auth/twitch">Login with Twitch</a></li>
</ul>
</nav>
</div>

16
templates/welcome.html

@ -9,21 +9,7 @@ @@ -9,21 +9,7 @@
</head>
<body>
<header>
<div class="container">
<div class="logo">
<h1>Codegrillathon</h1>
</div>
<nav>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#features">Features</a></li>
<li><a href="#how-it-works">How It Works</a></li>
<li><a href="#submit">Submit Project</a></li>
</ul>
</nav>
</div>
</header>
<header>WELCOME PAGE</header>
<section id="home" class="hero">
<div class="container">

2
tmp/build-errors.log

@ -1 +1 @@ @@ -1 +1 @@
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1

BIN
tmp/main

Binary file not shown.

4
todo.md

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
userid user_type username capabilities
1 hacker|repo|streamer codegirl 1,2,3,4,5
userid user_type username capabilities provider avatar_url
1 hacker|repo|streamer codegirl 1,2,3,4,5 twitch|github ...
capability_id capability_name
1 join_hackathon

Loading…
Cancel
Save