Skip to content

Commit 683ba04

Browse files
committed
Soft landing for non-post omniauth requests
Previously any request to `/users/<id>` would trigger a log in flow if you're not logged in. In omniauth/omniauth#1010 (or somewhere around there) omniauth removed the ability to use a GET request to log in at all. Now all login requests must be done via GET. This means you cannot redirect to a login endpoint, you must drop the user off somewhere there is a form and they must manually click it to trigger a POST. That's what this PR does. If you try to edit or view your user account without being logged in today: it will give you a 404 error page with a cryptic error message. ``` Not found. Authentication passthru. ``` (Which I think omniauth could improve A LOT, FWIW). Anywhoo, to move forward I'm replacing the prior `authenticate_user!` before filter with one that checks if the request is a post. If it is, then it will directly authenticate the user. Otherwise we drop them on a "soft" page that tells them they must first login. It's not perfect, I'm sure there will be edge cases, but it works.
1 parent ab204d7 commit 683ba04

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

app/controllers/users_controller.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
# frozen_string_literal: true
22

33
class UsersController < ApplicationController
4-
before_action :authenticate_user!, only: [:edit, :update, :destroy]
4+
before_action :omniauth_workaround_authenticate_user!, only: [:edit, :update, :destroy]
5+
6+
def omniauth_workaround_authenticate_user!
7+
return if current_user
8+
9+
# Omniauth changed the requirement so calling `authenticate_user!`
10+
# must be done through a post request, if someone tries
11+
# one of these actions that they must be logged in for
12+
# we have to tell them to log in first.
13+
if request.post?
14+
authenticate_user!
15+
else
16+
redirect_to user_path(id: params[:id]), alert: "Please log in to access your requested page"
17+
end
18+
end
519

620
def show
721
@user = User.find(params[:id])
22+
23+
# You must be logged in to see your own user account, enforced in the view
824
if @user.private?
9-
redirect_to root_path unless current_user && @user == current_user
25+
redirect_to root_path, alert: "User is private" unless current_user && @user == current_user
1026
end
1127
end
1228

app/views/users/show.html.slim

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
- if current_user.blank?
22
section.user-logged-out
3-
h1= link_to 'Login to view your account', user_github_omniauth_authorize_path, method: :post
3+
.subpage-content-wrapper
4+
section.subpage-content-header
5+
h1.subpage-primary-title You must be logged in to see this content
6+
h2.subpage-secondary-title.user-email Please log in
47

5-
- if current_user && current_user == @user
8+
section.user-settings.content-section style="text-align: center"
9+
= link_to user_github_omniauth_authorize_path(origin: edit_user_url(@user)), class: 'button', method: :post
10+
| Log in
11+
12+
- elsif current_user != @user
13+
section.user-logged-out
14+
.subpage-content-wrapper
15+
section.subpage-content-header
16+
h1.subpage-primary-title You may only view your own user account
17+
h2.subpage-secondary-title.user-email
18+
19+
section.user-settings.content-section style="text-align: center"
20+
= link_to user_path(current_user), class: 'button'
21+
| View my user account
22+
23+
- elsif current_user && current_user == @user
624
.subpage-content-wrapper
725
section.subpage-content-header
826
h1.subpage-primary-title= @user.github
@@ -61,3 +79,5 @@
6179
= f.check_box :favorite_languages, { multiple: true }, language, nil
6280
= f.label language, for: "user_favorite_languages_#{language.downcase}"
6381
p= button_tag "Save Favorite Languages", class: "button full-width-action"
82+
- else
83+
p If you see this something went really wrong. Please open an issue with reproduction steps

0 commit comments

Comments
 (0)