aboutsummaryrefslogblamecommitdiffstats
path: root/internal/utils/key_generator.go
blob: 1f88be911a1dba513eb1e8dd2002c5cb7a739830 (plain) (tree)



























                                                                                                                               
package utils

import (
	"encoding/base64"

	crand "crypto/rand"

	"donetick.com/core/logging"
	"github.com/gin-gonic/gin"
)

func GenerateInviteCode(c *gin.Context) string {
	logger := logging.FromContext(c)
	// Define the length of the token (in bytes). For example, 32 bytes will result in a 44-character base64-encoded token.
	tokenLength := 12

	// Generate a random byte slice.
	tokenBytes := make([]byte, tokenLength)
	_, err := crand.Read(tokenBytes)
	if err != nil {
		logger.Errorw("utility.GenerateEmailResetToken failed to generate random bytes", "err", err)
	}

	// Encode the byte slice to a base64 string.
	token := base64.URLEncoding.EncodeToString(tokenBytes)

	return token
}