Files
whatsapp-bot/handlers/auth.go
SekiDesu0 e256fcb073 modified: .env
new file:   __debug_bin.exe
	modified:   bot.db
	modified:   db/db.go
	modified:   go.mod
	new file:   handlers/auth.go
	modified:   handlers/dashboard.go
	new file:   handlers/saas.go
	modified:   handlers/webhook.go
	modified:   main.go
	new file:   saas_bot.db
	modified:   services/openrouter.go
	new file:   services/types.go
	modified:   services/whatsapp.go
	new file:   static/style.css
	modified:   templates/dashboard.html
	new file:   templates/landing.html
	new file:   templates/login.html
	new file:   templates/register.html
	deleted:    types/types.go
2026-03-02 00:38:05 -03:00

62 lines
1.6 KiB
Go

package handlers
import (
"fmt"
"whatsapp-bot/db"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
)
// Show Pages
func ShowLogin(c *gin.Context) { c.HTML(200, "login.html", nil) }
func ShowRegister(c *gin.Context) { c.HTML(200, "register.html", nil) }
func ShowLanding(c *gin.Context) { c.HTML(200, "landing.html", nil) }
// REGISTER
func RegisterHandler(c *gin.Context) {
email := c.PostForm("email")
pass := c.PostForm("password")
// Hash Password
hashed, _ := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
// Create User in DB
res, err := db.Conn.Exec("INSERT INTO users (email, password_hash) VALUES (?, ?)", email, string(hashed))
if err != nil {
c.HTML(400, "register.html", gin.H{"Error": "Email already taken"})
return
}
// Create Default Bot Config for new user
userID, _ := res.LastInsertId()
db.Conn.Exec("INSERT INTO bot_configs (user_id) VALUES (?)", userID)
c.Redirect(302, "/login")
}
// LOGIN
func LoginHandler(c *gin.Context) {
email := c.PostForm("email")
pass := c.PostForm("password")
var id int
var hash string
err := db.Conn.QueryRow("SELECT id, password_hash FROM users WHERE email=?", email).Scan(&id, &hash)
if err != nil || bcrypt.CompareHashAndPassword([]byte(hash), []byte(pass)) != nil {
c.HTML(401, "login.html", gin.H{"Error": "Invalid credentials"})
return
}
// Set simple cookie for session (In production use a secure session library)
c.SetCookie("user_id", fmt.Sprintf("%d", id), 3600*24, "/", "", false, true)
c.Redirect(302, "/dashboard")
}
// LOGOUT
func LogoutHandler(c *gin.Context) {
c.SetCookie("user_id", "", -1, "/", "", false, true)
c.Redirect(302, "/")
}