Exemplo n.º 1
0
func PostLoginHandler(w http.ResponseWriter, rnd render.Render, r *http.Request, s *session.Session) {
	username := r.FormValue("username")
	password := r.FormValue("password")

	s.Username = username
	s.IsAuthorized = true

	fmt.Println(username)
	fmt.Println(password)

	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims["Name"] = "token"
	token.Claims["exp"] = time.Now().Add(time.Minute * 5).Unix()
	tokenString, err := token.SignedString([]byte(session.TOKEN_STR))
	fmt.Println(tokenString)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Printf("Token Signing error: %v\n", err)
		fmt.Fprintln(w, "Sorry, error while Signing Token!")
	}

	http.SetCookie(w, &http.Cookie{
		Name:  session.TOKEN_NAME,
		Value: tokenString,
	})
	s.Id = tokenString

	storage, err := models.GetStorage()
	if err != nil {
		log.Fatalln(err)
	}
	storage.SaveUser(models.NewUser(username))
	rnd.Redirect("/")
}
Exemplo n.º 2
0
func LogoutHandler(rnd render.Render, w http.ResponseWriter, r *http.Request, s *session.Session) {
	s.Username = ""
	s.Id = ""
	s.IsAuthorized = false
	http.SetCookie(w, &http.Cookie{
		Name:  session.TOKEN_NAME,
		Value: " ",
	})

	rnd.Redirect("/")
}