From e5a22b8bd810c5dc465a68a9d8cf97e490d1683f Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sat, 25 Mar 2023 15:18:28 +0200 Subject: [PATCH] added a flag indicating OAuth2 auth record creation --- CHANGELOG.md | 2 ++ apis/record_auth.go | 10 ++++++---- core/events.go | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09e8a974..88a6ffcf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,8 @@ - Added option to explicitly set the record id from the Admin UI ([#2118](https://github.com/pocketbase/pocketbase/issues/2118)). +- Added `core.RecordAuthWithOAuth2Event.IsNewRecord` bool field to indicate whether the OAuth2 action created a new auth record. + - **!** Renamed `daos.GetTableColumns()` to `daos.TableColumns()` for consistency with the other Dao table related helpers. - **!** Renamed `daos.GetTableInfo()` to `daos.TableInfo()` for consistency with the other Dao table related helpers. diff --git a/apis/record_auth.go b/apis/record_auth.go index fa022196..9f3bc7b0 100644 --- a/apis/record_auth.go +++ b/apis/record_auth.go @@ -177,10 +177,16 @@ func (api *recordAuthApi) authWithOAuth2(c echo.Context) error { return NewBadRequestError("An error occurred while loading the submitted data.", readErr) } + event := new(core.RecordAuthWithOAuth2Event) + event.HttpContext = c + event.Collection = collection + event.IsNewRecord = false + form.SetBeforeNewRecordCreateFunc(func(createForm *forms.RecordUpsert, authRecord *models.Record, authUser *auth.AuthUser) error { return createForm.DrySubmit(func(txDao *daos.Dao) error { requestData := RequestData(c) requestData.Data = form.CreateData + event.IsNewRecord = true createRuleFunc := func(q *dbx.SelectQuery) error { admin, _ := c.Get(ContextAdminKey).(*models.Admin) @@ -213,10 +219,6 @@ func (api *recordAuthApi) authWithOAuth2(c echo.Context) error { }) }) - event := new(core.RecordAuthWithOAuth2Event) - event.HttpContext = c - event.Collection = collection - _, _, submitErr := form.Submit(func(next forms.InterceptorNextFunc[*forms.RecordOAuth2LoginData]) forms.InterceptorNextFunc[*forms.RecordOAuth2LoginData] { return func(data *forms.RecordOAuth2LoginData) error { event.Record = data.Record diff --git a/core/events.go b/core/events.go index 3437f484..78aa2ef1 100644 --- a/core/events.go +++ b/core/events.go @@ -208,6 +208,7 @@ type RecordAuthWithOAuth2Event struct { HttpContext echo.Context Record *models.Record OAuth2User *auth.AuthUser + IsNewRecord bool } type RecordAuthRefreshEvent struct {