Checking to see if a user is logged in, and logging a user our
This commit is contained in:
		@ -494,3 +494,11 @@ func (m *Repository) PostShowLogin(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	m.App.Session.Put(r.Context(), "flash", "Logged in successfully")
 | 
			
		||||
	http.Redirect(w, r, "/", http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Logout logs a user out
 | 
			
		||||
func (m *Repository) Logout(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	// TODO Use Redis to store the session. Check the documentation of scs package
 | 
			
		||||
	m.App.Session.Destroy(r.Context())
 | 
			
		||||
	m.App.Session.RenewToken(r.Context())
 | 
			
		||||
	http.Redirect(w, r, "/user/login", http.StatusSeeOther)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -4,13 +4,14 @@ import "go-udemy-web-1/internal/forms"
 | 
			
		||||
 | 
			
		||||
// TemplateData holds data sent from handlers to templates
 | 
			
		||||
type TemplateData struct {
 | 
			
		||||
	StringMap map[string]string
 | 
			
		||||
	IntMap    map[string]int
 | 
			
		||||
	FloatMap  map[string]float32
 | 
			
		||||
	Data      map[string]interface{}
 | 
			
		||||
	CSRFToken string
 | 
			
		||||
	Flash     string
 | 
			
		||||
	Warning   string
 | 
			
		||||
	Error     string
 | 
			
		||||
	Form      *forms.Form
 | 
			
		||||
	StringMap       map[string]string
 | 
			
		||||
	IntMap          map[string]int
 | 
			
		||||
	FloatMap        map[string]float32
 | 
			
		||||
	Data            map[string]interface{}
 | 
			
		||||
	Form            *forms.Form
 | 
			
		||||
	CSRFToken       string
 | 
			
		||||
	Flash           string
 | 
			
		||||
	Warning         string
 | 
			
		||||
	Error           string
 | 
			
		||||
	IsAuthenticated int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -32,6 +32,9 @@ func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateDa
 | 
			
		||||
	td.Warning = app.Session.PopString(r.Context(), "warning")
 | 
			
		||||
	td.Error = app.Session.PopString(r.Context(), "error")
 | 
			
		||||
	td.CSRFToken = nosurf.Token(r)
 | 
			
		||||
	if app.Session.Exists(r.Context(), "user_id") {
 | 
			
		||||
		td.IsAuthenticated = 1
 | 
			
		||||
	}
 | 
			
		||||
	return td
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user