The following snippet lets you redirect users back to a protected content page they were attempting to access after login. It will override the value in Auth > Sign up and Login >Sign up settings > Post Signup URL.
Depending on your need, you might be looking for:
👉 Redirect to a plan-specific page post login, aka when a user logs in
👉 Redirect to a plan-specific page post signup, ie. before they create a password
👉 Redirect to a plan-specific page post first-time login, aka onboarding
The snippet
Add the provided custom code snippet (found between <!-- ✨ Custom Code Snippet Start --> and <!-- ✨ Custom Code Snippet End ✨ -->) to the head of your site below the Outseta script, which should already be in your site's head tag.
Remove all console.log lines after verifying that the code works.
<!-- Outseta Script Options -->
<script>
var o_options = {
domain: '[your-domain].outseta.com',
};
</script>
<!-- Outseta Script (doing all the magic) -->
<script src="https://cdn.outseta.com/outseta.min.js" data-options="o_options"></script>
<!-- ✨ Custom Code Snippet Start -->
<script>
// 1. Store the URL of the page the user attempted to access
Outseta.on('nocode.accessDenied', () => {
console.log('✨ nocode.accessDenied');
if (!Outseta.getAccessToken()) {
console.log(
'✨ |- user is unauthenticated, store url',
window.location.href
);
// This is an unauthenticated user (not a denied access user),
// so store the current url.
sessionStorage.setItem('postLoginUrl', window.location.href);
} else {
console.log('|- user is authenticated, but does not have access');
}
});
// 2. Redirect the user to the stored URL after they log in
Outseta.on('redirect', (redirectUrl) => {
console.log('✨ redirect', redirectUrl);
const redirectURL = new URL(redirectUrl);
const accessToken = redirectURL.searchParams.get('access_token');
if (accessToken) {
console.log('✨ |- is post login redirect');
// This is a login redirect, so let's
// see if we have a stored postLoginUrl URL
const postLoginUrl = sessionStorage.getItem('postLoginUrl');
if (postLoginUrl) {
console.log('✨ |- set access token');
Outseta.setAccessToken(accessToken);
console.log('✨ |- redirect to postLoginUrl', postLoginUrl);
// Remove the stored postLoginUrl URL
sessionStorage.removeItem('postLoginUrl');
// Redirect to the stored URL,
window.location.href = postLoginUrl;
// Disable redirect to original redirectUrl
return false;
}
}
});
</script>
<!-- Custom Code Snippet End ✨ -->
This script:
- Listens for the
nocode.accessDeniedevent and stores the current URL in session storage - Intercepts the redirect after login and checks for a stored URL to redirect to
- Cleans up the stored URL after use