Allow account creation without slack account

This commit is contained in:
Max Wofford
2025-03-10 11:39:35 -04:00
parent 3c2ac27cda
commit 976741b391
3 changed files with 35 additions and 22 deletions

View File

@@ -28,14 +28,26 @@ class SessionsController < ApplicationController
end
def email
email_address = EmailAddress.find_by(email: params[:email].downcase)
email = params[:email].downcase
email_address = EmailAddress.find_by(email: email)
if email_address
# Existing user - send sign in link
token = email_address.user.create_email_signin_token
AuthMailer.sign_in_email(email_address, token).deliver_later
redirect_to root_path(sign_in_email: true), notice: "Check your email for a sign-in link!"
else
redirect_to root_path, alert: "Email not found. Please sign in with Slack first."
# New user - create account and send sign in link
user = User.create!(
username: email.split("@").first,
slack_uid: SecureRandom.uuid # Generate a unique ID for email users
)
email_address = user.email_addresses.create!(email: email)
token = user.create_email_signin_token
AuthMailer.sign_in_email(email_address, token).deliver_later
redirect_to root_path(sign_in_email: true), notice: "Welcome! Check your email for a sign-in link!"
end
end

View File

@@ -2,7 +2,7 @@ class User < ApplicationRecord
has_paper_trail
encrypts :slack_access_token
validates :slack_uid, presence: true, uniqueness: true
validates :slack_uid, uniqueness: true, allow_nil: true
validates :username, presence: true
has_many :heartbeats
@@ -179,6 +179,10 @@ class User < ApplicationRecord
heartbeats.where(project: active_project).duration_seconds
end
def most_recent_direct_entry_heartbeat
heartbeats.where(source_type: :direct_entry).order(time: :desc).first
end
def create_email_signin_token
sign_in_tokens.create!(auth_type: :email)
end

View File

@@ -23,25 +23,22 @@
<div class="auth-options">
<%= link_to "Sign in with Slack", slack_auth_path, class: "auth-button slack" %>
<% if Rails.env.development? %>
<div class="auth-divider">or</div>
<%= form_tag email_auth_path, class: "email-auth-form" do %>
<div class="field">
<%= email_field_tag :email, nil, placeholder: "Enter your email", required: true %>
</div>
<%= submit_tag "Send sign-in link", class: "auth-button email" %>
<div class="auth-divider">or</div>
<%= form_tag email_auth_path, class: "email-auth-form" do %>
<div class="field">
<%= email_field_tag :email, nil, placeholder: "Enter your email", required: true %>
</div>
<%= submit_tag "Send sign-in link", class: "auth-button email" %>
<% end %>
<% if params[:sign_in_email] %>
<div class="auth-success">
Check your email for a sign-in link!
</div>
<% dev_tool do %>
Because you're on localhost, <%= link_to "click here to view the email", letter_opener_web_path %>
<% end %>
<% if params[:sign_in_email] %>
<div class="auth-success">
Check your email for a sign-in link!
</div>
<% dev_tool do %>
Because you're on localhost, <%= link_to "click here to view the email", letter_opener_web_path %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>