DEV Community

John Bull
John Bull

Posted on • Updated on

A key dependency I was using was deprecated, what did I learn?

Well, I took some time, fumbled around with a few false starts, tried a few things that didn’t work and I am finally in the groove again on my application migration. It feels awesome, to be in the driver seat again, happily jamming away on some code and making my app come to life. So here is what I have learned don’t use a third party authentication API, just write one yourself, it will save hours and hours of time and no amount of frustration.

So what I had was a social media type of application where a user could log in create an account and add a blog and post examples of their projects that they have built. This worked well, but to get that to work with the old authentication API I was using I had to do a couple of workarounds to get a few things to work and those workarounds cased what I had to have some bad code, I can say that now looking back. At the time I was like, “This will work, for now, I will create a custom field for each user in the app’s database, essentially copying the id from my mongo database to the third-party database and then I could check if it matched for a few of my features. Now I didn’t know passprt.js at the time, so this seemed like a fair trade off, now that I have the passport.js implemented I am able to check the user through passport.js’s built-in method. This saves a ton of time, and I found a ton of these little things that I did, that is making me realize I did a ton of extra work while using this shortcut that at the time seemed reasonable.

Let’s compare the two get routes where that are essentially doing the same thing.

This first one is the old way. It isn’t that their is all that much more code, it is all the time troubleshooting and testing the extra steps and pages when in the end there is a more straightforward approach.

router.get(/showUser/:id, function(req, res) {
        FccUsers.findById(req.params.id).populate(projects blogs).exec(function(err, userRef) {
            if (err) {
                console.log(err);
            } else {
                Project.find(function(err, projects) {
                if (err) {
                    console.log(err);
                } else {
                    Blog.find(function(err, blogs) {
                    if (req.user && userRef._id === req.user.customData.authUserID) {
                            res.render(showUser, {userRef: userRef, projects: projects, blogs: blogs,});
                        } else {
                            res.render(showUserPublic, {userRef: userRef, projects: projects, blogs: blogs,});
                        }
                    });
                }
            });
        }
    });
});
Enter fullscreen mode Exit fullscreen mode

This is what I was able to refactor to.

router.get(/showUser/:id, function(req, res) {
    User.findById(req.params.id).populate(projects blogs).exec(function(err,                 
        userRef) {
            if (err) {
                console.log(err);
            } else {
        Project.find(function(err, projects) {
            if (err) {
            console.log(err);
            } else {
                Blog.find(function(err, blogs) {
                  res.render(showUser, {userRef: userRef, projects: projects, blogs: blogs,});
                });
            }
        });
      }
    });
});
Enter fullscreen mode Exit fullscreen mode

Before I had to check if the user was logged in, and check if that matched my external copy of the user id. I had to do this because I couldn’t reference the user object until after the user was logged except by having a convoluted set of routes and the callback that checked back with the external system for this reference to the user. It’s quite a bit more concise and less like the callback hell that drives any sane person to the brink. I am so glad I don’t have to do that anymore. Passport.js Yall.

This is just one example but there are little things all over the code base and it’s getting a whole lot smaller now.

At first, my progress was slow, It took a couple hours to get my feet underneath me again on this project. I thought about giving up. doing a re-write of the whole thing. But I’m glad I stuck with it. After a few hours of fiddling, I’m back up to my normal, steady pace. Definitely, a learning experience and I will post a rundown when she is back up.

I know the problem with all of this is that it is all in callbacks which work fine for this project but can get a bit hellish, so in the future, it is going to be promises and async-await going forward.

If you want to check it out.

http://ec2-13-57-104-16.us-west-1.compute.amazonaws.com/

My portfolio
https://jbull.co

Oldest comments (0)