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.

85 lines
1.5 KiB

2 years ago
package db
import (
"context"
"log"
2 years ago
"net"
"strconv"
2 years ago
2 years ago
"git.slaventius.ru/test3k/auth/internal/config"
2 years ago
2 years ago
api "git.slaventius.ru/test3k/umate/pkg/api"
2 years ago
"google.golang.org/grpc"
2 years ago
"google.golang.org/grpc/credentials/insecure"
2 years ago
)
type AuthDBClient struct {
config *config.Config
client *grpc.ClientConn
db api.AuthDBClient
ctx context.Context
}
func NewDBClient(ctx context.Context, config *config.Config) *AuthDBClient {
2 years ago
conStr := net.JoinHostPort(config.Db.Host, strconv.Itoa(config.Db.Port))
2 years ago
// client, err := grpc.Dial(conStr, grpc.WithInsecure())
client, err := grpc.Dial(conStr, grpc.WithTransportCredentials(insecure.NewCredentials()))
2 years ago
if err != nil {
log.Fatal(err)
}
return &AuthDBClient{
config: config,
client: client,
db: api.NewAuthDBClient(client),
ctx: ctx,
}
}
2 years ago
func (s *AuthDBClient) Close() error {
return s.client.Close()
2 years ago
}
func (s *AuthDBClient) Login(uid string, password string) error {
_, err := s.db.Login(s.ctx, &api.LoginRequest{
Login: uid,
Password: password,
})
//
if err != nil {
return err
}
2 years ago
return nil
2 years ago
}
2 years ago
func (s *AuthDBClient) Registration(uid string, email string, password string) (string, error) {
2 years ago
res, err := s.db.Registration(s.ctx, &api.RegistrationRequest{
2 years ago
Login: uid,
Password: password,
Email: email,
2 years ago
})
//
if err != nil {
return "", err
}
2 years ago
return res.GetCode(), nil
2 years ago
}
func (s *AuthDBClient) Confirmation(code string) error {
_, err := s.db.Confirmation(s.ctx, &api.ConfirmationRequest{
Code: code,
})
//
if err != nil {
return err
}
2 years ago
return nil
2 years ago
}