-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented JWT oauth using the github provider.
- Loading branch information
1 parent
c71a02c
commit 6daf0f0
Showing
29 changed files
with
449 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ | |
# Ignore master key for decrypting credentials and more. | ||
/config/master.key | ||
|
||
/.env | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,16 @@ | ||
class ApplicationController < ActionController::API | ||
end | ||
def current_user | ||
token = params[:token] | ||
payload = TokenEncoder.decode(token) | ||
puts payload | ||
@current_user ||= User.find_by_login(payload[0]['sub']) | ||
end | ||
|
||
def logged_in? | ||
current_user != nil | ||
end | ||
|
||
def authenticate_user! | ||
head :unauthorized unless logged_in? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
require 'pry' | ||
|
||
class AuthenticationController < ApplicationController | ||
def github | ||
|
||
#binding.pry | ||
|
||
authenticator = Authenticator.new | ||
user_info = authenticator.github(params[:code]) | ||
|
||
login = user_info[:login] | ||
name = user_info[:name] | ||
puts login | ||
|
||
# Generate token... | ||
token = TokenEncoder.encode(login) | ||
# ... create user if it doesn't exist... | ||
User.where(login: login).first_or_create!( | ||
name: name, | ||
#avatar_url: avatar_url | ||
) | ||
puts "here: "+token | ||
# ... and redirect to client app. | ||
redirect_to "#{issuer}?token=#{token}" | ||
rescue StandardError => error | ||
redirect_to "#{issuer}?error=#{error.message}" | ||
end | ||
|
||
private | ||
|
||
def issuer | ||
ENV['CLIENT_URL'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class DashboardsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_dashboard, only: [:show, :update, :destroy] | ||
|
||
# GET /dashboards | ||
def index | ||
@dashboards = Dashboard.all | ||
|
||
render json: @dashboards | ||
end | ||
|
||
# GET /dashboards/1 | ||
def show | ||
render json: @dashboard | ||
end | ||
|
||
# POST /dashboards | ||
def create | ||
@dashboard = Dashboard.new(dashboard_params) | ||
|
||
if @dashboard.save | ||
render json: @dashboard, status: :created, location: @dashboard | ||
else | ||
render json: @dashboard.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /dashboards/1 | ||
def update | ||
if @dashboard.update(dashboard_params) | ||
render json: @dashboard | ||
else | ||
render json: @dashboard.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /dashboards/1 | ||
def destroy | ||
@dashboard.destroy | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_dashboard | ||
@dashboard = Dashboard.find(params[:id]) | ||
end | ||
|
||
# Only allow a trusted parameter "white list" through. | ||
def dashboard_params | ||
params.require(:dashboard).permit(:user_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class UsersController < ApplicationController | ||
before_action :set_user, only: [:show, :update, :destroy] | ||
|
||
# GET /users | ||
def index | ||
@users = User.all | ||
|
||
render json: @users | ||
end | ||
|
||
# GET /users/1 | ||
def show | ||
render json: @user | ||
end | ||
|
||
# POST /users | ||
def create | ||
@user = User.new(user_params) | ||
|
||
if @user.save | ||
render json: @user, status: :created, location: @user | ||
else | ||
render json: @user.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /users/1 | ||
def update | ||
if @user.update(user_params) | ||
render json: @user | ||
else | ||
render json: @user.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /users/1 | ||
def destroy | ||
@user.destroy | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_user | ||
@user = User.find(params[:id]) | ||
end | ||
|
||
# Only allow a trusted parameter "white list" through. | ||
def user_params | ||
params.require(:user).permit(:login, :name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Dashboard < ApplicationRecord | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class User < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
import React from 'react'; | ||
|
||
import React, { Component } from 'react'; | ||
import { getQueryParams } from "./utility/urlUtility" | ||
import './App.css'; | ||
|
||
function App() { | ||
import Login from'./component/login' | ||
class App extends Component { | ||
|
||
constructor(){ | ||
super() | ||
this.state = { token: getQueryParams().token } | ||
} | ||
render(){ | ||
|
||
return ( | ||
<div className="App"> | ||
Yo! | ||
|
||
{!this.state.token ? <Login /> : "" } | ||
|
||
</div> | ||
); | ||
);} | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react' | ||
|
||
|
||
export default ()=>{ | ||
return ( <a href={`https://github.com/login/oauth/authorize?client_id=${process.env.REACT_APP_CLIENT_ID}`} >log in w/ github</a>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function getQueryParams() { | ||
const query = window.location.search.substring(1); | ||
const pairs = query.split('&').map((str) => str.split('=')); | ||
return pairs.reduce((memo, pair) => { | ||
memo[pair[0]] = pair[1]; | ||
return memo; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
Rails.application.routes.draw do | ||
resources :dashboards | ||
resources :users | ||
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html | ||
get "/test" => "test#new" | ||
get "/auth/google/callback" => "session#googleAuth" | ||
#get "/auth/google/callback" => "session#googleAuth" | ||
get '/auth/github', to: 'authentication#github', format: false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateUsers < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :users do |t| | ||
t.string :login | ||
t.string :name | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateDashboards < ActiveRecord::Migration[6.0] | ||
def change | ||
create_table :dashboards do |t| | ||
t.references :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Oops, something went wrong.