Building Picgram: Week 2

Photo by Sara Kurfeß on Unsplash

Continuing on with building out the backend for our Picgram web app, I added the following to the Sessions Controller:

class SessionsController < Devise::SessionsController
# is where a user will authenticate his/her credentials and it will assign the JWT to the user if successful.

def create
user = User.find_by_email(sign_in_params[:email])
if user && user.valid_password?(sign_in_params[:password])
token = user.generate_jwt
render json: token.to_json
else
render json: { errors: { ‘email or password’ => [‘is invalid’] } }, status: :unprocessable_entity
end
end
end

As the note within the code states, this is used for the user to authenticate their credentials and by doing so it will assign the JWT token to the user if successful.

In the Application Controller, I also added the following:

before_action :process_token

Now whenever our app is called, it will process the token (if provided) and then take whatever action is required.

In the Registrations Controller, we needed to add a private method for permitting the user to sign in by requiring their email and password.

def create
user = User.new(sign_up_params)
if user.save
token = user.generate_jwt
render json: token.to_json
else
render json: { errors: { ‘email or password’ => [‘is invalid’] } }, status: :unprocessable_entity
end
end

private
def sign_up_params
params.require(:user).permit(:email, :password)
end

We also added the fastjson_api gem to our project which is a JSON serializer for Rails. After bundle installing this gem, we created our Posts and Comments controller with the standard CRUD methods and also our Posts and Comments serializers which has our model relationship associations and our attributes in order to customize what we want to see in our JSON data.

Lastly to wrap up our week, we wanted to add a followers and friendships model in order to keep track of users that want to follow us and users that wanted to be our friends. We hit a couple of bumps when deciding how this will be best illustrated and decided to come up with the below in our schema below:

ActiveRecord::Schema.define(version: 2021_05_06_180418) do

# These are extensions that must be enabled in order to support this database
enable_extension “plpgsql”
create_table “comments”, force: :cascade do |t|
t.text “text”
t.integer “user_id”
t.integer “post_id”
t.datetime “created_at”, precision: 6, null: false
t.datetime “updated_at”, precision: 6, null: false
end

create_table “followings”, force: :cascade do |t|
t.bigint “user_id”, null: false
t.bigint “follower_id”, null: false
t.datetime “created_at”, precision: 6, null: false
t.datetime “updated_at”, precision: 6, null: false
t.index [“follower_id”], name: “index_followings_on_follower_id”
t.index [“user_id”], name: “index_followings_on_user_id”
end

create_table “friendships”, force: :cascade do |t|
t.bigint “user_id”, null: false
t.bigint “friend_id”, null: false
t.datetime “created_at”, precision: 6, null: false
t.datetime “updated_at”, precision: 6, null: false
t.index [“friend_id”], name: “index_friendships_on_friend_id”
t.index [“user_id”], name: “index_friendships_on_user_id”
end

create_table “posts”, force: :cascade do |t|
t.string “image”
t.text “description”
t.integer “user_id”
t.datetime “created_at”, precision: 6, null: false
t.datetime “updated_at”, precision: 6, null: false
end
create_table “users”, force: :cascade do |t|
t.string “email”, default: “”, null: false
t.string “encrypted_password”, default: “”, null: false
t.string “reset_password_token”
t.datetime “reset_password_sent_at”
t.datetime “remember_created_at”
t.datetime “created_at”, precision: 6, null: false
t.datetime “updated_at”, precision: 6, null: false
t.index [“email”], name: “index_users_on_email”, unique: true
t.index [“reset_password_token”], name: “index_users_on_reset_password_token”, unique: true
end

add_foreign_key “followings”, “users”
add_foreign_key “friendships”, “users”
end

And of course when added each and every layer to our project, we tested it out within our rails console to ensure that the data created is the data we want to reflect in our application. Stay tuned for the additions we plan on making next week!

Below is the updated link to our repo:

https://github.com/leplerjacob/picgram-backend

Flatiron School Graduate with finance, budget, and tax experience.