I’m using the React WebApp Template and what I’m trying to do is to check if the user is authenticated and if not present then with a html form for them to login. I can login however I get the user auth details which I don’t want, I’ve tried looking for ways to redirect but have been unsuccessful.
Ways to redirect in ServiceStack if they’re not Authenticated, create a Service with routes for all the pages you want to redirect, e.g
Creates Auth Only Service with Routes of Pages you want to redirect
[Route("/authonly")]
[Route("/contact")]
public class RedirectToLoginPage {}
[Authenticate(HtmlRedirect="~/login")]
public class AuthOnlyServices : Service
{
public object Any(RedirectToLoginPage request) => request;
}
HtmlRedirect can also be globally declared in your AuthFeature plugin
Add a Metadata Redirect rule
If you want to do this for all pages you can emit a HTML refresh meta tag if they’re not authenticated and the they’re not on the login page, e.g:
For snippets like this I’d prefer to wrap it in a partial, e.g. copy the above fragment in a file like ensure-authenticated.html then embed it in pages you want to redirect if they’re not authenticated:
Thanks for that, I’ve implemented the metadadta redirect rule, but after a successful login it keeps redirecting as though I’m not authenticated.
If I remove the redirect template code and navigate manually to login and authenticate I can called services that require authentication. Whats the best way to debug the isAuthenticated filter?
The session cookie is there matches to the corresponding cache table.
I’ve copied the TemplateCode on to the acutal index.html page and below is the output.
There is no information on the session except for the ss-id, aswell as the appHostServiceName and other fields blank.
I don’t have any other suggestions, inspect the raw HTTP Headers to make sure the ss-id/ss-pid Cookies are being sent which point to an Authenticated UserSession in the registered ICacheClient and that calling /auth returns a session, if it returns a 401 it means you’re not authenticated.
The session is registered in the ICacheClient I can see it in the db. The debug page /metadata/debug also confirms I’ve authenticated and so does every other html pages EXCEPT INDEX.HTML
I have even placed the login html snippet in index.html and {{ userSession | select: { it.IsAuthenticated } }} still comes back with nothing BUT it will work on another htm page.
I’m not doing anything fancy just want to ensure that the user is logged on before accessing the react app. Is there any chance you can provide me with a modified react-spa project that includes code to log in via Index.html.
I maybe wrong but I have a feeling that this is bug…
I don’t have the spare capacity to create customized starting templates. If you’re using React you can use JavaScript to check if they’re authenticated and redirect if not, e.g:
I think you misunderstood me, I was asking if you can use the react-spa app and provide some login features so I can compare and see where I’m going wrong.