Secure the Admin Page
Explore how to secure an admin page by implementing Firebase Authentication in a Svelte application. Learn to restrict access to authenticated users, manage redirects to the admin dashboard, and enable additional sign-in providers for a smooth user experience.
We'll cover the following...
We'll cover the following...
Securing the admin page
Click Run to open the services/web/src/routes/admin/index.svelte page and add the following <script>:
<script>
import { goto } from '@sapper/app';
import { onMount } from "svelte";
import { currentUser } from "../../stores/user";
onMount(() => {
// Checking if user not already logged in using the value from the store.
if (!$currentUser) {
goto("/auth/login")
}
});
</script>
Below, we can now wrap all content in an if-statement to make sure it is only available to authenticated users.
{#if $currentUser}
<h2>Admin dashboard</h2>
<p>Only authenticated users have access to this.</p>
{/if}
Click Run again to save the file. ... ...