148 lines
2.5 KiB
Go
148 lines
2.5 KiB
Go
package controllers
|
|
|
|
import(
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"example.com/gin/test/models"
|
|
"example.com/gin/test/repositories"
|
|
"example.com/gin/test/services"
|
|
)
|
|
|
|
|
|
type UserController struct {}
|
|
|
|
func NewUserController() UserController {
|
|
return UserController{}
|
|
}
|
|
|
|
|
|
func (uc *UserController) Register(c *gin.Context) {
|
|
//Get the email/passwd off req body
|
|
var body struct {
|
|
Name string
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
err := c.Bind(&body)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Failed to read body",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
_, err = services.Users.Register(body.Name, body.Email, body.Password)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error: ", err)
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Failed to create user",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
|
|
//respond
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
}
|
|
|
|
|
|
func (uc *UserController) Login(c *gin.Context) {
|
|
//Get the email/passwd off req body
|
|
var body struct {
|
|
Email string
|
|
Password string
|
|
}
|
|
|
|
err := c.Bind(&body)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Login Failed",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
tokenString, err := services.Users.Login(body.Email, body.Password)
|
|
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "Login Failed",
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
// send it back
|
|
c.SetSameSite(http.SameSiteLaxMode)
|
|
c.SetCookie("Authorization", tokenString, 3600 * 24, "", "", false, true)
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
}
|
|
|
|
func (uc *UserController) Validate(c *gin.Context) {
|
|
user, exists := c.Get("user")
|
|
|
|
if exists {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": fmt.Sprintf("Logged in with userID: %d", user.(models.User).ID),
|
|
})
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Currently not logged in.",
|
|
})
|
|
}
|
|
}
|
|
|
|
func (rc *UserController) LoginView(c *gin.Context) {
|
|
rooms, _ := repositories.Rooms.GetAll()
|
|
|
|
data := gin.H{
|
|
"title": "Room Page",
|
|
"rooms": rooms,
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "login.html", data)
|
|
}
|
|
|
|
func (rc *UserController) RegisterView(c *gin.Context) {
|
|
rooms, _ := repositories.Rooms.GetAll()
|
|
|
|
data := gin.H{
|
|
"title": "Room Page",
|
|
"rooms": rooms,
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "register.html", data)
|
|
}
|
|
|
|
func (rc *UserController) ResetView(c *gin.Context) {
|
|
rooms, _ := repositories.Rooms.GetAll()
|
|
|
|
data := gin.H{
|
|
"title": "Room Page",
|
|
"rooms": rooms,
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "passwordreset.html", data)
|
|
}
|
|
|
|
func (rc *UserController) MainView(c *gin.Context) {
|
|
rooms, _ := repositories.Rooms.GetAll()
|
|
|
|
data := gin.H{
|
|
"title": "Room Page",
|
|
"rooms": rooms,
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "index.html", data)
|
|
}
|
|
|