Files
authorizer/server/utils/common.go

50 lines
1.2 KiB
Go
Raw Normal View History

package utils
import (
2022-01-21 12:18:07 +05:30
"log"
2022-01-17 11:32:13 +05:30
"github.com/authorizerdev/authorizer/server/db"
2022-01-21 12:18:07 +05:30
"github.com/authorizerdev/authorizer/server/db/models"
2022-01-17 11:32:13 +05:30
"github.com/gin-gonic/gin"
)
2022-01-17 11:32:13 +05:30
// StringSliceContains checks if a string slice contains a particular string
2021-10-19 12:57:59 +05:30
func StringSliceContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
2022-01-17 11:32:13 +05:30
// SaveSessionInDB saves sessions generated for a given user with meta information
2022-01-21 12:18:07 +05:30
// Do not store token here as that could be security breach
2022-01-17 11:32:13 +05:30
func SaveSessionInDB(userId string, c *gin.Context) {
2022-01-21 12:18:07 +05:30
sessionData := models.Session{
2022-01-17 11:32:13 +05:30
UserID: userId,
UserAgent: GetUserAgent(c.Request),
IP: GetIP(c.Request),
}
2022-01-21 13:34:04 +05:30
err := db.Provider.AddSession(sessionData)
2022-01-21 12:18:07 +05:30
if err != nil {
log.Println("=> error saving session in db:", err)
} else {
log.Println("=> session saved in db:", sessionData)
}
2022-01-17 11:32:13 +05:30
}
2022-01-31 11:35:24 +05:30
// RemoveDuplicateString removes duplicate strings from a string slice
func RemoveDuplicateString(strSlice []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}