Added auth check on all routes

This commit is contained in:
Florian Sylvain
2022-10-20 13:07:06 +02:00
parent e00d5dd25b
commit 0dd12627cf
3 changed files with 26 additions and 4 deletions
+20
View File
@@ -78,3 +78,23 @@ func LogoutUser(c *gin.Context) {
removeSession(user)
SendOkMessageToClient(c, "User successfully logged out.")
}
func AuthCheck(c *gin.Context) {
var user User
username, password, isOk := c.Request.BasicAuth()
if !isOk {
SendErrorMessageToClient(c, "Incorrect or missing user credentials.")
c.Abort()
return
}
user.Email = username
user.Password = password
user.Password = getUserHashedPassword(user)
if !isUserLoggedIn(user) {
SendErrorMessageToClient(c, "Authentification failed, credentials could be wrong, user may not be logged in, session may have expired.")
c.Abort()
return
}
}