mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-17 13:24:56 +00:00
Multitenant users api (#75)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/auth"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/users/proto"
|
||||
@@ -12,6 +13,10 @@ import (
|
||||
|
||||
// Login using email and password returns the users profile and a token
|
||||
func (u *Users) Login(ctx context.Context, req *pb.LoginRequest, rsp *pb.LoginResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.Email) == 0 {
|
||||
return ErrMissingEmail
|
||||
@@ -20,7 +25,13 @@ func (u *Users) Login(ctx context.Context, req *pb.LoginRequest, rsp *pb.LoginRe
|
||||
return ErrInvalidPassword
|
||||
}
|
||||
|
||||
return u.DB.Transaction(func(tx *gorm.DB) error {
|
||||
db, err := u.getDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
|
||||
return db.Transaction(func(tx *gorm.DB) error {
|
||||
// lookup the user
|
||||
var user User
|
||||
if err := tx.Where(&User{Email: req.Email}).First(&user).Error; err == gorm.ErrRecordNotFound {
|
||||
|
||||
Reference in New Issue
Block a user