diff options
| -rw-r--r-- | app/controllers/api/v1/auth_controller.rb | 65 | ||||
| -rw-r--r-- | app/controllers/api/v1/users_controller.rb | 16 | ||||
| -rw-r--r-- | app/helpers/api/v1/users_helper.rb | 2 | ||||
| -rw-r--r-- | app/javascript/components/Layout.jsx | 6 | ||||
| -rw-r--r-- | app/javascript/routes/index.jsx | 6 | ||||
| -rw-r--r-- | app/models/user.rb | 4 | ||||
| -rw-r--r-- | app/views/api/v1/users/create.html.erb | 4 | ||||
| -rw-r--r-- | app/views/api/v1/users/delete.html.erb | 4 | ||||
| -rw-r--r-- | app/views/api/v1/users/index.html.erb | 4 | ||||
| -rw-r--r-- | app/views/api/v1/users/new.html.erb | 4 | ||||
| -rw-r--r-- | config/routes.rb | 4 | ||||
| -rw-r--r-- | db/migrate/20240428013649_create_users.rb | 13 | ||||
| -rw-r--r-- | db/migrate/20240503001922_create_active_storage_tables.active_storage.rb | 57 | ||||
| -rw-r--r-- | db/schema.rb | 20 | ||||
| -rw-r--r-- | package.json | 3 | ||||
| -rw-r--r-- | test/controllers/api/v1/users_controller_test.rb | 23 | ||||
| -rw-r--r-- | test/fixtures/users.yml | 13 | ||||
| -rw-r--r-- | test/models/user_test.rb | 7 |
18 files changed, 212 insertions, 43 deletions
diff --git a/app/controllers/api/v1/auth_controller.rb b/app/controllers/api/v1/auth_controller.rb index 2688074..32f33dc 100644 --- a/app/controllers/api/v1/auth_controller.rb +++ b/app/controllers/api/v1/auth_controller.rb @@ -5,10 +5,16 @@ class Api::V1::AuthController < ApplicationController @user_table ||= {} end end + def data if !cookies[:session].nil? puts cookies[:session] - render json: Api::V1::AuthController.user_table[cookies[:session]] + #render json: Api::V1::AuthController.user_table[cookies[:session]] + result = User.find_by(access_token_digest: cookies[:session]) + result[:user_data] = result[:user_data] + puts "A PREFIX SO WE CAN SEE IT" + pp result + render json: result else puts "Not logged in" end @@ -17,23 +23,38 @@ class Api::V1::AuthController < ApplicationController # user logs in through github # github redirects them to this endpoint with the token in the url as query params # we need to use this token to exchange with github for user info(i.e username) - puts "Code: #{params[:code]}" # this is the github token - puts ENV["GITHUB_CLIENT_SECRET"] - puts ENV["GITHUB_CLIENT_ID"] + #puts "Code: #{params[:code]}" # this is the github token + #puts ENV["GITHUB_CLIENT_SECRET"] + #puts ENV["GITHUB_CLIENT_ID"] access_token = get_access_token(params[:code]) - user_data = get_github_user_data(access_token) - puts "USER DATA:" - pp user_data - token = "#{user_data['id']}" - hashed_token = OpenSSL::HMAC.hexdigest(ENV["ENC_ALGO"], ENV["ENC_KEY"], token + access_token) + user_data = JSON.parse(get_github_user_data(access_token)) + #puts "------------------------- USER DATA: ------------------------- " + #pp user_data + id = user_data['id'].to_s + #puts "id: #{id}, at: #{access_token}" + + hashed_token = hash_token("#{access_token}") Api::V1::AuthController.user_table[hashed_token] = user_data - puts "Hashed Token: #{hashed_token}" + #puts "Hashed Token: #{hashed_token}" cookies[:session] = hashed_token + user_params = { + access_token_digest: hashed_token, + salt: params[:code].to_s, + user_data: user_data + } + puts "USER DATA HERE NERD" + puts user_data.class + user = User.find_or_create_by(identifier: id) + user.update(user_params) redirect_to '/' end private + def hash_token(token) + OpenSSL::HMAC.hexdigest(ENV["ENC_ALGO"], ENV["ENC_KEY"], token) + end + def get_github_user_data(access_token) uri = URI("https://api.github.com/user") headers = { Authorization: "Bearer #{access_token}" } @@ -45,20 +66,20 @@ class Api::V1::AuthController < ApplicationController puts response #if response.is_a?(Net::HTTPSuccess) #if response.body.nil? - result = response - if !result["error"].nil? - puts "Error: #{result["error"]}" - puts response - # we had an error - # TODO - else - puts "huh?" if result.nil? - return result - end + result = response + if !result["error"].nil? + puts "Error: #{result["error"]}" + puts response + # we had an error + # TODO + else + puts "huh?" if result.nil? + return result + end #else # puts "Error(body nil)" - # something went wrong? - # TODO + # something went wrong? + # TODO #end end diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb new file mode 100644 index 0000000..f367ae4 --- /dev/null +++ b/app/controllers/api/v1/users_controller.rb @@ -0,0 +1,16 @@ +class Api::V1::UsersController < ApplicationController + def index + # return list of all users + blog = User.all.order(created_at: :desc) + render json: blog + end + + def create_or_update(user_params) + # add new user, overwrite if exists + end + + def delete + # remove user + end + +end diff --git a/app/helpers/api/v1/users_helper.rb b/app/helpers/api/v1/users_helper.rb new file mode 100644 index 0000000..4d5288c --- /dev/null +++ b/app/helpers/api/v1/users_helper.rb @@ -0,0 +1,2 @@ +module Api::V1::UsersHelper +end diff --git a/app/javascript/components/Layout.jsx b/app/javascript/components/Layout.jsx index 74568cd..6bf698a 100644 --- a/app/javascript/components/Layout.jsx +++ b/app/javascript/components/Layout.jsx @@ -3,14 +3,14 @@ import { Outlet, Link } from "react-router-dom"; export default function Layout ({userData}) { - //console.log(userData); - //const [userData, setUserData] = useState({ login: "" }); + console.log(userData); + //const [userData, setUserData] = useState({ name: "" }); return ( <> <div className="flex flex-row h-screen bg-slate-800 text-slate-100"> <nav className="flex flex-row h-full w-64 p-4 gap-4 items-center"> <div className="h-full flex flex-col"> - <div>Logged in as: {userData.login}</div> + { userData.name ? <div> Logged in as: {userData.name} </div> : <a href="https://github.com/login/oauth/authorize?client_id=74468ad0847e527262d9"> Login with Github </a> } <div className="text-4xl py-12">Adam Malczewski</div> <div className="flex flex-row justify-center w-full block grow"> <div className="block grow"> diff --git a/app/javascript/routes/index.jsx b/app/javascript/routes/index.jsx index bc5d541..235f41b 100644 --- a/app/javascript/routes/index.jsx +++ b/app/javascript/routes/index.jsx @@ -1,12 +1,12 @@ import React, { useState, useEffect } from "react"; -import { BrowserRouter as Router, Routes, Route, Link } from "react-router-dom"; +import { BrowserRouter as Router, Routes, Route } from "react-router-dom"; import Home from "../components/Home"; import Blogs from "../components/Blogs"; import Layout from "../components/Layout"; export default function index() { - const [userData, setUserData] = useState({ login: "" }); + const [userData, setUserData] = useState({}); const url = "/api/v1/auth/data"; useEffect(() => { fetch(url).then((response) => { @@ -14,7 +14,7 @@ export default function index() return response.json(); } throw new Error("Network response was not ok."); - }).then((response) => setUserData(response));}, []); + }).then((response) => setUserData(response.user_data));}, []); // get user data here // then pass it in as 'props' into the components return (<> diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..39c7822 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,4 @@ +class User < ApplicationRecord + + validates :identifier, presence: true +end diff --git a/app/views/api/v1/users/create.html.erb b/app/views/api/v1/users/create.html.erb new file mode 100644 index 0000000..a4e8216 --- /dev/null +++ b/app/views/api/v1/users/create.html.erb @@ -0,0 +1,4 @@ +<div> + <h1 class="font-bold text-4xl">Api::V1::Users#create</h1> + <p>Find me in app/views/api/v1/users/create.html.erb</p> +</div> diff --git a/app/views/api/v1/users/delete.html.erb b/app/views/api/v1/users/delete.html.erb new file mode 100644 index 0000000..940daee --- /dev/null +++ b/app/views/api/v1/users/delete.html.erb @@ -0,0 +1,4 @@ +<div> + <h1 class="font-bold text-4xl">Api::V1::Users#delete</h1> + <p>Find me in app/views/api/v1/users/delete.html.erb</p> +</div> diff --git a/app/views/api/v1/users/index.html.erb b/app/views/api/v1/users/index.html.erb new file mode 100644 index 0000000..9681401 --- /dev/null +++ b/app/views/api/v1/users/index.html.erb @@ -0,0 +1,4 @@ +<div> + <h1 class="font-bold text-4xl">Api::V1::Users#index</h1> + <p>Find me in app/views/api/v1/users/index.html.erb</p> +</div> diff --git a/app/views/api/v1/users/new.html.erb b/app/views/api/v1/users/new.html.erb new file mode 100644 index 0000000..58e8003 --- /dev/null +++ b/app/views/api/v1/users/new.html.erb @@ -0,0 +1,4 @@ +<div> + <h1 class="font-bold text-4xl">Api::V1::Users#new</h1> + <p>Find me in app/views/api/v1/users/new.html.erb</p> +</div> diff --git a/config/routes.rb b/config/routes.rb index d164474..786c21d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,10 @@ Rails.application.routes.draw do namespace :api do namespace :v1 do + get 'users/index', to: 'users#index' + #get 'users/new' + #get 'users/create' + #get 'users/delete' get 'blogs/index', to: 'blog#index' post 'blogs/create', to: 'blog#create' get '/show/:id', to: 'blog#show' diff --git a/db/migrate/20240428013649_create_users.rb b/db/migrate/20240428013649_create_users.rb new file mode 100644 index 0000000..04a58c6 --- /dev/null +++ b/db/migrate/20240428013649_create_users.rb @@ -0,0 +1,13 @@ +class CreateUsers < ActiveRecord::Migration[7.1] + def change + create_table :users do |t| + t.string :identifier + t.string :access_token_digest + t.string :salt + t.json :user_data + + t.timestamps + end + add_index :users, :identifier, unique: true + end +end diff --git a/db/migrate/20240503001922_create_active_storage_tables.active_storage.rb b/db/migrate/20240503001922_create_active_storage_tables.active_storage.rb new file mode 100644 index 0000000..e4706aa --- /dev/null +++ b/db/migrate/20240503001922_create_active_storage_tables.active_storage.rb @@ -0,0 +1,57 @@ +# This migration comes from active_storage (originally 20170806125915) +class CreateActiveStorageTables < ActiveRecord::Migration[7.0] + def change + # Use Active Record's configured type for primary and foreign keys + primary_key_type, foreign_key_type = primary_and_foreign_key_types + + create_table :active_storage_blobs, id: primary_key_type do |t| + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.string :service_name, null: false + t.bigint :byte_size, null: false + t.string :checksum + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :key ], unique: true + end + + create_table :active_storage_attachments, id: primary_key_type do |t| + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type + t.references :blob, null: false, type: foreign_key_type + + if connection.supports_datetime_with_precision? + t.datetime :created_at, precision: 6, null: false + else + t.datetime :created_at, null: false + end + + t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + + create_table :active_storage_variant_records, id: primary_key_type do |t| + t.belongs_to :blob, null: false, index: false, type: foreign_key_type + t.string :variation_digest, null: false + + t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true + t.foreign_key :active_storage_blobs, column: :blob_id + end + end + + private + def primary_and_foreign_key_types + config = Rails.configuration.generators + setting = config.options[config.orm][:primary_key_type] + primary_key_type = setting || :primary_key + foreign_key_type = setting || :bigint + [primary_key_type, foreign_key_type] + end +end diff --git a/db/schema.rb b/db/schema.rb index 8ba434e..69731f6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_04_09_004746) do +ActiveRecord::Schema[7.1].define(version: 2024_05_03_001922) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -42,13 +42,6 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_09_004746) do t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end - create_table "attachments", force: :cascade do |t| - t.string "filename", null: false - t.binary "data", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - create_table "blogs", force: :cascade do |t| t.string "name", null: false t.string "category", null: false @@ -60,13 +53,14 @@ ActiveRecord::Schema[7.1].define(version: 2024_04_09_004746) do t.datetime "updated_at", null: false end - create_table "recipes", force: :cascade do |t| - t.string "name", null: false - t.text "ingredients", null: false - t.text "instruction", null: false - t.string "image", default: "https://raw.githubusercontent.com/do-community/react_rails_recipe/master/app/assets/images/Sammy_Meal.jpg" + create_table "users", force: :cascade do |t| + t.string "identifier" + t.string "access_token_digest" + t.string "salt" + t.json "user_data" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["identifier"], name: "index_users_on_identifier", unique: true end add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" diff --git a/package.json b/package.json index a4bc6a6..5d91cff 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "@hotwired/turbo-rails": "^8.0.4", "@popperjs/core": "^2.11.8", "autoprefixer": "^10.4.19", - "bootstrap-icons": "^1.11.3", "esbuild": "^0.20.1", "nodemon": "^3.1.0", "postcss": "^8.4.38", @@ -21,7 +20,7 @@ }, "scripts": { "build": "esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets", - "build:css:compile": "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules", + "build:css:compile": "sass ./app/assets/builds/application.css --no-source-map --load-path=node_modules", "build:css:prefix": "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css", "build:css": "yarn build:css:compile && yarn build:css:prefix", "watch:css": "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \"yarn build:css\"" diff --git a/test/controllers/api/v1/users_controller_test.rb b/test/controllers/api/v1/users_controller_test.rb new file mode 100644 index 0000000..e5636d1 --- /dev/null +++ b/test/controllers/api/v1/users_controller_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest + test "should get index" do + get api_v1_users_index_url + assert_response :success + end + + test "should get new" do + get api_v1_users_new_url + assert_response :success + end + + test "should get create" do + get api_v1_users_create_url + assert_response :success + end + + test "should get delete" do + get api_v1_users_delete_url + assert_response :success + end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..9cf2d21 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,13 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + identifier: MyString + access_token_digest: MyString + salt: MyString + data: + +two: + identifier: MyString + access_token_digest: MyString + salt: MyString + data: diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000..5c07f49 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end |
