How to handle authentication in Angular JS application

前端 未结 4 680
面向向阳花
面向向阳花 2020-12-04 20:23

I am implementing an auth system in my angular js app.

What I am planning it like below:

  1. Get user info(name and pass from login form)
  2. Check w
4条回答
  •  一整个雨季
    2020-12-04 20:46

    I am not sure about your backend, but this is how I would do it

    • Create a separate login page (dedicated url not angular sub view or modal dialog).
    • If the user is not authenticated redirect to this login page. This is done by server redirects. This page may or may not use angular framework, as it just involves sending a user\password to server.
    • Make a POST (not AJAX request) from the login page, and verify on server.
    • On the server set the auth cookie. (Different frameworks do it differently. ASP.Net sets form authentication cookie.)
    • Once the user is authenticated redirect user to the actual angular app and load all its components.

    This saves any code require to manage authentication on client side in Angular. If the user lands on this page he is authenticated and has the cookie.

    Also default browser behavior is to send all cookies associated with a domain with each request, so you don't have to worry if angular is sending some cookie or not.

提交回复
热议问题