Skip to content

Commit

Permalink
Create IT1 onboarding flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharydenton authored and aptos-bot committed May 7, 2022
1 parent 385f0da commit 1a4622e
Show file tree
Hide file tree
Showing 65 changed files with 827 additions and 180 deletions.
2 changes: 1 addition & 1 deletion ecosystem/platform/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"last 1 safari version"
]
},
"proxy": "http://localhost:8000",
"proxy": "http://localhost:3001",
"devDependencies": {
"@tailwindcss/forms": "^0.5.0",
"autoprefixer": "^10.4.4",
Expand Down
4 changes: 4 additions & 0 deletions ecosystem/platform/server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
!/tmp/storage/
!/tmp/storage/.keep

# Ignore asset builds.
/app/assets/builds

/public/assets

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/credentials.yml.enc

# Never commit this dangerous file :-)
.env
1 change: 1 addition & 0 deletions ecosystem/platform/server/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.1.2
2 changes: 2 additions & 0 deletions ecosystem/platform/server/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ gem 'sassc-rails'
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem 'tailwindcss-rails', '~> 2.0'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
Expand Down
8 changes: 8 additions & 0 deletions ecosystem/platform/server/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.4-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.4-x86_64-linux)
racc (~> 1.4)
oauth2 (1.4.9)
faraday (>= 0.17.3, < 3.0)
jwt (>= 1.0, < 3.0)
Expand Down Expand Up @@ -342,6 +344,10 @@ GEM
stimulus-rails (1.0.4)
railties (>= 6.0.0)
strscan (3.0.1)
tailwindcss-rails (2.0.8-arm64-darwin)
railties (>= 6.0.0)
tailwindcss-rails (2.0.8-x86_64-linux)
railties (>= 6.0.0)
thor (1.2.1)
tilt (2.0.10)
timeout (0.2.0)
Expand Down Expand Up @@ -371,6 +377,7 @@ GEM

PLATFORMS
arm64-darwin-21
x86_64-linux

DEPENDENCIES
activeadmin
Expand Down Expand Up @@ -403,6 +410,7 @@ DEPENDENCIES
selenium-webdriver
sprockets-rails
stimulus-rails
tailwindcss-rails (~> 2.0)
turbo-rails
tzinfo-data
web-console
Expand Down
2 changes: 1 addition & 1 deletion ecosystem/platform/server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ run-generate:
bundle exec rails generate $(RUN_ARGS)

run-rails:
bundle exec puma -t 1:1 -b tcp://0.0.0.0:3000
bundle exec puma -t 1:1 -b tcp://0.0.0.0:3001

run-server:
bundle exec rails s
Expand Down
1 change: 1 addition & 0 deletions ecosystem/platform/server/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
//= link_tree ../builds
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 2 additions & 8 deletions ecosystem/platform/server/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@
*
*= require_tree .
*= require_self
*= stub "application.tailwind"
*= stub "active_admin"
*/

.yield-wrapper-outer {
display:flex;
}

.yield-wrapper-inner {
margin: auto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def set_csrf_cookie
}
end

def after_sign_in_path_for(resource)
if resource.is_root?
admin_dashboard_path
else
'/'
end
def after_sign_in_path_for(user)
stored_location_for(user) ||
if user.email.nil?
onboarding_email_path
else
overview_index_path
end
end

def admin_access_denied(_exception)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

class It1ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :set_it1_profile, only: %i[show edit update destroy]

# GET /it1_profiles/new
def new
@it1_profile = It1Profile.new
end

# GET /it1_profiles/1/edit
def edit; end

# POST /it1_profiles or /it1_profiles.json
def create
params = it1_profile_params
params[:user] = current_user
@it1_profile = It1Profile.new(params)

respond_to do |format|
if @it1_profile.save
format.html { redirect_to overview_index_path, notice: 'IT1 application was successfully created.' }
else
format.html { render :new, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /it1_profiles/1 or /it1_profiles/1.json
def update
respond_to do |format|
if @it1_profile.update(it1_profile_params)
format.html { redirect_to overview_index_path, notice: 'IT1 application was successfully updated.' }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_it1_profile
@it1_profile = It1Profile.find(params[:id])
head :forbidden unless @it1_profile.user_id == current_user.id
end

# Only allow a list of trusted parameters through.
def it1_profile_params
params.fetch(:it1_profile, {}).permit(:consensus_key, :account_key, :network_key, :validator_address,
:validator_port, :metrics_port, :fullnode_address, :fullnode_port, :terms)
end
end
18 changes: 18 additions & 0 deletions ecosystem/platform/server/app/controllers/onboarding_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class OnboardingController < ApplicationController
def email
@oauth_username = current_user.authorizations.pluck(:username).first
@oauth_email = current_user.authorizations.pluck(:email).first
end

def email_update
email_params = params.require(:user).permit(:email)
if current_user.update(email_params)
current_user.send_confirmation_instructions
redirect_to overview_index_path
else
render :email, status: :unprocessable_entity
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class OverviewController < ApplicationController
before_action :authenticate_user!

def index; end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,26 @@

module Users
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def github
@user = User.from_omniauth(auth_data, current_user)
if @user.persisted?
@message = I18n.t 'devise.omniauth_callbacks.success', kind: 'Github'
sign_in(@user)
else
# TODO: make this bulletproof
@message = I18n.t 'devise.omniauth_callbacks.failure', kind: :github,
reason: @user.errors.full_messages.join("\n")
User.omniauth_providers.each do |provider|
define_method provider do
oauth_callback(provider)
end

# TODO: RENDER SOME RESPONSE!
# render json: { message: }
render 'api/users/show', formats: [:json]
end

def discord
private

def oauth_callback(provider)
@user = User.from_omniauth(auth_data, current_user)

if @user.persisted?
message = I18n.t 'devise.omniauth_callbacks.success', kind: 'Discord'
sign_in(@user)
sign_in_and_redirect @user, event: :authentication # this will throw if @user is not activated
set_flash_message(:notice, :success, kind: provider.to_s.titleize) if is_navigational_format?
else
# TODO: make this bulletproof
message = I18n.t 'devise.omniauth_callbacks.failure', kind: :discord,
reason: @user.errors.full_messages.join("\n")
raise 'Unable to persist user'
end

# TODO: RENDER SOME RESPONSE!
render json: { message: }
end

private

def auth_data
request.env['omniauth.auth']
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class WelcomeController < ApplicationController
def index; end

def it1; end
end
4 changes: 4 additions & 0 deletions ecosystem/platform/server/app/helpers/it1_profiles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module It1ProfilesHelper
end
4 changes: 4 additions & 0 deletions ecosystem/platform/server/app/helpers/onboarding_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module OnboardingHelper
end
4 changes: 4 additions & 0 deletions ecosystem/platform/server/app/helpers/overview_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module OverviewHelper
end
4 changes: 4 additions & 0 deletions ecosystem/platform/server/app/helpers/welcome_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module WelcomeHelper
end
5 changes: 2 additions & 3 deletions ecosystem/platform/server/app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
class ApplicationRecord < ActiveRecord::Base
primary_abstract_class

# ensure hex is blank, or length 66 + 0x{hex}
def self.validate_hex(field_name, allow_nil: true)
validates field_name, length: { is: 6 }, format: { with: /\A0x(?:[A-F0-9]{2}){32}\z/i }, allow_nil:
def self.validate_aptos_address(field_name, allow_nil: true)
validates field_name, format: { with: /\A0x[a-f0-9]{1,32}\z/i }, allow_nil:
end
end
17 changes: 17 additions & 0 deletions ecosystem/platform/server/app/models/it1_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class It1Profile < ApplicationRecord
belongs_to :user

validates :consensus_key, presence: true, uniqueness: true, format: { with: /\A0x[a-f0-9]{64}\z/i }
validates :account_key, presence: true, uniqueness: true, format: { with: /\A0x[a-f0-9]{64}\z/i }
validates :network_key, presence: true, uniqueness: true, format: { with: /\A0x[a-f0-9]{64}\z/i }

validates :validator_address, presence: true
validates :validator_port, presence: true, numericality: { only_integer: true }
validates :metrics_port, presence: true, numericality: { only_integer: true }

validates :fullnode_port, numericality: { only_integer: true }, allow_nil: true

validates :terms, acceptance: true
end
3 changes: 2 additions & 1 deletion ecosystem/platform/server/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ class User < ApplicationRecord
validates :username, uniqueness: { case_sensitive: false }, allow_nil: true
validates :email, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_nil: true

validate_hex :mainnet_address
validate_aptos_address :mainnet_address

has_many :authorizations, dependent: :destroy
has_one :it1_profile

# https://github.com/makandra/rails_state_machine
# TODO: this state machine
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1a4622e

Please sign in to comment.