Add an API to reset the password
This commit is contained in:
@ -4,24 +4,34 @@ import (
|
||||
"context"
|
||||
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
|
||||
"git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/accounts"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
)
|
||||
|
||||
func NewAccountRPCImpl(contoller ctrl.Manager, hashCost int16) *AccountsServer {
|
||||
func NewAccountRPCImpl(contoller ctrl.Manager, hashCost int16, email *email.EmailConf, devMode bool) *AccountsServer {
|
||||
return &AccountsServer{
|
||||
Controller: contoller,
|
||||
Params: &controllers.AccountParams{
|
||||
HashCost: hashCost,
|
||||
},
|
||||
emailConfig: *email,
|
||||
devMode: devMode,
|
||||
}
|
||||
}
|
||||
|
||||
type AccountsServer struct {
|
||||
accounts.UnimplementedAccountsServer
|
||||
Controller ctrl.Manager
|
||||
Params *controllers.AccountParams
|
||||
Controller ctrl.Manager
|
||||
Params *controllers.AccountParams
|
||||
emailConfig email.EmailConf
|
||||
// When dev mode is enabled, email won't be sent, instead the code will be returned in metadata
|
||||
devMode bool
|
||||
}
|
||||
|
||||
func (a *AccountsServer) SignUp(ctx context.Context, in *accounts.AccountWithPassword) (*accounts.AccountFullWithToken, error) {
|
||||
@ -64,6 +74,24 @@ func (a *AccountsServer) SignIn(ctx context.Context, in *accounts.AccountWithPas
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *AccountsServer) ResetPassword(ctx context.Context, in *accounts.AccountData) (*empty.Empty, error) {
|
||||
data := populateData(in.GetName(), "", in.GetEmail())
|
||||
acc := populateAccount(data, a.Controller)
|
||||
code, err := acc.ResetPassword(ctx, a.emailConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if a.devMode {
|
||||
header := metadata.Pairs("code", code)
|
||||
if err := grpc.SendHeader(ctx, header); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func populateData(username, password, email string) *controllers.AccountData {
|
||||
return &controllers.AccountData{
|
||||
Username: username,
|
||||
|
Reference in New Issue
Block a user