Archived
Template
1
0
This repository has been archived on 2023-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
Files
golang-base-project/routes/login.go

114 lines
2.8 KiB
Go
Raw Normal View History

2021-12-12 14:56:13 +01:00
package routes
import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/uberswe/golang-base-project/middleware"
"github.com/uberswe/golang-base-project/models"
"github.com/uberswe/golang-base-project/ulid"
2021-12-12 14:56:13 +01:00
"golang.org/x/crypto/bcrypt"
"log"
"net/http"
"time"
)
// Login renders the HTML of the login page
2021-12-12 14:56:13 +01:00
func (controller Controller) Login(c *gin.Context) {
pd := PageData{
Title: "Login",
IsAuthenticated: isAuthenticated(c),
CacheParameter: controller.config.CacheParameter,
2021-12-12 14:56:13 +01:00
}
c.HTML(http.StatusOK, "login.html", pd)
}
// LoginPost handles login requests and returns the appropriate HTML and messages
2021-12-12 14:56:13 +01:00
func (controller Controller) LoginPost(c *gin.Context) {
loginError := "Could not login, please make sure that you have typed in the correct email and password. If you have forgotten your password, please click the forgot password link below."
pd := PageData{
Title: "Login",
IsAuthenticated: isAuthenticated(c),
CacheParameter: controller.config.CacheParameter,
2021-12-12 14:56:13 +01:00
}
email := c.PostForm("email")
user := models.User{Email: email}
res := controller.db.Where(&user).First(&user)
if res.Error != nil {
pd.Messages = append(pd.Messages, Message{
Type: "error",
Content: loginError,
})
log.Println(res.Error)
c.HTML(http.StatusInternalServerError, "login.html", pd)
2021-12-12 20:13:03 +01:00
return
2021-12-12 14:56:13 +01:00
}
if res.RowsAffected == 0 {
pd.Messages = append(pd.Messages, Message{
Type: "error",
Content: loginError,
})
c.HTML(http.StatusBadRequest, "login.html", pd)
2021-12-12 20:13:03 +01:00
return
2021-12-12 14:56:13 +01:00
}
if user.ActivatedAt == nil {
pd.Messages = append(pd.Messages, Message{
Type: "error",
Content: "Account is not activated yet.",
})
c.HTML(http.StatusBadRequest, "login.html", pd)
2021-12-12 20:13:03 +01:00
return
2021-12-12 14:56:13 +01:00
}
password := c.PostForm("password")
err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
pd.Messages = append(pd.Messages, Message{
Type: "error",
Content: loginError,
})
c.HTML(http.StatusBadRequest, "login.html", pd)
2021-12-12 20:13:03 +01:00
return
2021-12-12 14:56:13 +01:00
}
// Generate a ulid for the current session
sessionIdentifier := ulid.Generate()
2021-12-12 14:56:13 +01:00
ses := models.Session{
Identifier: sessionIdentifier,
}
// Session is valid for 1 hour
ses.ExpiresAt = time.Now().Add(time.Hour)
2021-12-12 14:56:13 +01:00
ses.UserID = user.ID
res = controller.db.Save(&ses)
if res.Error != nil {
pd.Messages = append(pd.Messages, Message{
Type: "error",
Content: loginError,
})
log.Println(res.Error)
c.HTML(http.StatusInternalServerError, "login.html", pd)
2021-12-12 20:13:03 +01:00
return
2021-12-12 14:56:13 +01:00
}
session := sessions.Default(c)
session.Set(middleware.SessionIDKey, sessionIdentifier)
2021-12-12 14:56:13 +01:00
err = session.Save()
if err != nil {
pd.Messages = append(pd.Messages, Message{
Type: "error",
Content: loginError,
})
log.Println(err)
c.HTML(http.StatusInternalServerError, "login.html", pd)
2021-12-12 20:13:03 +01:00
return
2021-12-12 14:56:13 +01:00
}
c.Redirect(http.StatusTemporaryRedirect, "/admin")
}