-
Notifications
You must be signed in to change notification settings - Fork 209
Using Authboss with Gorm and Postgresql
frederikhors edited this page Jan 5, 2019
·
1 revision
DRAFT for now (working on https://github.com/volatiletech/authboss/issues/209 and https://github.com/volatiletech/authboss/issues/210).
db.go:
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"log"
"os"
)
var (
DB *gorm.DB
connectionError error
)
func init() {
DB, connectionError = gorm.Open("postgres", os.Getenv("DATABASE_URL"))
if connectionError != nil {
log.Fatal("DB connection error", connectionError)
}
DB.LogMode(true)
}
type User struct {
ID int //but using Gorm you can use: gorm.Model (http://gorm.io/docs/models.html)
// Non-authboss related field
Name string
// Auth
Email string `gorm:"type:varchar(100);unique_index"`
Password string
// from here I need help; working here: https://github.com/volatiletech/authboss/issues/209
// Confirm
ConfirmSelector string
ConfirmVerifier string
Confirmed bool
// Lock
AttemptCount int
LastAttempt time.Time
Locked time.Time
// Recover
RecoverSelector string
RecoverVerifier string
RecoverTokenExpiry time.Time
// OAuth2
OAuth2UID string
OAuth2Provider string
OAuth2AccessToken string
OAuth2RefreshToken string
OAuth2Expiry time.Time
// 2fa
TOTPSecretKey string
SMSPhoneNumber string
SMSSeedPhoneNumber string
RecoveryCodes string
// Remember is in another table
}
storer.go:
package auth
import (
"myProject/models"
"context"
"github.com/volatiletech/authboss"
)
type DBStorer struct {
}
func NewDBStorer() *DBStorer {
return &DBStorer{}
}
func (m DBStorer) Save(ctx context.Context, user authboss.User) error {
// TODO, still don't know if it works
u := user.(*models.User)
if err := models.DB.Create(u).Error; err != nil {
return err
}
return nil
}
func (m DBStorer) Load(ctx context.Context, key string) (user authboss.User, err error) {
var u models.User
if err := models.DB.Where(&models.User{Email: key}).First(&u).Error; err != nil {
return nil, authboss.ErrUserNotFound
}
return &u, nil
}