rough skeleton and basic user authentication

This commit is contained in:
zomo
2025-10-31 10:32:50 -05:00
commit 8355ca374b
17 changed files with 542 additions and 0 deletions

78
ttv/auth.go Normal file
View File

@@ -0,0 +1,78 @@
package ttv
import (
"context"
"errors"
"github.com/adeithe/go-twitch/api"
"zomo.dev/largehadroncollider/db"
"zomo.dev/largehadroncollider/util"
)
// sign in to twitch with each saved tokens
func initAuth(conf *util.Config, dbConn *db.DBConn) (*TwitchAuth, error) {
ctx := context.Background()
tokens, err := getTokensFromDB(dbConn)
if err != nil {
return nil, err
}
client := api.New(conf.ClientID)
accounts, err := testTokens(ctx, client, tokens)
if err != nil {
return nil, err
}
return &TwitchAuth{ ctx, client, accounts }, nil
}
type TwitchAuth struct {
Ctx context.Context
Client *api.Client
Accounts []TwitchAuthAccount
}
type TwitchAuthAccount struct {
api.User
Token string
}
func getTokensFromDB(dbConn *db.DBConn) ([]string, error) {
// TODO db cold
return []string{}, nil
}
func testTokens(ctx context.Context, client *api.Client, tokens []string) ([]TwitchAuthAccount, error) {
accounts := make([]TwitchAuthAccount, 0)
for _, token := range tokens {
account, err := testToken(ctx, client, token)
if err != nil {
return nil, err
}
accounts = append(accounts, account)
}
return accounts, nil
}
func testToken(ctx context.Context, client *api.Client, token string) (TwitchAuthAccount, error) {
users, err := client.Users.List().Do(ctx, api.WithBearerToken(token))
if err != nil {
return TwitchAuthAccount{}, err
}
usersData := users.Data
if len(usersData) <= 0 {
return TwitchAuthAccount{}, errors.New("user data returned an empty array")
}
mainUser := usersData[0]
return TwitchAuthAccount{
mainUser,
token,
}, nil
}

6
ttv/events.go Normal file
View File

@@ -0,0 +1,6 @@
package ttv
// hook into twitch events with a given user auth
// the websocket server (or anything else) will have a line to send a function here, i.e. ttv.AddEventHook(func(eventInfo) { ... })
// this ttv/events class will act as an Event Emitter, where these functions will all be called

19
ttv/main.go Normal file
View File

@@ -0,0 +1,19 @@
package ttv
import (
"zomo.dev/largehadroncollider/db"
"zomo.dev/largehadroncollider/util"
)
func InitTwitchConn(conf *util.Config, dbConn *db.DBConn) (*TwitchConn, error) {
auth, err := initAuth(conf, dbConn)
if err != nil {
return nil, err
}
return &TwitchConn{ auth }, nil
}
type TwitchConn struct {
Auth *TwitchAuth
}