userhandling.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package main
  2. import (
  3. "log"
  4. "math/rand"
  5. "net/http"
  6. glws "git.alfi.li/gamelang/frontend/webserver"
  7. gamelangpb "git.alfi.li/gamelang/protobuf/gamelang"
  8. "github.com/labstack/echo/v4"
  9. )
  10. func userEndpoints(jg *JeopardyGameServer) []glws.Endpoint {
  11. endpoints := []glws.Endpoint{}
  12. endpoints = append(endpoints, glws.Endpoint{"GET", "/user", func(ctx echo.Context) error {
  13. log.Print("/user: get")
  14. log.Print("/user: sending register form")
  15. return ctx.Render(http.StatusOK, "registerform", "")
  16. }})
  17. // POST to /user creates a user
  18. endpoints = append(endpoints, glws.Endpoint{"POST", "/user", func(ctx echo.Context) error {
  19. username := ctx.FormValue("username")
  20. password := ctx.FormValue("password")
  21. log.Printf("new user request: %v", username)
  22. newUser := gamelangpb.User{Name: username, Password: []byte(password)}
  23. id := rand.Uint32()
  24. name := newUser.GetName()
  25. _, err := jg.um.Create(id, name, password)
  26. //user, err := jg.userclient.CreateUser(context.Background(), &newUser)
  27. if err != nil {
  28. return ctx.String(501, "user creation failed")
  29. }
  30. jg.webserver.NewSession(newUser, ctx)
  31. log.Printf("new user created: %v", username)
  32. return ctx.Redirect(302, "/")
  33. }})
  34. endpoints = append(endpoints, glws.Endpoint{"GET", "/login", func(ctx echo.Context) error {
  35. log.Print("/login: get")
  36. // check if the login page was called with failed parameter (set by /login POST)
  37. failedBefore := false
  38. if value := ctx.FormValue("failed"); value == "1" {
  39. failedBefore = true
  40. }
  41. ok, user := jg.webserver.CheckSess(ctx)
  42. if ok {
  43. // logged in
  44. log.Printf("/login: user %v logged in", user.Name)
  45. return ctx.String(http.StatusOK, "hello, "+user.Name+"\nyou are logged in.")
  46. }
  47. // not logged in
  48. log.Print("/login: not logged in")
  49. return ctx.Render(http.StatusOK, "loginform", failedBefore)
  50. }})
  51. endpoints = append(endpoints, glws.Endpoint{"POST", "/login", func(ctx echo.Context) error {
  52. log.Print("/login: post")
  53. username := ctx.FormValue("username")
  54. password := ctx.FormValue("password")
  55. // if the session is already set, the user is already logged in
  56. ok, _ := jg.webserver.CheckSess(ctx)
  57. if ok {
  58. return ctx.String(http.StatusOK, "you are already logged in.")
  59. }
  60. log.Printf("user login attempt: %v", username)
  61. //checkuser := gamelangpb.User{Name: username, Password: []byte(password)}
  62. user, ok := jg.um.CheckUser(username, password) //jg.userclient.CheckUser(context.Background(), &checkuser)
  63. if ok { //err == nil {
  64. jg.webserver.NewSession(user, ctx)
  65. log.Printf("user login suceeded: %v", username)
  66. return ctx.String(http.StatusOK, "hello, "+username+"\nyou are now logged in.")
  67. }
  68. log.Printf("user login failed: %v\n%s", username)
  69. return ctx.Redirect(302, "/login?failed=1")
  70. }})
  71. return endpoints
  72. }