From b10b9843233c42f5b2b5dfdafb2af1e2f7f6919c Mon Sep 17 00:00:00 2001 From: ddlifter Date: Wed, 22 May 2024 22:18:17 +0300 Subject: [PATCH] feat(frontend): Implement opening chat page after auth, feat(client): Implement full pipeline of register, login, send message --- client/src/common/register.go | 33 ------- client/src/common/send_message.go | 39 --------- client/src/create_room/create_room.go | 8 +- client/src/handle_front/handle_front.go | 26 ++++-- client/src/login/login.go | 35 -------- client/src/main.go | 10 +++ client/src/send_message/send_message.go | 110 ++++++++++++++++++++++++ frontend/scripts/auth.js | 33 ++++++- frontend/scripts/signin.js | 0 frontend/scripts/signup.js | 0 frontend/templates/auth.html | 6 +- frontend/templates/signin.html | 71 --------------- frontend/templates/signup.html | 0 13 files changed, 179 insertions(+), 192 deletions(-) delete mode 100644 client/src/common/register.go delete mode 100644 client/src/common/send_message.go delete mode 100644 client/src/login/login.go create mode 100644 client/src/main.go create mode 100644 client/src/send_message/send_message.go delete mode 100644 frontend/scripts/signin.js delete mode 100644 frontend/scripts/signup.js delete mode 100644 frontend/templates/signin.html delete mode 100644 frontend/templates/signup.html diff --git a/client/src/common/register.go b/client/src/common/register.go deleted file mode 100644 index ce2c864..0000000 --- a/client/src/common/register.go +++ /dev/null @@ -1,33 +0,0 @@ -package common - -import ( - "context" - "log" - - proto "bb-hackathon/tcp-chat.git/proto" - - "google.golang.org/grpc" -) - -func Register(username string, password string) { - conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) - if err != nil { - log.Fatalf("did not connect: %v", err) - } - defer conn.Close() - - client := proto.NewRegistryClient(conn) - - ctx := context.Background() - userCredentials := &proto.UserCredentials{ - Username: username, - Password: password, - } - - _, err = client.RegisterNewUser(ctx, userCredentials) - if err != nil { - log.Fatalf("could not register new user: %v", err) - } - - log.Printf("Registered new user successfully") -} diff --git a/client/src/common/send_message.go b/client/src/common/send_message.go deleted file mode 100644 index 179afba..0000000 --- a/client/src/common/send_message.go +++ /dev/null @@ -1,39 +0,0 @@ -package common - -import ( - "bb-hackathon/tcp-chat.git/proto" - "context" - "log" - - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" -) - -func SendMessage(t string, room string) { - conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) - if err != nil { - log.Fatalf("Failed to connect: %v", err) - } - defer conn.Close() - - client := proto.NewChatClient(conn) - - md := metadata.Pairs( - "user_uuid", "d9890202-a129-4044-8736-e7770fcae7f5", - "auth_token", "f94ee0212842c6434f019eb42bfcdc63", - ) - - ctx := metadata.NewOutgoingContext(context.Background(), md) - log.Println(t) - messageReq := &proto.ClientsideMessage{ - RoomUuid: &proto.UUID{Uuid: room}, - Text: t, - } - - _, err = client.SendMessage(ctx, messageReq) - if err != nil { - log.Fatalf("SendMessage failed: %v", err) - } else { - log.Println("Message sent successfully: %s", t) - } -} diff --git a/client/src/create_room/create_room.go b/client/src/create_room/create_room.go index cf512a3..5191344 100644 --- a/client/src/create_room/create_room.go +++ b/client/src/create_room/create_room.go @@ -21,14 +21,14 @@ func main() { client := proto.NewChatClient(conn) ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs( - "user_uuid", "d9890202-a129-4044-8736-e7770fcae7f5", - "auth_token", "f94ee0212842c6434f019eb42bfcdc63", + "user_uuid", "17c4ab93-0b16-4233-9936-9f1c89ab96d7", + "auth_token", "70ea0d70360f118b4de4dd7d2b40c543", )) roomReq := &proto.ClientsideRoom{ - Name: "Room2", + Name: "Room0", Members: []*proto.UUID{ - &proto.UUID{Uuid: "d9890202-a129-4044-8736-e7770fcae7f5"}, + &proto.UUID{Uuid: "17c4ab93-0b16-4233-9936-9f1c89ab96d7"}, }, } diff --git a/client/src/handle_front/handle_front.go b/client/src/handle_front/handle_front.go index 29d8969..1113ecd 100644 --- a/client/src/handle_front/handle_front.go +++ b/client/src/handle_front/handle_front.go @@ -1,7 +1,7 @@ package main import ( - "bb-hackathon/tcp-chat.git/src/common" + sendmessage "bb-hackathon/tcp-chat.git/src/send_message" "encoding/json" "fmt" "log" @@ -37,10 +37,8 @@ func sendMessageHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - fmt.Println(t) - common.SendMessage(t, "36537ef0-ee3e--aaa1-6547e466dd4a") + sendmessage.SendMessage(t, "8299ace8-e565-497a-868a-e48fde731fef") - // Ответ клиенту w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]string{"status": "success"}) } @@ -54,9 +52,24 @@ func registerHandler(w http.ResponseWriter, r *http.Request) { return } fmt.Println(username, password) - common.Register(username, password) + sendmessage.Register(username, password) + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{"status": "success"}) +} + +func loginHandler(w http.ResponseWriter, r *http.Request) { + var msg UserCreds + err := json.NewDecoder(r.Body).Decode(&msg) + username := msg.Login + password := msg.Password + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + fmt.Println(username, password) + sendmessage.Login(username, password) - // Ответ клиенту w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]string{"status": "success"}) } @@ -65,6 +78,7 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/send", sendMessageHandler) mux.HandleFunc("/register", registerHandler) + mux.HandleFunc("/login", loginHandler) // Добавляем CORSHandler handler := CORSHandler(mux) diff --git a/client/src/login/login.go b/client/src/login/login.go deleted file mode 100644 index 31249d3..0000000 --- a/client/src/login/login.go +++ /dev/null @@ -1,35 +0,0 @@ -package main - -import ( - "context" - "fmt" - "log" - - proto "bb-hackathon/tcp-chat.git/proto" - - "google.golang.org/grpc" -) - -func main() { - conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) - if err != nil { - log.Fatalf("did not connect: %v", err) - } - defer conn.Close() - - client := proto.NewRegistryClient(conn) - - ctx := context.Background() - userCredentials := &proto.UserCredentials{ - Username: "user2", - Password: "password", - } - - response, err := client.LoginAsUser(ctx, userCredentials) - if err != nil { - log.Fatalf("could not login: %v", err) - } - - fmt.Println(response) - log.Printf("Logged in successfully") -} diff --git a/client/src/main.go b/client/src/main.go new file mode 100644 index 0000000..c820080 --- /dev/null +++ b/client/src/main.go @@ -0,0 +1,10 @@ +package main + +import ( + sendmessage "bb-hackathon/tcp-chat.git/src/send_message" +) + +func main() { + sendmessage.Login("user1234", "1234") + sendmessage.SendMessage("message", "8299ace8-e565-497a-868a-e48fde731fef") +} diff --git a/client/src/send_message/send_message.go b/client/src/send_message/send_message.go new file mode 100644 index 0000000..38fb7bc --- /dev/null +++ b/client/src/send_message/send_message.go @@ -0,0 +1,110 @@ +package sendmessage + +import ( + "context" + "log" + "sync" + + proto "bb-hackathon/tcp-chat.git/proto" + + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" +) + +var ( + UserUUID string + AuthToken string + authDataMux sync.Mutex +) + +func setUserAuthData(uuid, token string) { + authDataMux.Lock() + defer authDataMux.Unlock() + UserUUID = uuid + AuthToken = token +} + +func getUserAuthData() (string, string) { + authDataMux.Lock() + defer authDataMux.Unlock() + return UserUUID, AuthToken +} + +func Register(username string, password string) { + conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + + client := proto.NewRegistryClient(conn) + + ctx := context.Background() + userCredentials := &proto.UserCredentials{ + Username: username, + Password: password, + } + + _, err = client.RegisterNewUser(ctx, userCredentials) + if err != nil { + log.Fatalf("could not register new user: %v", err) + } + + log.Printf("Registered new user successfully") +} + +func Login(username, password string) { + conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + + client := proto.NewRegistryClient(conn) + + ctx := context.Background() + userCredentials := &proto.UserCredentials{ + Username: username, + Password: password, + } + + response, err := client.LoginAsUser(ctx, userCredentials) + if err != nil { + log.Fatalf("could not login: %v", err) + } + + setUserAuthData(response.UserUuid.Uuid, response.Token.Token) + + log.Printf("Logged in successfully") +} + +func SendMessage(t string, room string) { + userUUID, authToken := getUserAuthData() + + conn, err := grpc.Dial("luna:9001", grpc.WithInsecure()) + if err != nil { + log.Fatalf("Failed to connect: %v", err) + } + defer conn.Close() + + client := proto.NewChatClient(conn) + + md := metadata.Pairs( + "user_uuid", userUUID, + "auth_token", authToken, + ) + + ctx := metadata.NewOutgoingContext(context.Background(), md) + log.Println(t) + messageReq := &proto.ClientsideMessage{ + RoomUuid: &proto.UUID{Uuid: room}, + Text: t, + } + + _, err = client.SendMessage(ctx, messageReq) + if err != nil { + log.Fatalf("SendMessage failed: %v", err) + } else { + log.Printf("Message sent successfully: %s", t) + } +} diff --git a/frontend/scripts/auth.js b/frontend/scripts/auth.js index f5e02f0..1effdda 100755 --- a/frontend/scripts/auth.js +++ b/frontend/scripts/auth.js @@ -2,6 +2,7 @@ const signUpButton = document.getElementById('signUp'); const signInButton = document.getElementById('signIn'); const container = document.getElementById('container'); const registerButton = document.getElementById('register'); +const loginButton = document.getElementById('login'); signUpButton.addEventListener('click', () => { container.classList.add("right-panel-active"); }); @@ -27,7 +28,37 @@ registerButton.addEventListener('click', () => { .then(response => response.json()) .then(data => { console.log("Message sent:", data); - messageInput.value = ""; // Clear the input field + loginInput.value = ""; + passwordInput.value = ""; + window.location.href = "index.html"; + + }) + .catch(error => { + console.error("Error sending message:", error); + }); + } +}); + +loginButton.addEventListener('click', () => { + var loginInput = document.getElementById("login_login"); + var login = loginInput.value; + var passwordInput = document.getElementById("login_password"); + var password = passwordInput.value; + + if (login.trim() !== "" && password.trim() !== "") { + fetch("http://localhost:8080/login", { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ login: login, password: password }) + }) + .then(response => response.json()) + .then(data => { + console.log("Message sent:", data); + loginInput.value = ""; + passwordInput.value = ""; + window.location.href = "index.html"; }) .catch(error => { console.error("Error sending message:", error); diff --git a/frontend/scripts/signin.js b/frontend/scripts/signin.js deleted file mode 100644 index e69de29..0000000 diff --git a/frontend/scripts/signup.js b/frontend/scripts/signup.js deleted file mode 100644 index e69de29..0000000 diff --git a/frontend/templates/auth.html b/frontend/templates/auth.html index dd0030e..6e8bf44 100755 --- a/frontend/templates/auth.html +++ b/frontend/templates/auth.html @@ -20,9 +20,9 @@

Создать аккаунт

Войти в аккаунт

- - - + + +
diff --git a/frontend/templates/signin.html b/frontend/templates/signin.html deleted file mode 100644 index c3109c1..0000000 --- a/frontend/templates/signin.html +++ /dev/null @@ -1,71 +0,0 @@ - - - - - CodePen - Weekly Coding Challenge #1 - Double slider Sign in/up Form - Desktop Only - - - - - -

Weekly Coding Challenge #1: Sign in/up Form

-
- - -
-
-
-

Welcome Back!

-

To keep connected with us please login with your personal info

- -
-
-

Hello, Friend!

-

Enter your personal details and start journey with us

- -
-
-
-
- -
-

- Created with by - Florin Pop - - Read how I created this and how you can join the challenge - here. -

-
- - - - - diff --git a/frontend/templates/signup.html b/frontend/templates/signup.html deleted file mode 100644 index e69de29..0000000