
Firebase includes a feature called Authentication. This allows you to use Google logins in your app, and offloads all of the complexity and issues of user management and security to Google.
The next few steps are based on Firebase's Authentication documentation, but simplified for our use.



// Signs in a user with Google authentication.
export async function login() {
const result = await signInWithPopup(auth, provider)
console.log('User signed in:', result.user)
}
Your app will need to be able to access information across different pages. The user is one example of this kind of information and it's a good opportunity to introduce the idea of state. An app's state is essentially all the important information that your application needs to remember at any given moment while it's running.
It's a bit like the app's short-term memory. This 'memory' can include things like:
When this information (the state) changes – like when your user logs in – the app can react to that change, such as showing them their personalised dashboard.
SvelteKit uses $state as a special kind of variable for this task. When this variable changes, all the components that use it get notified to update themselves to show the new information.
The next steps are based on SvelteKit's Stores and Runes documentation.


// Signs in a user with Google authentication.
export async function login() {
const result = await signInWithPopup(auth, provider)
console.log('User signed in:', result.user)
user.uid = result.user.uid
user.email = result.user.email
user.displayName = result.user.displayName
user.photoURL = result.user.photoURL
}
The logout function is as simple as importing the signOut function and creating a logout function that resets the user variable and calls the signOut function.

// Signs out the current user.
export async function logout() {
user.uid = null
user.email = null
user.displayName = null
user.photoURL = null
await signOut(auth)
}
At the moment your front page doesn't react to the user logging in. You can change that by adding an {#if} block that checks if there's a user currently logged in.
If the user variable is null, then the button should offer the user to login.
If the user variable has info in it, then the user is logged in and we can display their information and offer them a logout.
{#if user.uid == null}
<button class="button" on:click={login}>
Login
</button>
{:else}
<p>{user.displayName}</p>
<button class="button" on:click={logout}>
Logout
</button>
{/if}
You can personalise the page when the user is logged in by using their display picture and name.
You can create more $state variables in the state.svelte.js file to track other things your app needs to remember between pages, such as the classroom information.
You can change your main page so that it only shows the inputs if the user is logged in. You can also include their uid in the classrooms you store in the database. This would let you get only the classrooms with their uid.