Skip to content

Commit

Permalink
feat: Added registration
Browse files Browse the repository at this point in the history
  • Loading branch information
sjewyr committed May 22, 2024
1 parent 281272f commit 561fec5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package common

import (
"context"
Expand All @@ -9,7 +9,7 @@ import (
"google.golang.org/grpc"
)

func main() {
func Register(username string, password string) {
conn, err := grpc.Dial("luna:9001", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
Expand All @@ -20,8 +20,8 @@ func main() {

ctx := context.Background()
userCredentials := &proto.UserCredentials{
Username: "user2",
Password: "password",
Username: username,
Password: password,
}

_, err = client.RegisterNewUser(ctx, userCredentials)
Expand Down
27 changes: 23 additions & 4 deletions client/src/handle_front/handle_front.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ func CORSHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")

w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, Access-Control-Allow-Origin")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}

next.ServeHTTP(w, r)
})
}

type Message struct {
Message string `json:"message"`
}
type UserCreds struct {
Login string `json:"login"`
Password string `json:"password"`
}

func sendMessageHandler(w http.ResponseWriter, r *http.Request) {
var text Message
Expand All @@ -41,11 +44,27 @@ func sendMessageHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}
func registerHandler(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)
common.Register(username, password)

// Ответ клиенту
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/send", sendMessageHandler)

mux.HandleFunc("/register", registerHandler)
// Добавляем CORSHandler
handler := CORSHandler(mux)

Expand Down
27 changes: 26 additions & 1 deletion frontend/scripts/auth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
const signUpButton = document.getElementById('signUp');
const signInButton = document.getElementById('signIn');
const container = document.getElementById('container');

const registerButton = document.getElementById('register');
signUpButton.addEventListener('click', () => {
container.classList.add("right-panel-active");
});

signInButton.addEventListener('click', () => {
container.classList.remove("right-panel-active");
});

registerButton.addEventListener('click', () => {
var loginInput = document.getElementById("register_login");
var login = loginInput.value;
var passwordInput = document.getElementById("register_password");
var password = passwordInput.value;

if (login.trim() !== "" && password.trim() !== "") {
fetch("http://localhost:8080/register", {
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);
messageInput.value = ""; // Clear the input field
})
.catch(error => {
console.error("Error sending message:", error);
});
}
});
6 changes: 3 additions & 3 deletions frontend/templates/auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ <h2>Команда БАДИБИЛДИНГ</h2>
<div class="form-container sign-up-container">
<form action="#">
<h1>Создать аккаунт</h1>
<input type="text" placeholder="Логин" />
<input type="password" placeholder="Пароль" />
<button style="border: none; cursor: pointer;">Зарегистрироваться</button>
<input type="text" placeholder="Логин" id='register_login'/>
<input type="password" placeholder="Пароль" id='register_password'/>
<button style="border: none; cursor: pointer;" id = "register">Зарегистрироваться</button>
</form>
</div>
<div class="form-container sign-in-container">
Expand Down

0 comments on commit 561fec5

Please sign in to comment.