better sighnout and more user info
This commit is contained in:
22
src/routes/private/home/+page.server.ts
Normal file
22
src/routes/private/home/+page.server.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
// src/routes/my-page/+page.server.ts
|
||||||
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
|
export const load: PageServerLoad = async ({ locals }) => {
|
||||||
|
// get the logged-in user
|
||||||
|
const { data: { user }, error: authError } = await locals.supabase.auth.getUser();
|
||||||
|
|
||||||
|
const { data: user_profile, error: profileError } = await locals.supabase.from('profiles').select('*, section:sections (id, name)').eq('id', user?.id).single();
|
||||||
|
|
||||||
|
if (authError) {
|
||||||
|
console.error('Supabase auth error:', authError);
|
||||||
|
throw new Error('Could not get user');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (profileError) {
|
||||||
|
console.error('Supabase profile error:', profileError);
|
||||||
|
throw new Error('Could not get user profile');
|
||||||
|
}
|
||||||
|
|
||||||
|
return { user, user_profile };
|
||||||
|
|
||||||
|
};
|
||||||
@@ -1,36 +1,19 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
import type { User } from '@supabase/supabase-js';
|
||||||
import { goto } from '$app/navigation'
|
|
||||||
|
|
||||||
let { data } = $props();
|
export let data: {
|
||||||
let user_data = $state();
|
user: User | null,
|
||||||
|
user_profile: any | null
|
||||||
onMount(async () => {
|
|
||||||
const { data: { user } } = await data.supabase.auth.getUser();
|
|
||||||
user_data = user;
|
|
||||||
});
|
|
||||||
|
|
||||||
async function signOut() {
|
|
||||||
const { error } = await data.supabase.auth.signOut();
|
|
||||||
if (error) {
|
|
||||||
console.error('Sign out error:', error);
|
|
||||||
} else {
|
|
||||||
user_data = null; // Clear user data on sign out
|
|
||||||
await goto('/');
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if user_data}
|
|
||||||
<div class="user-profile">
|
<div class="user-profile">
|
||||||
<h2 class="text-2xl font-bold mb-2">Currently logged in</h2>
|
<h2 class="mb-2 text-2xl font-bold">Currently logged in</h2>
|
||||||
<p><strong>Username:</strong> {user_data.user_metadata.display_name}</p>
|
<p><strong>Username:</strong> {data.user?.user_metadata.display_name}</p>
|
||||||
<p><strong>Email:</strong> {user_data.email}</p>
|
<p><strong>Email:</strong> {data.user?.email}</p>
|
||||||
|
<p><strong>Section:</strong> {data.user_profile?.section.name}</p>
|
||||||
|
<p><strong>Position:</strong> {data.user_profile?.section_position}</p>
|
||||||
</div>
|
</div>
|
||||||
<button class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600 mt-4" onclick={signOut}>
|
<button class="mt-4 rounded bg-red-500 px-4 py-2 text-white hover:bg-red-600">
|
||||||
Sign Out
|
<a href="/auth/signout">Sign out</a>
|
||||||
</button>
|
</button>
|
||||||
{:else}
|
|
||||||
<p>Loading user profile...</p>
|
|
||||||
{/if}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user