diff options
| author | realtradam <[email protected]> | 2024-05-02 20:54:36 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-05-02 20:54:36 -0400 |
| commit | a97531e77e9e3f0e391b2504fcb278cabc2b901e (patch) | |
| tree | b8cd2f6094b66a574a5f1f6dda68f5c852a8c105 /app | |
| parent | 3108882fd50308ed1e3d0a587ce2709aa12a6d9e (diff) | |
| download | gameHolster-a97531e77e9e3f0e391b2504fcb278cabc2b901e.tar.gz gameHolster-a97531e77e9e3f0e391b2504fcb278cabc2b901e.zip | |
finish inital auth
Diffstat (limited to 'app')
| -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 |
10 files changed, 87 insertions, 28 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> |
