Move the cookie creation function to the correct module

This commit is contained in:
Ivan R. 2023-04-09 11:30:28 +05:00
parent bf4c52e68b
commit 7b34b597c8
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
2 changed files with 7 additions and 8 deletions

View file

@ -4,7 +4,6 @@ import (
"crypto/rand"
"encoding/base64"
"errors"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"time"
@ -87,11 +86,6 @@ func CreateAccessToken(db *gorm.DB, adminID uint64) (AccessToken, error) {
return accessToken, nil
}
// Save token for 29 days in cookies
func SetTokenCookie(c *gin.Context, token AccessToken) {
c.SetCookie("phoenix-token", token.Value, 60*60*24*29, "/", "", false, true)
}
func ValidateToken(db *gorm.DB, value string) error {
var token AccessToken
result := db.Where("value = ?", value).First(&token)

View file

@ -43,7 +43,7 @@ func CreateUser(c *gin.Context, db *gorm.DB) {
ShowError(c, err)
return
}
backend.SetTokenCookie(c, token)
SetTokenCookie(c, token)
// Redirect to homepage.
c.Redirect(http.StatusFound, "/")
@ -65,8 +65,13 @@ func AuthorizeUser(c *gin.Context, db *gorm.DB) {
ShowError(c, err)
return
}
backend.SetTokenCookie(c, token)
SetTokenCookie(c, token)
// Redirect to homepage.
c.Redirect(http.StatusFound, "/")
}
// Save token for 29 days in cookies
func SetTokenCookie(c *gin.Context, token backend.AccessToken) {
c.SetCookie("phoenix-token", token.Value, 60*60*24*29, "/", "", false, true)
}