For the past month I’ve been working on a project for a client. As part of my desire to always continue learning new things, I switched up my usual tech stack for this one.
Up till now, I’ve typically only created basic rails applications. My front end has always been basic jquery with a framework like Foundation or Bootstrap. This go around, I decided to do a rails backend, with an angular front end. Several people have asked why I decided to use angular, and I don’t have a great answer for that other than because it was the first JS framework I was interested in learning more about.
In Rails 5.1 they introduced webpacker and the ability to bake a JS frontend directly into any rails application. Initially I was going to take this approach, but my limited knowledge of how Angular applications worked in conjunction with a rails application proved this route to be extremely challenging for me. So I modified my approach and separated the angular side into its own application to be served by a strictly API driven rails backend.
I believe I now could implement it the other way for the next project I work on, should I decide to do it with angular. I understand the file structure of an angular app, and realize how that translated to the way webpacker set things up.
But all of that pretext really has nothing to do with the topic of this post. When is it ok to give up on your current strategy of solving a problem?
The main struggle I have been having until tonight was how to implement the user authentication system between the angular application and rails api. I’ve been using a combination of technologies (devise_token_auth and angular2-token) which from what I’ve read is a popular choice in my tech stack. However, it’s not quite as simple as plug ‘n play like the standard Devise setup is.
The error was something along the lines of being unable to authenticate the user because the origin of the request wasn’t the same as the source, also known as a Cross-Origin Resource Sharing error (CORS). I wracked my brain over this problem for the last couple of weeks, periodically returning to attempt solutions to no avail.
But tonight I finally stumbled across a sliver of info that pointed out the solution. The angular request wasn’t sending over the correct headers, regardless of me having it setup properly according to the documentation for both services. The user auth data for the current session wasn’t being passed back to the rails api, so it couldn’t verify that the user requesting it, was the same user who was signed in. By passing the correct data through each request that needs an authenticated user, the api backend can now see the correct tokens to verify the user’s identity.
The approach I took to this problem was kind of like how a dog remembers he has a toy, plays with the toy for a few minutes, but drops it when he gets bored and sees a shinier new thing to look at. I may have been able to solve this problem a few weeks ago if I just focused on the singular problem, but then the rest of the application would’ve been put on temporary hold.
I know some people would argue I should’ve stayed on that one task till I solved it. This is often how more traditional software development bug fixes take shape. However, I don’t think that’s a viable option when you’re the only person working on a project which has deadlines. If that one feature was my only task, and I have zero other responsibilities in the project, sure, I would hunker down and barrage google with my poorly worded search phrases. However, when working solo, I think my approach is more appropriate.
The occasional times that I worked on the problem, I was able to jot down ideas for what might be causing the issue, in this case the error messages weren’t clear to what was the cause. Because I limited how long I worked on this problem to how many times I reread the same stack overflow answer from 5 years ago, my sanity levels were kept in check.
I think the most important part of my approach, was other aspects of the application were able to be developed as I was working on this problem. By taking my attention off the giant brick wall, I was able to continue working on other deliverables that needed to get done while still thinking about the issue in the back of my mind.
I’m the type of person who has to ignore a problem sometimes to find the solution if it isn’t obvious. For the foreseeable future, I’ll likely continue using this strategy.
How would you have approached this issue? How do you like to solve problems like this?
Happy Coding! Jercomments powered by Disqus