-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.go
46 lines (37 loc) · 1.1 KB
/
api.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
package api
import (
"math/rand"
"strings"
"time"
"github.com/cybersamx/go-recipes/fake-mock/model"
)
// AccountService represents a service that provides functions for managing user accounts
// in an application.
type AccountService struct {
accountModel model.AccountModel
}
// NewAccountService creates an instance of AccountService.
func NewAccountService(accountModel model.AccountModel) AccountService {
return AccountService{
accountModel: accountModel,
}
}
// ForgotPassword resets the password of the user associated with the passed email by
// replacing the existing password with a randomly generated password.
func (as *AccountService) ForgotPassword(email string) (string, error) {
pwd := newRandomPassword()
err := as.accountModel.UpdateAccount(email, pwd)
if err != nil {
return "", err
}
return pwd, nil
}
func newRandomPassword() string {
pool := []rune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.")
rand.Seed(time.Now().Unix())
var buffer strings.Builder
for i := 0; i < 8; i++ {
buffer.WriteRune(pool[rand.Intn(len(pool))])
}
return buffer.String()
}