FIX: better error message if invite is expired (#10783)

FIX: better error message if invite is expired (#10783)

https://meta.discourse.org/t/invite-token-is-invalid/165270/5?u=techapj

diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index ed8255e..530f9e7 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -18,23 +18,23 @@ class InvitesController < ApplicationController
     expires_now
 
     invite = Invite.find_by(invite_key: params[:id])
+    if invite.present? && !invite.expired? && !invite.redeemed?
+      store_preloaded("invite_info", MultiJson.dump(
+        invited_by: UserNameSerializer.new(invite.invited_by, scope: guardian, root: false),
+        email: invite.email,
+        username: UserNameSuggester.suggest(invite.email),
+        is_invite_link: invite.is_invite_link?)
+      )
 
-    if invite.present? && !invite.expired?
-      if !invite.redeemed?
-        store_preloaded("invite_info", MultiJson.dump(
-          invited_by: UserNameSerializer.new(invite.invited_by, scope: guardian, root: false),
-          email: invite.email,
-          username: UserNameSuggester.suggest(invite.email),
-          is_invite_link: invite.is_invite_link?)
-        )
-
-        render layout: 'application'
+      render layout: 'application'
+    else
+      flash.now[:error] = if invite.present? && invite.expired?
+        I18n.t('invite.expired', base_url: Discourse.base_url)
+      elsif invite.present? && invite.redeemed?
+        I18n.t('invite.not_found_template', site_name: SiteSetting.title, base_url: Discourse.base_url)
       else
-        flash.now[:error] = I18n.t('invite.not_found_template', site_name: SiteSetting.title, base_url: Discourse.base_url)
-        render layout: 'no_ember'
+        I18n.t('invite.not_found', base_url: Discourse.base_url)
       end
-    else
-      flash.now[:error] = I18n.t('invite.not_found', base_url: Discourse.base_url)
       render layout: 'no_ember'
     end
   end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index da611f8..f7f7160 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -205,6 +205,7 @@ en:
       <<: *errors
 
   invite:
+    expired: "Your invite token is expired. Please <a href='%{base_url}/about'>contact staff</a>."
     not_found: "Your invite token is invalid. Please <a href='%{base_url}/about'>contact staff</a>."
     not_found_json: "Your invite token is invalid. Please contact staff."
     not_found_template: |
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index 031ed10..995ded2 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -29,7 +29,7 @@ describe InvitesController do
 
       body = response.body
       expect(body).to_not have_tag(:script, with: { src: '/assets/application.js' })
-      expect(CGI.unescapeHTML(body)).to include(I18n.t('invite.not_found', base_url: Discourse.base_url))
+      expect(CGI.unescapeHTML(body)).to include(I18n.t('invite.expired', base_url: Discourse.base_url))
     end
 
     it "renders the accept invite page if invite exists" do

GitHub sha: ff44515a

This commit appears in #10783 which was approved by CvX. It was merged by techAPJ.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/invite-token-is-invalid/165270/10