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.

113 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. _, _, err = srv.gamesrv.CreatePlanet(user.Id)
  45. return user, err
  46. }
  47. var signingKey = []byte("TODO") // TODO
  48. func (srv Service) Login(email, password string) (*string, *models.User, error) {
  49. var userDb models.UserDb
  50. err := srv.db.Users.Find(bson.M{"email": email}).One(&userDb)
  51. if err != nil {
  52. return nil, nil, errors.New("user not exist")
  53. }
  54. if !checkPasswordHash(password, userDb.Password) {
  55. return nil, nil, errors.New("error with password")
  56. }
  57. user := models.UserDbToUser(srv.db, userDb)
  58. // create jwt
  59. token := jwt.New(jwt.SigningMethodHS256)
  60. claims := make(jwt.MapClaims)
  61. claims["user"] = true
  62. claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
  63. token.Claims = claims
  64. tokenString, err := token.SignedString(signingKey)
  65. if err != nil {
  66. return nil, nil, errors.New("error creating token")
  67. }
  68. return &tokenString, user, err
  69. }
  70. func (srv Service) GetUserById(userid bson.ObjectId) (*models.User, error) {
  71. var userDb models.UserDb
  72. err := srv.db.Users.Find(bson.M{"_id": userid}).One(&userDb)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return models.UserDbToUser(srv.db, userDb), nil
  77. }
  78. func (srv Service) GetUserPlanetsById(userid bson.ObjectId) ([]*models.Planet, error) {
  79. var planets []*models.Planet
  80. err := srv.db.Planets.Find(bson.M{"ownerid": userid}).All(&planets)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return planets, err
  85. }
  86. // func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
  87. // // update user stats
  88. // user := getUserFromDB
  89. // user.GetStats()
  90. //
  91. // }
  92. //
  93. // func (srv Service) GetUser(id bson.ObjectId) (*models.User, error) {
  94. // // update user stats
  95. //
  96. // }