Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password login #1

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
SUB_DIRS = protobuf migrations
SUB_DIRS = protobuf
PACKAGES ?= $(shell go list ./...)

all: $(SUB_DIRS)

$(SUB_DIRS):
make -C $@

migrations:
make -C migrations

test:
-mkdir build
go test $(PACKAGES) -v -cover -failfast
Expand Down
17 changes: 13 additions & 4 deletions internal/services/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ package auth
import (
"context"
"database/sql"
"errors"
"github.com/golang-jwt/jwt/v5"
"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"
"time"
)

func findEmailUser(ctx context.Context, email string, db *sql.DB) (uuid.UUID, error) {
func matchEmailUser(ctx context.Context, email string, password string, db *sql.DB) (uuid.UUID, error) {
query, err := db.QueryContext(ctx, `
SELECT user_id
SELECT user_id, password
FROM email_login
WHERE email = $1`, email)
defer query.Close()
Expand All @@ -28,10 +30,17 @@ func findEmailUser(ctx context.Context, email string, db *sql.DB) (uuid.UUID, er
}

var userId uuid.UUID
err = query.Scan(&userId)
var hashedPassword string
err = query.Scan(&userId, &hashedPassword)
if err != nil {
return uuid.Nil, err
}

_ = bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
if err != nil {
return uuid.Nil, errors.New("incorrect password")
}

return userId, nil
}

Expand All @@ -55,7 +64,7 @@ func (s Service) EmailLogin(ctx context.Context, req *monify.EmailLoginRequest)
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger)
db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB)

userId, err := findEmailUser(ctx, req.Email, db)
userId, err := matchEmailUser(ctx, req.Email, req.Password, db)
if err != nil {
logger.Error("", zap.Error(err))
return nil, status.Errorf(codes.Internal, "internal err.")
Expand Down
15 changes: 11 additions & 4 deletions internal/services/auth/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"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"
Expand All @@ -24,15 +25,21 @@ func emailExists(ctx context.Context, email string, db *sql.DB) (bool, error) {
return rows.Next(), nil
}

func createUser(ctx context.Context, db *sql.DB, email string) (uuid.UUID, error) {
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, `
_, 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) VALUES ($1, $2)`, email, userId)

_, 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
}
Expand All @@ -51,7 +58,7 @@ func (s Service) EmailRegister(ctx context.Context, req *monify.EmailRegisterReq
return nil, status.Error(codes.AlreadyExists, "Email already exists.")
}

userId, err := createUser(ctx, db, req.Email)
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.")
Expand Down
10 changes: 10 additions & 0 deletions internal/services/group/list_joined.go
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
package group

import (
"context"
monify "monify/protobuf"
)

func (g Service) ListJoinedGroups(context.Context, *monify.Empty) (*monify.ListJoinedGroupsResponse, error) {
panic("")

}
3 changes: 0 additions & 3 deletions migrations/2405190114_add_group_invite_code.down.sql

This file was deleted.

3 changes: 0 additions & 3 deletions migrations/2405190114_add_group_invite_code.up.sql

This file was deleted.

2 changes: 2 additions & 0 deletions migrations/2405200834_add_password.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE email_login DROP COLUMN password;

1 change: 1 addition & 0 deletions migrations/2405200834_add_password.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE email_login ADD COLUMN password CHAR(255) NOT NULL;
1 change: 0 additions & 1 deletion protobuf/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

MODULES = $(wildcard *.proto)

OUTPUT = $(MODULES:%.proto=%.pb.go)
Expand Down
76 changes: 48 additions & 28 deletions protobuf/auth.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions protobuf/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ service AuthService{

message EmailLoginRequest{
string email = 1;
string password = 2;
}


Expand All @@ -22,6 +23,7 @@ message EmailLoginResponse{

message EmailRegisterRequest{
string email = 1;
string password = 2;
}

message EmailRegisterResponse{
Expand Down
Loading