Skip to content

Commit

Permalink
sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
tsawler committed Feb 10, 2021
1 parent 53b8ece commit a6520ef
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
17 changes: 16 additions & 1 deletion cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@ import (
"bookings-udemy/pkg/handlers"
"bookings-udemy/pkg/render"
"fmt"
"github.com/alexedwards/scs/v2"
"log"
"net/http"
"time"
)

const portNumber = ":8080"

var app config.AppConfig
var session *scs.SessionManager

// main is the main function
func main() {
var app config.AppConfig
// change this to true when in production
app.InProduction = false

// set up the session
session = scs.New()
session.Lifetime = 24 * time.Hour
session.Cookie.Persist = true
session.Cookie.SameSite = http.SameSiteLaxMode
session.Cookie.Secure = app.InProduction

app.Session = session

tc, err := render.CreateTemplateCache()
if err != nil {
Expand Down
15 changes: 6 additions & 9 deletions cmd/web/middleware.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package main

import (
"fmt"
"github.com/justinas/nosurf"
"net/http"
)

func WriteToConsole(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Hit the page")
next.ServeHTTP(w, r)
})
}

// NoSurf is the csrf protection middleware
func NoSurf(next http.Handler) http.Handler {
csrfHandler := nosurf.New(next)

csrfHandler.SetBaseCookie(http.Cookie{
HttpOnly: true,
Path: "/",
Secure: false,
Secure: app.InProduction,
SameSite: http.SameSiteLaxMode,
})
return csrfHandler
}

// SessionLoad loads and saves session data for current request
func SessionLoad(next http.Handler) http.Handler {
return session.LoadAndSave(next)
}
1 change: 1 addition & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func routes(app *config.AppConfig) http.Handler {

mux.Use(middleware.Recoverer)
mux.Use(NoSurf)
mux.Use(SessionLoad)

mux.Get("/", handlers.Repo.Home)
mux.Get("/about", handlers.Repo.About)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module bookings-udemy
go 1.15

require (
github.com/alexedwards/scs/v2 v2.4.0
github.com/go-chi/chi v1.5.1
github.com/justinas/nosurf v1.1.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
github.com/alexedwards/scs v1.4.1 h1:/5L5a07IlqApODcEfZyMsu8Smd1S7Q4nBjEyKxIRTp0=
github.com/alexedwards/scs/v2 v2.4.0 h1:XfnMamKnvp1muJVNr1WzikQTclopsBXWZtzz0NBjOK0=
github.com/alexedwards/scs/v2 v2.4.0/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/go-chi/chi v1.5.1 h1:kfTK3Cxd/dkMu/rKs5ZceWYp+t5CtiE7vmaTv3LjC6w=
github.com/go-chi/chi v1.5.1/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k=
github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk=
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"github.com/alexedwards/scs/v2"
"html/template"
"log"
)
Expand All @@ -10,4 +11,6 @@ type AppConfig struct {
UseCache bool
TemplateCache map[string]*template.Template
InfoLog *log.Logger
InProduction bool
Session *scs.SessionManager
}

0 comments on commit a6520ef

Please sign in to comment.