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"
2022-01-09 14:42:03 +01:00
"github.com/uberswe/golang-base-project/ulid"
2021-12-12 14:56:13 +01:00
"golang.org/x/crypto/bcrypt"
"log"
"net/http"
"time"
)
2022-01-09 14:42:03 +01:00
// 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 {
2021-12-13 20:33:50 +01:00
Title : "Login" ,
IsAuthenticated : isAuthenticated ( c ) ,
CacheParameter : controller . config . CacheParameter ,
2021-12-12 14:56:13 +01:00
}
c . HTML ( http . StatusOK , "login.html" , pd )
}
2022-01-09 14:42:03 +01:00
// 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 ) ,
2021-12-13 20:33:50 +01:00
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
2022-01-09 14:42:03 +01:00
sessionIdentifier := ulid . Generate ( )
2021-12-12 14:56:13 +01:00
ses := models . Session {
Identifier : sessionIdentifier ,
}
// Session is valid for 1 hour
2021-12-13 20:33:50 +01:00
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 )
2022-01-09 14:42:03 +01:00
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" )
}