Skip to content

Auto-attach push on Auth state change (user-gesture preservation)#3

Merged
anilcancakir merged 5 commits into
masterfrom
MNOT-1
Apr 13, 2026
Merged

Auto-attach push on Auth state change (user-gesture preservation)#3
anilcancakir merged 5 commits into
masterfrom
MNOT-1

Conversation

@anilcancakir
Copy link
Copy Markdown
Contributor

Summary

Implemented auth auto-attach in NotificationServiceProvider.boot() — hooks Auth.stateNotifier to automatically initialize push on login (with configurable user ID prefix) and cleanup on logout, solving the browser user-gesture requirement for web push permission prompts.

Changes Made

  • lib/src/providers/notification_service_provider.dart:55-91 — Added auth state listener after push driver init: reads 3 new config keys, registers Auth.stateNotifier.addListener() with dedup via lastUserId, handles login (initializePush + optional requestPushPermission) and logout (stopPolling + logoutPush) paths, full try/catch error handling
  • assets/stubs/install/notification_config.stub:8-10 — Added auto_attach_on_auth: true, external_id_prefix: 'user_', auto_request_permission: true defaults under push section
  • test/providers/notification_service_provider_test.dart:90-270 — Added 6 tests in auth auto-attach group: prefixed ID on login, requestPushPermission when enabled, no permission when disabled, logout cleanup, dedup same user ID, no listener when disabled

Tests

  • test/providers/notification_service_provider_test.dart — 11 tests (5 existing + 6 new), all passing
  • Test command: flutter test --coverage — 264 passed, 0 failed
  • flutter analyze --no-fatal-infos — No issues found
  • dart format . — 0 changed

Execution Stats

  • Complexity: normal
  • Waves: 3/3
  • Steps: 4/4 (0 failed)
  • Tier distribution: 2 quick, 2 mid, 0 senior
  • Escalations: 0
  • Verification: plan-code-review APPROVED

Accumulated Wisdom

  • Auth.stateNotifier is ValueNotifier<int> — bumps on login/logout/restore, fires synchronously in gesture context
  • Auth.fake() + Auth.guard().login(data, user) bumps stateNotifier automatically in tests
  • Async listener inside addListener needs await Future.delayed(Duration.zero) in tests to pump microtask queue
  • Authenticatable is a mixin on Model — test users need table, resource, fillable overrides + user.exists = true

Decisions

  • Wrapped entire listener body in single try/catch (reviewer flagged that logout path was unprotected in initial implementation)
  • Config keys use literal defaults in stub (not template variables) — these are user-customizable after publish, no install wizard prompts needed
  • Listener placed outside the onesignal if-block — works regardless of push driver type

Open Questions

  • None — implementation matches the task description's reference pattern exactly

Closes #2

Generated by Kodizm AI

anilcancakir and others added 3 commits April 13, 2026 14:32
Hook Auth.stateNotifier in NotificationServiceProvider.boot() to
auto-initialize push on login and cleanup on logout, solving the
browser user-gesture requirement for web push permission prompts.

Co-Authored-By: Kodizm <ai@kodizm.com>
Add auto_attach_on_auth, external_id_prefix, and auto_request_permission
defaults to the notification config stub template.

Co-Authored-By: Kodizm <ai@kodizm.com>
Update CHANGELOG, README feature table, configuration reference, and
service provider architecture doc with new push config keys and
auth auto-attach behavior.

Co-Authored-By: Kodizm <ai@kodizm.com>
Copilot AI review requested due to automatic review settings April 13, 2026 14:57
@sentry
Copy link
Copy Markdown

sentry Bot commented Apr 13, 2026

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an auth-state-driven lifecycle hook to the notifications service provider so push can be initialized immediately on login (preserving browser user-gesture context for web permission prompts) and cleaned up on logout, with new config keys and accompanying documentation/tests.

Changes:

  • Hook Auth.stateNotifier in NotificationServiceProvider.boot() to auto-initialize push on login and run cleanup on logout (configurable prefix + auto-permission request, with dedup).
  • Add new push config defaults (auto_attach_on_auth, external_id_prefix, auto_request_permission) to install stub and document them.
  • Add provider tests covering login attach, optional permission request, logout cleanup, dedup, and opt-out behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lib/src/providers/notification_service_provider.dart Adds auth state listener to auto-attach/cleanup push around login/logout.
assets/stubs/install/notification_config.stub Introduces default config keys for auth auto-attach behavior.
test/providers/notification_service_provider_test.dart Adds tests for auth auto-attach behavior and related helpers/mocks.
README.md Mentions the new auth auto-attach feature in the feature list.
doc/getting-started/configuration.md Documents new push config keys and their meanings/defaults.
doc/architecture/service-provider.md Documents the new auth auto-attach behavior and provides an example snippet.
CHANGELOG.md Adds an Unreleased entry describing the new feature and config keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/src/providers/notification_service_provider.dart Outdated
Comment thread lib/src/providers/notification_service_provider.dart
Comment thread doc/architecture/service-provider.md
Comment thread test/providers/notification_service_provider_test.dart Outdated
Comment thread test/providers/notification_service_provider_test.dart
anilcancakir and others added 2 commits April 13, 2026 15:18
…ration

Address PR review feedback: chain async listener work via Future queue to
prevent overlapping runs on rapid auth state changes, add static
_authListenerAttached flag for idempotent registration across hot restarts,
remove redundant inner tearDown, and replace wall-clock Future.delayed with
deterministic _pumpEventQueue helper in tests.

Co-Authored-By: Kodizm <ai@kodizm.com>
Fix incomplete example snippet in service-provider.md — add missing
variable definitions, lastUserId dedup guard, and serialized queue.
Add Fixed section to CHANGELOG describing the three improvements.

Co-Authored-By: Kodizm <ai@kodizm.com>
@anilcancakir anilcancakir merged commit fed0f06 into master Apr 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-attach push on Auth state change (user-gesture preservation)

2 participants