Add user sessions
This commit is contained in:
@@ -14,7 +14,8 @@ type Users struct {
|
||||
Signup Template
|
||||
Signin Template
|
||||
}
|
||||
UserService *models.UserService
|
||||
UserService *models.UserService
|
||||
SessionService *models.SessionService
|
||||
}
|
||||
|
||||
func (u Users) GetSignup(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -34,7 +35,22 @@ func (u Users) PostSignup(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "User created: %+v", user)
|
||||
session, err := u.SessionService.Create(user.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Redirect(w, r, "/signin", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
cookie := http.Cookie{
|
||||
Name: "session",
|
||||
Value: session.Token,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
|
||||
http.Redirect(w, r, "/users/me", http.StatusFound)
|
||||
}
|
||||
|
||||
func (u Users) GetSignin(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -58,10 +74,16 @@ func (u Users) PostSignin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Bad cookie
|
||||
session, err := u.SessionService.Create(user.ID)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
cookie := http.Cookie{
|
||||
Name: "bad",
|
||||
Value: user.Email,
|
||||
Name: "session",
|
||||
Value: session.Token,
|
||||
Path: "/",
|
||||
HttpOnly: true,
|
||||
}
|
||||
@@ -71,23 +93,31 @@ func (u Users) PostSignin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (u Users) CurrentUser(w http.ResponseWriter, r *http.Request) {
|
||||
email, err := r.Cookie("bad")
|
||||
seshCookie, err := r.Cookie("session")
|
||||
if err != nil {
|
||||
fmt.Fprint(w, "The bad cookie could not be read.")
|
||||
fmt.Println(err)
|
||||
http.Redirect(w, r, "/signin", http.StatusFound)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "Bad cookie: %s\n", email.Value)
|
||||
user, err := u.SessionService.User(seshCookie.Value)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Redirect(w, r, "/signin", http.StatusFound)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "Current user: %s\n", user.Email)
|
||||
}
|
||||
|
||||
func WithTemplates(user_service *models.UserService, signup Template, signin Template) Users {
|
||||
func WithTemplates(user_service *models.UserService, session_service *models.SessionService, signup Template, signin Template) Users {
|
||||
u := Users{}
|
||||
u.Templates.Signup = signup
|
||||
u.Templates.Signin = signin
|
||||
u.UserService = user_service
|
||||
u.SessionService = session_service
|
||||
return u
|
||||
}
|
||||
|
||||
func Default(user_service *models.UserService, templatePath ...string) Users {
|
||||
func Default(user_service *models.UserService, session_service *models.SessionService) Users {
|
||||
signup_tpl := views.Must(views.FromFS(templates.FS, "signup.gohtml", "tailwind.gohtml"))
|
||||
signin_tpl := views.Must(views.FromFS(templates.FS, "signin.gohtml", "tailwind.gohtml"))
|
||||
|
||||
@@ -100,5 +130,5 @@ func Default(user_service *models.UserService, templatePath ...string) Users {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return WithTemplates(user_service, signup_tpl, signin_tpl)
|
||||
return WithTemplates(user_service, session_service, signup_tpl, signin_tpl)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user