forked from jimafisk/plentiform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sessions_handler.go
39 lines (30 loc) · 1.07 KB
/
sessions_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"net/http"
"github.com/flosch/pongo2"
repo "github.com/plentiform/plentiform/repositories"
)
func (app *Application) SessionsNewHandler(w http.ResponseWriter, r *http.Request) {
app.Render(w, r, "sessions/new", pongo2.Context{})
}
func (app *Application) SessionsCreateHandler(w http.ResponseWriter, r *http.Request) {
session, _ := app.GetSession(r)
user, _ := repo.NewUsersRepository(app.db).FindByEmailAndPassword(r.PostFormValue("email"), r.PostFormValue("password"))
if user == nil {
session.AddFlash("Either your email or password was invalid.")
session.Save(r, w)
app.Render(w, r, "sessions/new", pongo2.Context{"email": r.PostFormValue("email")})
return
}
session.Values["userId"] = user.Id
session.AddFlash("Successfully logged in!")
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}
func (app *Application) SessionsDestroyHandler(w http.ResponseWriter, r *http.Request) {
session, _ := app.GetSession(r)
session.Values["userId"] = nil
session.AddFlash("Successfully logged out!")
session.Save(r, w)
http.Redirect(w, r, "/", 302)
}