-
Notifications
You must be signed in to change notification settings - Fork 3
/
account-service.go
99 lines (82 loc) · 3.03 KB
/
account-service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package service
import (
"fmt"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/dgrijalva/jwt-go"
cons "github.com/sayeed1999/doctor-appointment-api-golang-hexagonal-architecture/internal/constants"
"github.com/sayeed1999/doctor-appointment-api-golang-hexagonal-architecture/internal/core/domain"
"github.com/sayeed1999/doctor-appointment-api-golang-hexagonal-architecture/internal/core/ports"
"github.com/sayeed1999/doctor-appointment-api-golang-hexagonal-architecture/internal/validators"
"golang.org/x/crypto/bcrypt"
)
type accountService struct {
*base
repo ports.AccountRepository
}
func InitializeAccountService(b *base, r ports.AccountRepository) *accountService {
return &accountService{
base: b,
repo: r,
}
}
func (s *accountService) Register(user domain.User) (domain.User, int, string) {
// fullname valiation
if strings.TrimSpace(user.Fullname) == "" {
return user, http.StatusBadRequest, "Name cannot be white spaces"
}
// email validation
if !validators.IsValidEmail(user.Email) {
return user, http.StatusBadRequest, cons.ApplicationMessage.InvalidEmail
}
// password validation
//TODO:- currently the password is hard-coded, should not be in production
user.Password = "123456Aa"
// phone validation
// encrypting password with bcrypt
user.Password, user.Cost = encryptPasswordWithBcrypt(user.Password)
if err := s.repo.Create(user); err != nil {
return user, http.StatusBadRequest, err.Error()
}
return user, http.StatusCreated, "New account has been created"
}
func encryptPasswordWithBcrypt(password string) (string, int) { // returns (hashedPassword, cost)
rand.Seed(time.Now().UnixNano())
var cost int = rand.Intn(20) + 1
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), cost)
var hashedPassword string = string(bytes)
return hashedPassword, cost
}
func (s *accountService) Login(email string, password string) (string, int, string) {
var user domain.User
// find if the email exists
if err := s.repo.FindFirst(&user, "email = ?", email); err != nil {
return "", http.StatusInternalServerError, ""
}
if user.ID == 0 {
return "", http.StatusNotFound, cons.ApplicationMessage.EmailNotRegistered
}
// hashedPasswordInBytes, _ := bcrypt.GenerateFromPassword([]byte(password), user.Cost)
// hashedPassword := string(hashedPasswordInBytes)
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)); err != nil {
return "", http.StatusBadRequest, cons.ApplicationMessage.WrongPassword
}
claims := jwt.StandardClaims{
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
Issuer: strconv.Itoa(int(user.ID)),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, _ := token.SignedString([]byte(s.conf.Jwt.SecretKey))
return signedToken, http.StatusAccepted, ""
}
func (s *accountService) GetUserById(id int) (domain.User, int, string) {
var user domain.User
_ = s.repo.FindById(&user, id)
if user.ID == 0 {
return user, http.StatusNotFound, fmt.Sprintf(cons.ApplicationMessage.ItemNotFound, "User")
}
return user, http.StatusFound, ""
}