-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: add empty checks for some routes
- Loading branch information
1 parent
7548146
commit f205f4f
Showing
5 changed files
with
90 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,70 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
"golang.org/x/crypto/bcrypt" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"monify/internal/middlewares" | ||
monify "monify/protobuf/gen/go" | ||
) | ||
|
||
func emailExists(ctx context.Context, email string, db *sql.DB) (bool, error) { | ||
rows, err := db.QueryContext(ctx, ` | ||
SELECT user_id | ||
FROM email_login | ||
WHERE email = $1 | ||
`, email) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer rows.Close() | ||
return rows.Next(), nil | ||
} | ||
|
||
func CreateUser(ctx context.Context, db *sql.DB, email string, password string) (uuid.UUID, error) { | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return [16]byte{}, err | ||
} | ||
|
||
userId := uuid.New() | ||
_, err = db.ExecContext(ctx, ` | ||
INSERT INTO user_identity (user_id) VALUES ($1) | ||
`, userId) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
_, err = db.ExecContext(ctx, `INSERT INTO email_login(email, user_id, password) VALUES ($1, $2, $3)`, email, userId, string(hashedPassword)) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
return userId, nil | ||
} | ||
|
||
func (s Service) EmailRegister(ctx context.Context, req *monify.EmailRegisterRequest) (*monify.EmailRegisterResponse, error) { | ||
db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB) | ||
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) | ||
exists, err := emailExists(ctx, req.Email, db) | ||
if err != nil { | ||
logger.Error("failed to query email", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "Internal err.") | ||
} | ||
if exists { | ||
return nil, status.Error(codes.AlreadyExists, "Email already exists.") | ||
} | ||
|
||
userId, err := CreateUser(ctx, db, req.Email, req.Password) | ||
if err != nil { | ||
logger.Error("failed to create user", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "Internal err.") | ||
} | ||
return &monify.EmailRegisterResponse{UserId: userId.String()}, nil | ||
} | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
"golang.org/x/crypto/bcrypt" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"monify/internal/middlewares" | ||
monify "monify/protobuf/gen/go" | ||
) | ||
|
||
func emailExists(ctx context.Context, email string, db *sql.DB) (bool, error) { | ||
rows, err := db.QueryContext(ctx, ` | ||
SELECT user_id | ||
FROM email_login | ||
WHERE email = $1 | ||
`, email) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer rows.Close() | ||
return rows.Next(), nil | ||
} | ||
|
||
func CreateUser(ctx context.Context, db *sql.DB, email string, password string) (uuid.UUID, error) { | ||
if email == "" || password == "" { | ||
return uuid.Nil, status.Error(codes.InvalidArgument, "Email and password is required.") | ||
} | ||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) | ||
if err != nil { | ||
return [16]byte{}, err | ||
} | ||
|
||
userId := uuid.New() | ||
_, err = db.ExecContext(ctx, ` | ||
INSERT INTO user_identity (user_id) VALUES ($1) | ||
`, userId) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
|
||
_, err = db.ExecContext(ctx, `INSERT INTO email_login(email, user_id, password) VALUES ($1, $2, $3)`, email, userId, string(hashedPassword)) | ||
if err != nil { | ||
return uuid.Nil, err | ||
} | ||
return userId, nil | ||
} | ||
|
||
func (s Service) EmailRegister(ctx context.Context, req *monify.EmailRegisterRequest) (*monify.EmailRegisterResponse, error) { | ||
db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB) | ||
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger) | ||
exists, err := emailExists(ctx, req.Email, db) | ||
if err != nil { | ||
logger.Error("failed to query email", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "Internal err.") | ||
} | ||
if exists { | ||
return nil, status.Error(codes.AlreadyExists, "Email already exists.") | ||
} | ||
|
||
userId, err := CreateUser(ctx, db, req.Email, req.Password) | ||
if err != nil { | ||
logger.Error("failed to create user", zap.Error(err)) | ||
return nil, status.Error(codes.Internal, "Internal err.") | ||
} | ||
return &monify.EmailRegisterResponse{UserId: userId.String()}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters