chore: user handle auth
This commit is contained in:
@@ -18,7 +18,7 @@ func NewUserController() UserController {
|
||||
|
||||
func CreateSessionData(c *gin.Context, extra any) gin.H {
|
||||
user, exists := c.Get("user")
|
||||
userImpl, _ := user.(openapi.UserDB)
|
||||
userImpl, _ := user.(openapi.UserResponse)
|
||||
|
||||
return gin.H{
|
||||
"loggedIn": exists,
|
||||
@@ -32,39 +32,6 @@ func (rc *UserController) Logout(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{})
|
||||
}
|
||||
|
||||
//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, false)
|
||||
//
|
||||
// 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 {
|
||||
@@ -95,33 +62,34 @@ func (uc *UserController) Login(c *gin.Context) {
|
||||
|
||||
// send it back
|
||||
c.SetSameSite(http.SameSiteLaxMode)
|
||||
c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*24, "", "", false, true)
|
||||
c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*1, "", "", false, true)
|
||||
c.JSON(http.StatusOK, gin.H{})
|
||||
}
|
||||
|
||||
func (rc *UserController) LoginHandler(c *gin.Context) {
|
||||
email := c.PostForm("username")
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
|
||||
tokenString, err := services.Users.Login(email, password)
|
||||
tokenString, err := services.Users.Login(username, password)
|
||||
|
||||
if err != nil {
|
||||
data := gin.H{
|
||||
"error": "Login Failed. Wrong Email or Password!",
|
||||
"error": "Login Failed. Wrong username or Password!",
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "login.html", data)
|
||||
return
|
||||
}
|
||||
|
||||
c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*24, "", "", false, true)
|
||||
|
||||
//set this so that CreateSessionData works
|
||||
//otherwise header would not be generated correctly
|
||||
//user, _ := repositories.Users.GetByEmail(email)
|
||||
//c.Set("user", user)
|
||||
user, _ := services.Users.GetByName(c, username)
|
||||
c.Set("user", user)
|
||||
|
||||
// send it back
|
||||
//c.SetSameSite(http.SameSiteLaxMode)
|
||||
c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*24, "", "", false, true)
|
||||
c.HTML(http.StatusOK, "login.html", CreateSessionData(c, gin.H{}))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user