T
T
Tikhon Ermakov2020-08-08 16:35:34
Laravel
Tikhon Ermakov, 2020-08-08 16:35:34

How to implement SPA authorization?

Good afternoon!

Application with Sanctum and React.

0. Send a Get request to /sanctum/csrf-cookie to initialize CSRF protection.
1. We send user data to the authenticate method and, if successful, we get a cookie.

1.1 Whether we are authorized or not, cookies will be in any case. It is important.

2. We made dispatch on the frontend and in the application state we indicated that we were authorized.

3. When reloading the page, how should we check or know that the user is logged in?

In my opinion, there are several options...

1. Send a Get request to /api/user .
2. In the blade, create a global variable, where the auth directives@endauth we will write the state. for an example it is possible so: window.isAuthenticated: true or false; window.user = auth::user;
3. Climb into localStorage and mark something there.

How to implement it correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question