123 lines
3.5 KiB
Go
123 lines
3.5 KiB
Go
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"golang.org/x/crypto/bcrypt"
|
||
)
|
||
|
||
const (
|
||
nocoURL = "https://nocodb.shaiheprjct.ru/api/v2/tables/mqvqd88xe01rxap/records"
|
||
apiToken = "eVqxLwbUQRLQHQ3g44js9vfLHvIdWhllLZwKfW25"
|
||
)
|
||
// Простая структура пользователя
|
||
type User struct {
|
||
Username string `json:"username"`
|
||
Password string `json:"password"`
|
||
}
|
||
|
||
func main() {
|
||
http.HandleFunc("/", homePage)
|
||
http.HandleFunc("/register", registerHandler)
|
||
http.HandleFunc("/login", loginHandler)
|
||
|
||
fmt.Println("Сервер запущен на http://localhost:8080")
|
||
http.ListenAndServe(":8080", nil)
|
||
}
|
||
|
||
// Хэширование пароля
|
||
func hashPassword(password string) (string, error) {
|
||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||
return string(bytes), err
|
||
}
|
||
|
||
// Регистрация (отправка в NocoDB)
|
||
func registerHandler(w http.ResponseWriter, r *http.Request) {
|
||
if r.Method == "POST" {
|
||
user := r.FormValue("username")
|
||
pass, _ := hashPassword(r.FormValue("password"))
|
||
|
||
jsonData, _ := json.Marshal(map[string]string{
|
||
"username": user,
|
||
"password": pass,
|
||
})
|
||
|
||
req, _ := http.NewRequest("POST", nocoURL, bytes.NewBuffer(jsonData))
|
||
req.Header.Set("xc-token", apiToken)
|
||
req.Header.Set("Content-Type", "application/json")
|
||
|
||
client := &http.Client{}
|
||
resp, _ := client.Do(req)
|
||
defer resp.Body.Close()
|
||
|
||
if resp.StatusCode == 200 || resp.StatusCode == 201 {
|
||
fmt.Fprint(w, "Регистрация успешна! Теперь можно войти.")
|
||
} else {
|
||
fmt.Fprint(w, "Ошибка регистрации. Возможно, имя занято.")
|
||
}
|
||
}
|
||
}
|
||
|
||
// Вход (проверка через NocoDB)
|
||
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||
if r.Method == "POST" {
|
||
user := r.FormValue("username")
|
||
pass := r.FormValue("password")
|
||
|
||
// Поиск пользователя в NocoDB по username
|
||
searchURL := fmt.Sprintf("%s?where=(username,eq,%s)", nocoURL, user)
|
||
req, _ := http.NewRequest("GET", searchURL, nil)
|
||
req.Header.Set("xc-token", apiToken)
|
||
|
||
client := &http.Client{}
|
||
resp, _ := client.Do(req)
|
||
defer resp.Body.Close()
|
||
|
||
body, _ := io.ReadAll(resp.Body)
|
||
var result struct {
|
||
List []User `json:"list"`
|
||
}
|
||
json.Unmarshal(body, &result)
|
||
|
||
if len(result.List) > 0 {
|
||
err := bcrypt.CompareHashAndPassword([]byte(result.List[0].Password), []byte(pass))
|
||
if err == nil {
|
||
fmt.Fprintf(w, "Добро пожаловать в личный кабинет, %s!", user)
|
||
return
|
||
}
|
||
}
|
||
fmt.Fprint(w, "Неверный логин или пароль.")
|
||
}
|
||
}
|
||
|
||
|
||
func homePage(w http.ResponseWriter, r *http.Request) {
|
||
// Эта строка заставит браузер отобразить форму, а не текст кода
|
||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||
|
||
fmt.Fprint(w, `
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<head><title>Личный кабинет</title></head>
|
||
<body>
|
||
<h2>Регистрация</h2>
|
||
<form action="/register" method="POST">
|
||
<input name="username" placeholder="Логин" required>
|
||
<input name="password" type="password" placeholder="Пароль" required>
|
||
<button type="submit">Создать аккаунт</button>
|
||
</form>
|
||
<hr>
|
||
<h2>Вход</h2>
|
||
<form action="/login" method="POST">
|
||
<input name="username" placeholder="Логин" required>
|
||
<input name="password" type="password" placeholder="Пароль" required>
|
||
<button type="submit">Войти</button>
|
||
</form>
|
||
</body>
|
||
</html>
|
||
`)
|
||
}
|