252 lines
6.5 KiB
Go
252 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/gorilla/mux"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type User struct {
|
|
ID string `json:"id" db:"id"`
|
|
Email string `json:"email" db:"email"`
|
|
Name string `json:"name" db:"name"`
|
|
Avatar *string `json:"avatar" db:"avatar_url"`
|
|
Balance float64 `json:"balance" db:"balance"`
|
|
Verified bool `json:"verified" db:"verified"`
|
|
}
|
|
|
|
type LoginRequest struct {
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type RegisterRequest struct {
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
type AuthResponse struct {
|
|
Token string `json:"token"`
|
|
User User `json:"user"`
|
|
}
|
|
|
|
type AuthService struct {
|
|
db *pgxpool.Pool
|
|
jwtSecret []byte
|
|
}
|
|
|
|
func NewAuthService(db *pgxpool.Pool, jwtSecret string) *AuthService {
|
|
return &AuthService{
|
|
db: db,
|
|
jwtSecret: []byte(jwtSecret),
|
|
}
|
|
}
|
|
|
|
func (s *AuthService) generateToken(userID string) (string, error) {
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
"user_id": userID,
|
|
"exp": time.Now().Add(time.Hour * 24).Unix(),
|
|
})
|
|
|
|
return token.SignedString(s.jwtSecret)
|
|
}
|
|
|
|
func (s *AuthService) hashPassword(password string) (string, error) {
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
|
return string(bytes), err
|
|
}
|
|
|
|
func (s *AuthService) checkPassword(password, hash string) bool {
|
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
return err == nil
|
|
}
|
|
|
|
func (s *AuthService) login(w http.ResponseWriter, r *http.Request) {
|
|
var req LoginRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var user User
|
|
var passwordHash string
|
|
err := s.db.QueryRow(context.Background(),
|
|
"SELECT id, email, name, avatar_url, balance, verified, password_hash FROM users WHERE email = $1",
|
|
req.Email).Scan(&user.ID, &user.Email, &user.Name, &user.Avatar, &user.Balance, &user.Verified, &passwordHash)
|
|
|
|
if err != nil {
|
|
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
if !s.checkPassword(req.Password, passwordHash) {
|
|
http.Error(w, "Invalid credentials", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
token, err := s.generateToken(user.ID)
|
|
if err != nil {
|
|
http.Error(w, "Failed to generate token", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := AuthResponse{
|
|
Token: token,
|
|
User: user,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *AuthService) register(w http.ResponseWriter, r *http.Request) {
|
|
var req RegisterRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Check if user already exists
|
|
var exists bool
|
|
err := s.db.QueryRow(context.Background(),
|
|
"SELECT EXISTS(SELECT 1 FROM users WHERE email = $1)", req.Email).Scan(&exists)
|
|
if err != nil {
|
|
http.Error(w, "Database error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if exists {
|
|
http.Error(w, "User already exists", http.StatusConflict)
|
|
return
|
|
}
|
|
|
|
// Hash password
|
|
hashedPassword, err := s.hashPassword(req.Password)
|
|
if err != nil {
|
|
http.Error(w, "Failed to hash password", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Create user
|
|
var user User
|
|
err = s.db.QueryRow(context.Background(),
|
|
`INSERT INTO users (email, password_hash, name, balance, verified)
|
|
VALUES ($1, $2, $3, 0.00, false)
|
|
RETURNING id, email, name, avatar_url, balance, verified`,
|
|
req.Email, hashedPassword, req.Name).Scan(
|
|
&user.ID, &user.Email, &user.Name, &user.Avatar, &user.Balance, &user.Verified)
|
|
|
|
if err != nil {
|
|
http.Error(w, "Failed to create user", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
token, err := s.generateToken(user.ID)
|
|
if err != nil {
|
|
http.Error(w, "Failed to generate token", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := AuthResponse{
|
|
Token: token,
|
|
User: user,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
func (s *AuthService) profile(w http.ResponseWriter, r *http.Request) {
|
|
userID := r.Header.Get("X-User-ID")
|
|
if userID == "" {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
var user User
|
|
err := s.db.QueryRow(context.Background(),
|
|
"SELECT id, email, name, avatar_url, balance, verified FROM users WHERE id = $1",
|
|
userID).Scan(&user.ID, &user.Email, &user.Name, &user.Avatar, &user.Balance, &user.Verified)
|
|
|
|
if err != nil {
|
|
http.Error(w, "User not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(user)
|
|
}
|
|
|
|
func (s *AuthService) health(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "healthy"})
|
|
}
|
|
|
|
func main() {
|
|
// Database connection
|
|
databaseURL := os.Getenv("DATABASE_URL")
|
|
if databaseURL == "" {
|
|
databaseURL = "postgresql://dantegpu:dantegpu123@localhost:5432/dantegpu"
|
|
}
|
|
|
|
db, err := pgxpool.New(context.Background(), databaseURL)
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to database:", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
// Test database connection
|
|
if err := db.Ping(context.Background()); err != nil {
|
|
log.Fatal("Failed to ping database:", err)
|
|
}
|
|
|
|
jwtSecret := os.Getenv("JWT_SECRET")
|
|
if jwtSecret == "" {
|
|
jwtSecret = "your_super_secret_jwt_key_here_make_it_long_and_random"
|
|
}
|
|
|
|
authService := NewAuthService(db, jwtSecret)
|
|
|
|
r := mux.NewRouter()
|
|
|
|
// CORS middleware
|
|
r.Use(func(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", "GET, POST, PUT, DELETE, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-User-ID")
|
|
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
})
|
|
|
|
// Routes
|
|
r.HandleFunc("/health", authService.health).Methods("GET")
|
|
r.HandleFunc("/login", authService.login).Methods("POST")
|
|
r.HandleFunc("/register", authService.register).Methods("POST")
|
|
r.HandleFunc("/profile", authService.profile).Methods("GET")
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8090"
|
|
}
|
|
|
|
fmt.Printf("Auth service starting on port %s\n", port)
|
|
log.Fatal(http.ListenAndServe(":"+port, r))
|
|
}
|