You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.8 KiB

  1. package usersrv
  2. import (
  3. "errors"
  4. "time"
  5. "github.com/arnaucube/gogame/database"
  6. "github.com/arnaucube/gogame/models"
  7. "github.com/arnaucube/gogame/services/gamesrv"
  8. jwt "github.com/dgrijalva/jwt-go"
  9. "golang.org/x/crypto/bcrypt"
  10. "gopkg.in/mgo.v2/bson"
  11. )
  12. type Service struct {
  13. db *database.Db
  14. gamesrv *gamesrv.Service
  15. }
  16. func New(db *database.Db, gameservice *gamesrv.Service) *Service {
  17. return &Service{
  18. db: db,
  19. gamesrv: gameservice,
  20. }
  21. }
  22. func hashPassword(password string) (string, error) {
  23. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
  24. return string(hash), err
  25. }
  26. func checkPasswordHash(password, hash string) bool {
  27. err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
  28. return err == nil
  29. }
  30. func (srv Service) Register(name, password, email string) (*models.User, error) {
  31. var userDb models.User
  32. err := srv.db.Users.Find(bson.M{"email": email}).One(&userDb)
  33. if err == nil {
  34. return nil, errors.New("user already exist")
  35. }
  36. hashedPassword, err := hashPassword(password)
  37. if err != nil {
  38. return nil, err
  39. }
  40. user, err := models.NewUser(srv.db, name, hashedPassword, email)
  41. if err != nil {
  42. return nil, err
  43. }
  44. _, planet, err := srv.gamesrv.CreatePlanet(user.Id)
  45. user.Planets = append(user.Planets, planet.Id)
  46. user.StoreInDb()
  47. return user, err
  48. }
  49. var signingKey = []byte("TODO") // TODO
  50. func (srv Service) Login(email, password string) (*string, *models.User, error) {
  51. var userDb models.UserDb
  52. err := srv.db.Users.Find(bson.M{"email": email}).One(&userDb)
  53. if err != nil {
  54. return nil, nil, errors.New("user not exist")
  55. }
  56. if !checkPasswordHash(password, userDb.Password) {
  57. return nil, nil, errors.New("error with password")
  58. }
  59. user := models.UserDbToUser(srv.db, userDb)
  60. // create jwt
  61. token := jwt.New(jwt.SigningMethodHS256)
  62. claims := make(jwt.MapClaims)
  63. claims["user"] = true
  64. claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
  65. token.Claims = claims
  66. tokenString, err := token.SignedString(signingKey)
  67. if err != nil {
  68. return nil, nil, errors.New("error creating token")
  69. }
  70. return &tokenString, user, err
  71. }
  72. func (srv Service) GetUserById(userid bson.ObjectId) (*models.User, error) {
  73. var userDb models.UserDb
  74. err := srv.db.Users.Find(bson.M{"_id": userid}).One(&userDb)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return models.UserDbToUser(srv.db, userDb), nil
  79. }
  80. func (srv Service) GetUserPlanetsById(userid bson.ObjectId) ([]*models.Planet, error) {
  81. var planets []*models.Planet
  82. err := srv.db.Planets.Find(bson.M{"ownerid": userid}).All(&planets)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return planets, err
  87. }
  88. // func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
  89. // // update user stats
  90. // user := getUserFromDB
  91. // user.GetStats()
  92. //
  93. // }
  94. //
  95. // func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
  96. // // update user stats
  97. //
  98. // }