-
-
Notifications
You must be signed in to change notification settings - Fork 96
feat: extract user dependent main nav to client side #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
salzig
wants to merge
8
commits into
master
Choose a base branch
from
feat/extract_user_dependent_main_nav_to_client_side
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+252
−44
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45d62a1
fix: small header menu item alignment fix
salzig ced4e1a
feat: set cookie with user data
salzig ad46083
feat: replace serverside rendered user nav with client side rendered
salzig d44b3f1
fix: depend user-data cookie on session lifetime
salzig b66cb17
fix: cover possible session states
salzig 27c5c28
fix: set user_cookie when missing but session cookie stil valid
salzig a2b200c
fix: use auth_path, cause csrf is domain based
salzig 73e8904
fix: fallback with full host, if partial is nil
salzig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Minimal controller registry — mirrors Stimulus conventions without the dependency. | ||
| // Files in controllers/ assign to Controllers['name'] = class { connect(el) {} } | ||
| const Controllers = {}; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function() { | ||
| document.querySelectorAll('[data-controller]').forEach(function(el) { | ||
| const name = el.dataset.controller; | ||
| if (Controllers[name]) new Controllers[name](el).connect(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Reads the _on_ruby_user data cookie (set by UserHandling#sign_in) and swaps the | ||
| // anonymous login button for the profile dropdown without a server round-trip. | ||
| Controllers['nav'] = class { | ||
| constructor(el) { this.el = el; } | ||
|
|
||
| connect() { | ||
| const user = this.readUserCookie(); | ||
| if (user) this.renderUserNav(user); | ||
| } | ||
|
|
||
| readUserCookie() { | ||
| const match = document.cookie.match(/(?:^|;\s*)_on_ruby_user=([^;]+)/); | ||
| if (!match) return null; | ||
| try { return JSON.parse(decodeURIComponent(match[1])); } | ||
| catch (e) { return null; } | ||
| } | ||
|
|
||
| renderUserNav(user) { | ||
| const template = this.el.querySelector('template'); | ||
| if (!template) return; | ||
|
|
||
| const frag = template.content.cloneNode(true); | ||
| const avatar = frag.querySelector('[data-avatar]'); | ||
| avatar.src = user.image_path; | ||
| avatar.alt = user.name; | ||
| frag.querySelector('[data-profile]').href = user.profile_path; | ||
| frag.querySelector('[data-edit]').href = user.edit_path; | ||
| frag.querySelector('[data-logout]').href = user.logout_path; | ||
|
|
||
| const show = (sel) => { const node = frag.querySelector(sel); if (node) node.hidden = false; }; | ||
|
JoschkaSchulz marked this conversation as resolved.
|
||
| if (user.is_admin) show('[data-admin]'); | ||
| if (user.is_super_admin) show('[data-super-admin]'); | ||
|
|
||
| this.el.querySelector('[data-login]').replaceWith(frag); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'spec_helper' | ||
|
|
||
| describe ApplicationController do | ||
| controller do | ||
| def index | ||
| render plain: 'ok' | ||
| end | ||
|
|
||
| def create | ||
| authenticate! | ||
| render plain: 'ok' unless performed? | ||
| end | ||
| end | ||
|
|
||
| let(:user) { create(:user, :with_authorization) } | ||
|
|
||
| context 'state 1 — fully anonymous' do | ||
| it 'does not set _on_ruby_user' do | ||
| get :index | ||
| expect(cookies['_on_ruby_user']).to be_nil | ||
| end | ||
| end | ||
|
|
||
| context 'state 2 — orphaned _on_ruby_user, no auth' do | ||
| before { cookies['_on_ruby_user'] = '{"slug":"ghost","name":"Ghost"}' } | ||
|
|
||
| it 'clears _on_ruby_user when authenticate! is triggered' do | ||
| post :create | ||
| expect(cookies['_on_ruby_user']).to be_blank | ||
| end | ||
| end | ||
|
|
||
| context 'state 3 — valid session, no remember_me' do | ||
| before { session[:user_id] = user.id } | ||
|
|
||
| it 'authenticates the user via session' do | ||
| post :create | ||
| expect(response).to have_http_status(:ok) | ||
| end | ||
|
|
||
| it 'does not set remember_me' do | ||
| post :create | ||
| expect(cookies[:remember_me]).to be_nil | ||
| end | ||
|
|
||
| it 're-sets _on_ruby_user when absent' do | ||
| get :index | ||
| expect(cookies['_on_ruby_user']).to be_present | ||
| end | ||
| end | ||
|
|
||
| context 'state 4 — remember_me valid, _on_ruby_user absent (migration)' do | ||
| before { cookies.signed[:remember_me] = [user.id, user.salt] } | ||
|
|
||
| it 're-sets _on_ruby_user on the next request' do | ||
| get :index | ||
| expect(cookies['_on_ruby_user']).to be_present | ||
| end | ||
| end | ||
|
|
||
| context 'state 5 — returning user, both permanent cookies present' do | ||
| before do | ||
| cookies.signed[:remember_me] = [user.id, user.salt] | ||
| cookies['_on_ruby_user'] = '{"slug":"testuser","name":"Test"}' | ||
| end | ||
|
|
||
| it 'does not overwrite _on_ruby_user' do | ||
| original = cookies['_on_ruby_user'] | ||
| get :index | ||
| expect(cookies['_on_ruby_user']).to eq(original) | ||
| end | ||
| end | ||
|
|
||
| context 'state 6 — stale remember_me, _on_ruby_user absent' do | ||
| before do | ||
| cookies.signed[:remember_me] = [user.id, user.salt] | ||
| user.destroy | ||
| end | ||
|
|
||
| it 'does not crash' do | ||
| expect { get :index }.not_to raise_error | ||
| end | ||
|
|
||
| it 'leaves _on_ruby_user absent' do | ||
| get :index | ||
| expect(cookies['_on_ruby_user']).to be_blank | ||
| end | ||
| end | ||
|
|
||
| context 'state 7 — stale remember_me and _on_ruby_user, user gone' do | ||
| before do | ||
| cookies.signed[:remember_me] = [user.id, user.salt] | ||
| cookies['_on_ruby_user'] = '{"slug":"testuser","name":"Test"}' | ||
| user.destroy | ||
| end | ||
|
|
||
| it 'clears _on_ruby_user when authenticate! is triggered' do | ||
| post :create | ||
| expect(cookies['_on_ruby_user']).to be_blank | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.