Hi @jhull,
To be honest, we will be getting to these type of scenarios with ROAST and don’t have them implemented quite yet. However, it’s very important (especially, as I can imagine with users entering a lot of content). It can even become an issue if you have the user authenticated and they spend 4 hours generating content without saving.
With the first question, there are two ways to approach this. If you want the user to remain logged in, you could follow an approach similar to how they implement it in WordPress admin. You’d create an interval that pings a an endpoint (such as /api/v1/user
) that will re-fresh the session. This will do two things, first, it will maintain the session for the user. Second, and possibly most important, you can hook into this interval to see if the user is NOT authenticated, then prompt them with the login screen.
You can then allow the user to re-auth and not do any redirection so they save where they are at. Below is a little pseudo code for that:
// Import User API
export default {
mounted(){
setInterval( function(){
this.checkAuth();
}.bind(this), 30000);
},
methods: {
checkAuth(){
this.userAPI.get()
.then(function(){
// Everything is cool, possibly even save some
// sort of local cache of data.
})
.catch(function(){
// Prompt Login but do not redirect after login.
// ROAST does this if you try to like a cafe without authentication.
});
}
}
}
I’d probably put this in the App Layout so it’s present on every page and call it in the mounted() hook. If you have multiple layouts, I’d put this in each route file and call it on the mounted()
hook, however, I’d destroy the call back on beforeRouteLeave()
so you don’t compound 100s of checks. Hopefully that helps!
As for your second question, this is definitely an SSR issue. The reason SPA works is because it makes a call to the server to load the authenticated user instead of having the user returned via server. When we had this issue in the past, it was because the X-XSRF-TOKEN was not being proxied to the API. Does this workflow work:
- User authenticates
- Open a New Tab
- Navigate to app and the user is NOT authenticated
- In new tab, click a link within the app that would require auth. User now appears authenticated.
If that work flow works, it’s because of the proxying the node server headers to the API so the API has access to the Laravel Sanctum token. Let me know if that helps as well!