Here we go again, a long list of project requirements. Getting more and more technical and integrating more concepts and features. My Ruby on Rails project for Flatirons was based on my Sinatra project but this time written in Rails, a bigger and better framework than Sinatra. I got to say, making a wed app in rails was so much easier(when i got more practice in it) than Sinatra, it was actually quite fun to do.
You get a whole bunch of built in features with rails that can basically almost wright all the code for you. From generating a controller to generating a database, Rails is the way to go. Of course, with a more powerful framework like Rails, there is a lot to learn.
GroupGames was created from my love of all types of board games and just table top games in general. It allows a user to sign up or sign in with their google account(one of the project requirements), and be able to view games that other people play and also be able to add games to the list. Then the user can add a review and a rating to a game so theres can read it and maybe try the game out if they haven’t played it before.
There are 4 models that my app is based on.
- User Model has_many game_reviews and also has_many games through game_reviews
- Game Model belongs_to category and also has_many game_reviews so naturally, my game model also has_many users through game_reviews
- Game_review Model would then belong_to a user and a game. This is my join table
- Category Model has_many games
With these models setup, it makes saving and retrieving the data easier and faster.
One of the requirements for this project was being able to log into the web app using a 3rd party login like Facebook or Google. I used Google because i actually don’t have a Facebook… This was achieved with using a gem called omniauth. Omniauth allows the user to use his or her, in my case Google, to create or sign in the user to my web app.
But before any of this magic can take place, i have to get the client keys from Google to be able to do this. By registering for a developer account and adding my project i was given the keys needed. I keep the keys safe by adding a .env file and stored my information with in and including that file in my .gitignore file so it would get pushed into my git repo. That keeps my information safe but i still need to is it in my application. Creating a config/initializers/omniauth.rb and adding:
Making sure to add the necessary routes within the config/routes file:
Adding this route im able to use it as a link_to in my view to be able to redirect the user to the Google login page to sign in with their account.
app/views/sessions/new.html.erb<%= link_to "Login with Google", '/auth/google_oauth2', method: :post, class: "btn-small" %>
But once the user is able to use their Google account to login, I have to somehow be able to use the information the i receive from Google which is in the form of a nested hash.
My users have to have a username, email, and password attributes in order to signup and get saved as a user. With the information received from Google, i am able to use most of the information such as the first_name as the username and their email as you guest it, and email, but that left the password. The users password isn't something Google particularly wants others to see so we have to get creative. Since the user of my web app is using Google to sign in and will most likely keep using it, they aren't needing to set a password. So for the password attribute I’ll be setting the password for the. But don’t worry, the way I’ll be setting it is for the most part pretty secure. Using the built in console in rails ill show you what this password might look like:
2.6.1 :001 > password = SecureRandom.hex(10)=> "80f3e42704dbea29fd22"2.6.1 :002 >
Just a bunch of random letters and numbers for that user. And if you’re wondering,”What happens if a hacker gets his hands on that password?”, thats a good question. It would take a hacker a long time to be able to get his hands on that password. And by the time he does, your password would have changed by then because it sets a new password every time you login using your Google account, so you have nothing to worry about.
Using rails to build this wed app was a little tricky but once i was able to grasp the process and just how easy it was to create the controller, model, views, and routes with one single command, it made things a lot easier. Im still no pro at it and still have a whole lot to learn in the software engineering world, but i can definitely tell how far i have come.
Here is my git repo for this project.