47 lines
962 B
Go
47 lines
962 B
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"git.haelnorr.com/h/oslstats/internal/discord"
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
type RegistrationSession struct {
|
|
DiscordUser *discordgo.User
|
|
Token *discord.Token
|
|
ExpiresAt time.Time
|
|
}
|
|
|
|
func (s *Store) CreateRegistrationSession(user *discordgo.User, token *discord.Token) (string, error) {
|
|
if user == nil {
|
|
return "", errors.New("user cannot be nil")
|
|
}
|
|
if token == nil {
|
|
return "", errors.New("token cannot be nil")
|
|
}
|
|
id := generateID()
|
|
s.sessions.Store(id, &RegistrationSession{
|
|
DiscordUser: user,
|
|
Token: token,
|
|
ExpiresAt: time.Now().Add(5 * time.Minute),
|
|
})
|
|
return id, nil
|
|
}
|
|
|
|
func (s *Store) GetRegistrationSession(id string) (*RegistrationSession, bool) {
|
|
val, ok := s.sessions.Load(id)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
|
|
session := val.(*RegistrationSession)
|
|
if time.Now().After(session.ExpiresAt) {
|
|
s.sessions.Delete(id)
|
|
return nil, false
|
|
}
|
|
|
|
return session, true
|
|
}
|