Skip to content

Commit

Permalink
Add basic self hosted onboarding (#1177)
Browse files Browse the repository at this point in the history
* Add basic self hosted onboarding

* Lint fix

* Normalize translations
  • Loading branch information
zachgoll authored Sep 13, 2024
1 parent 0149ca4 commit d3d9af8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
10 changes: 9 additions & 1 deletion app/controllers/concerns/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def authenticate_user!
if user = User.find_by(id: session[:user_id])
Current.user = user
else
redirect_to new_session_url
if self_hosted_first_login?
redirect_to new_registration_url
else
redirect_to new_session_url
end
end
end

Expand All @@ -36,4 +40,8 @@ def logout
def set_last_login_at
Current.user.update(last_login_at: DateTime.now)
end

def self_hosted_first_login?
Rails.application.config.app_mode.self_hosted? && User.count.zero?
end
end
6 changes: 5 additions & 1 deletion app/controllers/concerns/invitable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Invitable

private
def invite_code_required?
ENV["REQUIRE_INVITE_CODE"] == "true"
self_hosted? ? Setting.require_invite_for_signup : ENV["REQUIRE_INVITE_CODE"] == "true"
end

def self_hosted?
Rails.application.config.app_mode.self_hosted?
end
end
6 changes: 5 additions & 1 deletion app/controllers/concerns/self_hostable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ module SelfHostable
extend ActiveSupport::Concern

included do
helper_method :self_hosted?
helper_method :self_hosted?, :self_hosted_first_login?
end

private
def self_hosted?
Rails.configuration.app_mode.self_hosted?
end

def self_hosted_first_login?
self_hosted? && User.count.zero?
end
end
7 changes: 7 additions & 0 deletions app/models/demo/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def initialize
end

def reset_and_clear_data!
reset_settings!
clear_data!
create_user!

Expand All @@ -14,6 +15,7 @@ def reset_and_clear_data!

def reset_data!
Family.transaction do
reset_settings!
clear_data!
create_user!

Expand Down Expand Up @@ -52,12 +54,17 @@ def reset_family!
end

def clear_data!
InviteCode.destroy_all
User.find_by_email("[email protected]")&.destroy
ExchangeRate.destroy_all
Security.destroy_all
Security::Price.destroy_all
end

def reset_settings!
Setting.destroy_all
end

def create_user!
family.users.create! \
email: "[email protected]",
Expand Down
8 changes: 8 additions & 0 deletions app/views/registrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<%
header_title t(".title")
%>

<% if self_hosted_first_login? %>
<div class="fixed inset-0 w-full h-fit bg-gray-25 p-5 border-b border-alpha-black-200 flex flex-col gap-3 items-center text-center mb-12">
<h2 class="font-bold text-xl"><%= t(".welcome_title") %></h2>
<p class="text-gray-500 text-sm"><%= t(".welcome_body") %></p>
</div>
<% end %>

<%= styled_form_with model: @user, url: registration_path, class: "space-y-4" do |form| %>
<%= auth_messages form %>
<%= form.email_field :email, autofocus: false, autocomplete: "email", required: "required", placeholder: "[email protected]", label: true %>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/views/registrations/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ en:
success: You have signed up successfully.
new:
title: Create an account
welcome_body: To get started, you must sign up for a new account. You will
then be able to configure additional settings within the app.
welcome_title: Welcome to Self Hosted Maybe!

0 comments on commit d3d9af8

Please sign in to comment.