continued auth system
This commit is contained in:
64
db/db_cold/authTokens.go
Normal file
64
db/db_cold/authTokens.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package db_cold
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserAuth struct {
|
||||
gorm.Model
|
||||
UserID string `gorm:"primarykey"`
|
||||
UserName string
|
||||
UserLogin string
|
||||
UserEmail string
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
TokenExpires time.Time
|
||||
}
|
||||
|
||||
func (db *DBColdConn) initUserAuth() {
|
||||
db.Gorm.AutoMigrate(&UserAuth{})
|
||||
}
|
||||
|
||||
func (db *DBColdConn) GetAllUserAuth() ([]UserAuth, error) {
|
||||
var userAuths []UserAuth
|
||||
res := db.Gorm.Find(&userAuths)
|
||||
if res.Error != nil {
|
||||
return nil, res.Error
|
||||
}
|
||||
return userAuths, nil
|
||||
}
|
||||
|
||||
// add or update user auth, based on ID
|
||||
func (db *DBColdConn) UpdateUserAuth(userID, userName, userLogin, accessToken, refreshToken string, tokenExpires time.Time) error {
|
||||
userAuth := UserAuth{
|
||||
UserID: userID,
|
||||
UserName: userName,
|
||||
UserLogin: userLogin,
|
||||
AccessToken: accessToken,
|
||||
RefreshToken: refreshToken,
|
||||
TokenExpires: tokenExpires,
|
||||
}
|
||||
|
||||
rows, err := gorm.G[UserAuth](db.Gorm).Where("id = ?", userID).Find(db.Ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(rows) > 0 {
|
||||
// update
|
||||
_, err := gorm.G[UserAuth](db.Gorm).Where("id = ?", userID).Updates(db.Ctx, userAuth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// add
|
||||
err := gorm.G[UserAuth](db.Gorm).Create(db.Ctx, &userAuth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,10 +1,29 @@
|
||||
package db_cold
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"zomo.dev/largehadroncollider/util"
|
||||
)
|
||||
|
||||
// sqlite file
|
||||
|
||||
func InitDBColdConn() (DBColdConn, error) {
|
||||
return DBColdConn{}, nil
|
||||
func InitDBColdConn(conf *util.Config) (*DBColdConn, error) {
|
||||
db, err := gorm.Open(sqlite.Open(conf.SQliteDB), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
cold := &DBColdConn{ db, ctx }
|
||||
cold.initUserAuth()
|
||||
|
||||
return cold, nil
|
||||
}
|
||||
|
||||
type DBColdConn struct {
|
||||
Gorm *gorm.DB
|
||||
Ctx context.Context
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package db_hot
|
||||
|
||||
import "zomo.dev/largehadroncollider/util"
|
||||
|
||||
// redis connection
|
||||
|
||||
func InitDBHotConn() (DBHotConn, error) {
|
||||
return DBHotConn{}, nil
|
||||
func InitDBHotConn(conf *util.Config) (*DBHotConn, error) {
|
||||
return &DBHotConn{}, nil
|
||||
}
|
||||
|
||||
type DBHotConn struct {
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
)
|
||||
|
||||
func InitDBConn(conf *util.Config) (*DBConn, error) {
|
||||
hot, err := db_hot.InitDBHotConn()
|
||||
hot, err := db_hot.InitDBHotConn(conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cold, err := db_cold.InitDBColdConn()
|
||||
cold, err := db_cold.InitDBColdConn(conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -19,6 +19,6 @@ func InitDBConn(conf *util.Config) (*DBConn, error) {
|
||||
}
|
||||
|
||||
type DBConn struct {
|
||||
hot db_hot.DBHotConn
|
||||
cold db_cold.DBColdConn
|
||||
Hot *db_hot.DBHotConn
|
||||
Cold *db_cold.DBColdConn
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user