package core
import (
"context"
"errors"
validation "ganigeorgiev.com/pocketbase/ozzo-validation/v4"
"ganigeorgiev.com/pocketbase/pocketbase/tools/auth"
"ganigeorgiev.com/pocketbase/pocketbase/tools/hook"
"ganigeorgiev.com/pocketbase/pocketbase/tools/types"
)
var (
_ Model = (*ExternalAuth)(nil)
_ PreValidator = (*ExternalAuth)(nil)
_ RecordProxy = (*ExternalAuth)(nil)
)
const CollectionNameExternalAuths = "_externalAuths"
type ExternalAuth struct {
*Record
}
func NewExternalAuth(app App) *ExternalAuth {
m := &ExternalAuth{}
c, err := app.FindCachedCollectionByNameOrId(CollectionNameExternalAuths)
if err != nil {
c = NewBaseCollection("@__invalid__")
}
m.Record = NewRecord(c)
return m
}
func (m *ExternalAuth) PreValidate(ctx context.Context, app App) error {
if m.Record == nil || m.Record.Collection().Name != CollectionNameExternalAuths {
return errors.New("missing or invalid ExternalAuth ProxyRecord")
}
return nil
}
func (m *ExternalAuth) ProxyRecord() *Record {
return m.Record
}
func (m *ExternalAuth) SetProxyRecord(record *Record) {
m.Record = record
}
func (m *ExternalAuth) CollectionRef() string {
return m.GetString("collectionRef")
}
func (m *ExternalAuth) SetCollectionRef(collectionId string) {
m.Set("collectionRef", collectionId)
}
func (m *ExternalAuth) RecordRef() string {
return m.GetString("recordRef")
}
func (m *ExternalAuth) SetRecordRef(recordId string) {
m.Set("recordRef", recordId)
}
func (m *ExternalAuth) Provider() string {
return m.GetString("provider")
}
func (m *ExternalAuth) SetProvider(provider string) {
m.Set("provider", provider)
}
func (m *ExternalAuth) ProviderId() string {
return m.GetString("providerId")
}
func (m *ExternalAuth) SetProviderId(providerId string) {
m.Set("providerId", providerId)
}
func (m *ExternalAuth) Created() types.DateTime {
return m.GetDateTime("created")
}
func (m *ExternalAuth) Updated() types.DateTime {
return m.GetDateTime("updated")
}
func (app *BaseApp) registerExternalAuthHooks() {
recordRefHooks[*ExternalAuth](app, CollectionNameExternalAuths, CollectionTypeAuth)
app.OnRecordValidate(CollectionNameExternalAuths).Bind(&hook.Handler[*RecordEvent]{
Func: func(e *RecordEvent) error {
providerNames := make([]any, 0, len(auth.Providers))
for name := range auth.Providers {
providerNames = append(providerNames, name)
}
provider := e.Record.GetString("provider")
if err := validation.Validate(provider, validation.Required, validation.In(providerNames...)); err != nil {
return validation.Errors{"provider": err}
}
return e.Next()
},
Priority: 99,
})
app.OnRecordUpdateExecute().Bind(&hook.Handler[*RecordEvent]{
Func: func(e *RecordEvent) error {
if !e.Record.Collection().IsAuth() {
return e.Next()
}
hasUpgradedVerified := !e.Record.Original().IsNew() && !e.Record.Original().Verified() && e.Record.Verified()
if !hasUpgradedVerified {
return e.Next()
}
originalApp := e.App
return e.App.RunInTransaction(func(txApp App) error {
e.App = txApp
defer func() { e.App = originalApp }()
externalAuths, err := txApp.FindAllExternalAuthsByRecord(e.Record)
if err != nil {
return err
}
if len(externalAuths) > 0 {
if err := txApp.DeleteAllExternalAuthsByRecord(e.Record); err != nil {
return err
}
e.Record.RefreshTokenKey()
}
return e.Next()
})
},
Priority: 99,
})
}