1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-29 00:50:59 +02:00

added oauth2 db errors handling and replaced the auth response map with a struct

This commit is contained in:
Gani Georgiev 2024-11-18 21:16:20 +02:00
parent 37538f2a6d
commit 52e85a8036
2 changed files with 18 additions and 6 deletions

View File

@ -2,6 +2,7 @@ package apis
import ( import (
"context" "context"
"database/sql"
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
@ -95,6 +96,10 @@ func recordAuthWithOAuth2(e *core.RequestEvent) error {
"provider": form.Provider, "provider": form.Provider,
"providerId": authUser.Id, "providerId": authUser.Id,
}) })
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return e.InternalServerError("Failed OAuth2 relation check.", err)
}
switch { switch {
case err == nil && externalAuthRel != nil: case err == nil && externalAuthRel != nil:
authRecord, err = e.App.FindRecordById(form.collection, externalAuthRel.RecordRef()) authRecord, err = e.App.FindRecordById(form.collection, externalAuthRel.RecordRef())
@ -106,7 +111,10 @@ func recordAuthWithOAuth2(e *core.RequestEvent) error {
authRecord = fallbackAuthRecord authRecord = fallbackAuthRecord
case authUser.Email != "": case authUser.Email != "":
// look for an existing auth record by the external auth record's email // look for an existing auth record by the external auth record's email
authRecord, _ = e.App.FindAuthRecordByEmail(form.collection.Id, authUser.Email) authRecord, err = e.App.FindAuthRecordByEmail(form.collection.Id, authUser.Email)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return e.InternalServerError("Failed OAuth2 auth record check.", err)
}
} }
// --------------------------------------------------------------- // ---------------------------------------------------------------
@ -237,7 +245,7 @@ func oauth2Submit(e *core.RecordAuthWithOAuth2RequestEvent, optExternalAuth *cor
payload[e.Collection.OAuth2.MappedFields.Username] = e.OAuth2User.Username payload[e.Collection.OAuth2.MappedFields.Username] = e.OAuth2User.Username
} }
if _, ok := payload[e.Collection.OAuth2.MappedFields.AvatarURL]; !ok && if _, ok := payload[e.Collection.OAuth2.MappedFields.AvatarURL]; !ok &&
// no existing OAuth2 mapping // no explicit avatar payload value and existing OAuth2 mapping
e.Collection.OAuth2.MappedFields.AvatarURL != "" && e.Collection.OAuth2.MappedFields.AvatarURL != "" &&
// non-empty OAuth2 avatar url // non-empty OAuth2 avatar url
e.OAuth2User.AvatarURL != "" { e.OAuth2User.AvatarURL != "" {

View File

@ -109,13 +109,17 @@ func recordAuthResponse(e *core.RequestEvent, authRecord *core.Record, token str
} }
} }
result := map[string]any{ result := struct {
"token": e.Token, Meta any `json:"meta,omitempty"`
"record": e.Record, Record *core.Record `json:"record"`
Token string `json:"token"`
}{
Token: e.Token,
Record: e.Record,
} }
if e.Meta != nil { if e.Meta != nil {
result["meta"] = e.Meta result.Meta = e.Meta
} }
return e.JSON(http.StatusOK, result) return e.JSON(http.StatusOK, result)