swensonhe / strapi-firebase-auth

MIT License
18 stars 8 forks source link

How to authenticate with existing users via phone number #50

Open Rijast opened 8 months ago

Rijast commented 8 months ago

I have an existing Strapi site with a few thousand users already and I want to enable Firebase authentication via your plugin for those users using the phone provider.

As I see when the user is fetched from Strapi it is matched against the firebaseUserID. As my users don’t have one then it will throw an error.

What I would like is to be able to specify the field to match against in order to connect the authenticated user to the Strapi user in the user table.

In my case I am using the user's phone number as the username, so I could modify the code within the firebaseService.js as follows:

    fetchUser: function (decodedToken) { return __awaiter(void 0, void 0, void 0, function () {
        var _a, user, error;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4, (0, promiseHandler_1.promiseHandler)(strapi.db.query("plugin::users-permissions.user").findOne({
                        where: {
                            username: decodedToken.phone_number,
                        },
                    }))];
                case 1:
                    _a = _b.sent(), user = _a.data, error = _a.error;
                    if (error) {
                        throw new ValidationError((error === null || error === void 0 ? void 0 : error.message) || "User not found", error);
                    }
                    return [2, user];
            }
        });
    }); },

That then gives me what I need as the phone number is always unique and also secure as it is used as the authentication to get the token from Firebase.

Would something like this be possible to implement and do you have any suggestions for how I could use the existing plugin to achieve this?

Thanks a lot for you help!