How to maintain login status in a PWA initially loaded via Safari 14/iOS 14?

前端 未结 2 1733
抹茶落季
抹茶落季 2021-01-02 04:51

Our requirement is to have our users login to an app via a URL and, having added the app to their homescreen as a PWA, maintain that logged-in status, so that a second l

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-02 05:38

    It can be done. Here's how we've succeeded in doing it:

    1. When the user initially logs in to the app in the browser, we generate a UID on the server.
    2. We pair this UID with the username in a server file (access.data).
    3. We generate the web app manifest dynamically. In it we set the start_url to the index page and append a query string incorporating the UID e.g. "start_url": "//index.html?accessID=".
    4. We create a cookie to verify that the app has been accessed e.g. access=granted.
    5. When the user accesses the app as an iOS PWA, the app looks for this cookie and doesn't find it (cunning ;) - we use one of the iOS deficiencies (not sharing cookies between Safari and the PWA) to defeat that same deficiency).
    6. The absence of the access cookie tells the app to extract the UID from the query string.
    7. It sends the UID back to the server, which looks for a match in access.data.
    8. If the server finds a match, it tells the app that the PWA user is already logged in and there's no need to again display the login screen. Mission accomplished!

    Note: Android/Chrome simply ignores the accessID in the query string - I was wrong in my question to imply that Android/Chrome requires an unmodified start_url.

提交回复
热议问题