# Authentication error on surepet contrib

**URL:** <https://discourse.nodered.org/t/authentication-error-on-surepet-contrib/83668>\
**Category:** General\
**Created:** [13 December 2023 12:06 UTC](https://discourse.nodered.org/t/authentication-error-on-surepet-contrib/83668 "2023-12-13T12:06:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [13 December 2023 12:06 UTC](https://discourse.nodered.org/t/authentication-error-on-surepet-contrib/83668/1 "2023-12-13T12:06:40Z")

</div>

Hi everyone,

I'm using the sure-pet contrib to access the API from Surepetcare company.

I came to this place a while ago because this contrib is not in work anymore and was crashing my nodered when handling an error, so again thank you to this forum it was working amazing since then.

Since yesterday I cannot communicate anymore with the API, all I have in logs are

> Error: Get state failed: unauthenticated

The API seems to be working fine, I am using it with Jeedom (less possibilites than node-red, there is my problem) and there is no problem here. I checked the password and login, the API stopped working randomly, no reboot, no crash. It seems that something changes from their side and the actual code is not the right way to access it anymore.

So my code is changed from the github, here it is :

```auto
module.exports = function (RED) {
    "use strict"
    const SurePet = require('sure-pet-care-client');

    function SurePetNode(config) {
        RED.nodes.createNode(this, config);
        const node = this;
        node.surepet = RED.nodes.getNode(config.surepet);

        node.on('input', function (msg) {

            // special case, if `topic` === connect, then re-authenticate
            if (msg.topic === "connect") {
                node.surepet.client = new SurePet.SurePetCareClient();
                node.surepet.client.authenticate(msg.username || msg.payload?.username, msg.password || msg.paylo>
                .then(() => node.status({ fill: "green", shape: "dot", text: "Connected." }))
                .catch(err => {
                    node.error(err, msg)
                    node.status({ fill: "red", shape: "ring", text: err })
                })
                return
            }

            // normal operation
            if (config.mode === 'list_pets') {
                node.surepet.client.getPets().then((pets) => {
                    msg.payload = {
                        pets: pets
                    }
                    node.send(msg)
                }).catch((err) => {
                    node.error(err, msg);
                })

            } else if (config.mode === "get_status") {
                node.surepet.client.getState().then((state) => {
                    msg.payload = state;
                    node.send(msg);
                }).catch((err) => {
                    node.error(err, msg);
                })
            }
        });
    }
    RED.nodes.registerType("surepet", SurePetNode);

    function SurePetCredentials(n) {
        RED.nodes.createNode(this, n);
        const node = this
        node.username = n.username || node.credentials.username;
        node.password = n.password || node.credentials.password;
        node.client = new SurePet.SurePetCareClient();
        if (node.username && node.password) {
            node.log("Présence d'identifiants, authentification en cours.")
            node.client.authenticate(node.username, node.password)
                .then(() => node.status({ fill: "green", shape: "dot", text: "Connected." }))
                .catch(err => {
                    node.status({ fill: "red", shape: "ring", text: err })
                    node.error(err)
                })
        }
    }

    RED.nodes.registerType("surepet-credentials", SurePetCredentials, {
        credentials: {
            username: { type: "text" },
            password: { type: "password" }
        }
    });
}

```

Does someone have an idea of if there is a chance I can make it work again ?  
If this could be helpful, the Jeedom plugin that seems to work is this one : [GitHub - jmvedrine/jeedom-surepetcare: Jeedom plugin for Sure Petcare connected products for pets](https://github.com/jmvedrine/jeedom-surepetcare/tree/master)

Thanks for your time

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [13 December 2023 12:29 UTC](https://discourse.nodered.org/t/authentication-error-on-surepet-contrib/83668/2 "2023-12-13T12:29:49Z")

</div>

> [@klose54](#):
>
> Does someone have an idea of if there is a chance I can make it work again ?

Personally, i would start with raising an issue on the repository: [Issues · jpwsutton/node-red-contrib-surepet · GitHub](https://github.com/jpwsutton/node-red-contrib-surepet/issues). Perhaps the owner does not know this is an issue?

But, that said, the real culpret is probably the underlying library [`sure-pet-care-client`](https://github.com/RMHonor/sure-pet-care/issues) - this has no issues raised either. Maybe consider reporting the probem so the author is aware?

If you want to proceed yourself, then I would suggest forking the node, then switching from `sure-pet-care-client` (updated 3y ago) to [`node-surepetcare`](https://www.npmjs.com/package/node-surepetcare?activeTab=versions) (updated 10m ago) then publishing a PR (Pull Request) back to the original node.

If the author of `node-red-contrib-surepet` accepts - job done. If not, you can always install your branch direct from GitHub or release a new version to NPM e.g. `@YourName/node-red-contrib-surepet`

---

<div class="post-metadata">

**Author:** ![klose54](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klose54](https://discourse.nodered.org/u/klose54)\
**Post date:** [13 December 2023 13:00 UTC](https://discourse.nodered.org/t/authentication-error-on-surepet-contrib/83668/3 "2023-12-13T13:00:15Z")

</div>

I raised an issue back to my last problem months ago but it seems that the author is not active anymore.

I see what you mean, but I'm not capable of doing it myself so I will raise some tickets on the different subjects in hope that someone is still active on it.

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 February 2024 13:01 UTC](https://discourse.nodered.org/t/authentication-error-on-surepet-contrib/83668/4 "2024-02-11T13:01:06Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
