/
public.go
83 lines (69 loc) · 2.35 KB
/
public.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
package user
import (
"fmt"
"github.com/Sach97/ninshoo/model"
)
//TODO: strategy pattern or something if the user doesnt want to use deeplinks but just an email
//Signup register a user in db and send an email and return instructions
func (u *Service) Signup(credentials *model.UserCredentials) string {
user := &model.User{
Email: credentials.Email,
Password: credentials.Password,
}
userExists := u.userEmailExists(user.Email)
if userExists && !user.Confirmed {
return fmt.Sprintf("Sorry this email already exists. You must login or confirm your email address")
}
newUser, err := u.createUser(user)
if err != nil {
u.log.Errorf("Error during user creation : %v", err)
return fmt.Sprintf("Sorry an error occured please try again")
}
err = u.sendConfirmationEmail(newUser)
if err != nil {
u.log.Errorf("Error during sending email : %v", err)
return fmt.Sprintf("Sorry an error occured please try again")
}
return fmt.Sprintf("We've just sent you a confirmation email to %s", user.Email)
}
//Login return a jwt token if user is confirmed
func (u *Service) Login(credentials *model.UserCredentials) (string, error) {
//TODO: builder input
userExists := u.userEmailExists(credentials.Email)
if !userExists {
return "", u.msg.CredentialsError()
}
user, err := u.comparePassword(credentials)
if err != nil {
return "", err
}
if !user.Confirmed {
return "", fmt.Errorf("We've sent you an email to %s please click on the click in order to complete your registration", user.Email)
}
token, err := u.signJWT(user) //TODO: same builder input here
if err != nil {
u.log.Errorf("Error during jwt signing of user: %v", err)
}
return token, nil
}
//VerifyUserToken decode userid from token and verify if exists
func (u *Service) VerifyUserToken(token string) (bool, error) {
userID, err := u.tokenizer.GetUserID(token)
if err != nil {
u.log.Errorf("Error in retrieving userid for token %s: %v", token, err)
return false, fmt.Errorf("Your session has expired. ")
}
//TODO: handle session expired
userExists := u.userIDExists(userID)
if !userExists {
return false, fmt.Errorf("This user doesnt exists")
}
return u.confirmUser(userID)
}
func (u *Service) FindByID(userID string) (*model.User, error) {
userExists := u.userIDExists(userID)
if !userExists {
return nil, fmt.Errorf("This user doesnt exist")
}
return u.findByID(userID)
}