How to check and store whether user is logged in correctly?

Tom

Is there a better way of checking whether a user is logged in? Because I use the following approach for multiple apps and serving them somehow causes disparities, since it confuses the current app's item with other app's items.

I check whether a user is logged in like this:

constructor(private afAuth: AngularFireAuth, private router: Router, private db: AngularFirestore) {
    this.userData = new ReplaySubject<UserDetails>();
    afAuth.auth.onAuthStateChanged(user => {
      if (user) {
        this.user = user;
        const local = localStorage.getItem('user');
        if (local !== null) {
          this.userData.next(JSON.parse(localStorage.getItem('user')));
        } else {
          this.fetchUserData();
        }
      } else {
        localStorage.setItem('user', null);
      }
  });
}

get isLoggedIn(): boolean {
    const user = localStorage.getItem('user');
    return user !== 'null';
}
Frank van Puffelen

If each app is served from its own domain, then each will have its own localStorage and there can't be any conflict/confusion between them.

If you're serving multiple apps from the same domain, you'll have to use a unique name in the local storage for each app. Something like localStorage.setItem('app1_user', null) vs localStorage.setItem('app2_user', null).

But note that Firebase Authentication only has a single authenticated user per domain. So if you're serving multiple apps from the same domain, the user is (according to Firebase Authentication) signed in to all of them (or to none of them) at the same time.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to apply check in jsp whether current user is logged in or not

分類Dev

Checking whether element appears on page after form is submitted to check whether user has logged in

分類Dev

How to check whether a user is authenticated or not in Firebase?

分類Dev

How to check if a user is logged in via Facebook in Firebase Auth

分類Dev

How to check if user has logged-in for the first time?

分類Dev

How to check user if logged in My_Controller at CodeIgniter

分類Dev

Conditional check if user is logged in Umbraco

分類Dev

Check if user is logged in or not using firebase in react

分類Dev

How do I check if user is 'logged in' in a react component when using laravel authentication?

分類Dev

How to check whether an id is equal to the user's id and conditionally show buttons in react js?

分類Dev

how to check if already logged in Facebook in ios?

分類Dev

How to decide whether to store fields in indexes in RavenDB?

分類Dev

Header stays the same after I check if a user is logged in

分類Dev

How to change the navbar after a user is logged in?

分類Dev

Oracle ADF: How to enable a user to Stay Logged In

分類Dev

How to store the user in a session

分類Dev

How to check whether a string contains a substring in Kotlin?

分類Dev

How to check whether the system is FreeBSD in a python script?

分類Dev

How to check whether an item exists in the dynamodb table?

分類Dev

How to check whether file exist or not in given URL?

分類Dev

Room: how to check whether row exists

分類Dev

How to check whether a widget is currently clicked with the mouse?

分類Dev

How to check whether GNU Make supports Guile

分類Dev

How to check whether a string was already entered?

分類Dev

How to check whether a bucket exists in GCS with python

分類Dev

How to check whether an app or dmg is signed or not?

分類Dev

how to check whether a boolean is uninitialized in c++?

分類Dev

NGINX: check whether $remote_user equals to the first part of the location

分類Dev

Selenium (Java): Do both positive AND negative check whether page is loaded correctly

Related 関連記事

  1. 1

    How to apply check in jsp whether current user is logged in or not

  2. 2

    Checking whether element appears on page after form is submitted to check whether user has logged in

  3. 3

    How to check whether a user is authenticated or not in Firebase?

  4. 4

    How to check if a user is logged in via Facebook in Firebase Auth

  5. 5

    How to check if user has logged-in for the first time?

  6. 6

    How to check user if logged in My_Controller at CodeIgniter

  7. 7

    Conditional check if user is logged in Umbraco

  8. 8

    Check if user is logged in or not using firebase in react

  9. 9

    How do I check if user is 'logged in' in a react component when using laravel authentication?

  10. 10

    How to check whether an id is equal to the user's id and conditionally show buttons in react js?

  11. 11

    how to check if already logged in Facebook in ios?

  12. 12

    How to decide whether to store fields in indexes in RavenDB?

  13. 13

    Header stays the same after I check if a user is logged in

  14. 14

    How to change the navbar after a user is logged in?

  15. 15

    Oracle ADF: How to enable a user to Stay Logged In

  16. 16

    How to store the user in a session

  17. 17

    How to check whether a string contains a substring in Kotlin?

  18. 18

    How to check whether the system is FreeBSD in a python script?

  19. 19

    How to check whether an item exists in the dynamodb table?

  20. 20

    How to check whether file exist or not in given URL?

  21. 21

    Room: how to check whether row exists

  22. 22

    How to check whether a widget is currently clicked with the mouse?

  23. 23

    How to check whether GNU Make supports Guile

  24. 24

    How to check whether a string was already entered?

  25. 25

    How to check whether a bucket exists in GCS with python

  26. 26

    How to check whether an app or dmg is signed or not?

  27. 27

    how to check whether a boolean is uninitialized in c++?

  28. 28

    NGINX: check whether $remote_user equals to the first part of the location

  29. 29

    Selenium (Java): Do both positive AND negative check whether page is loaded correctly

ホットタグ

アーカイブ