Skip to content

Commit

Permalink
feat(auth): Create profile for user if not found on callback page.
Browse files Browse the repository at this point in the history
  • Loading branch information
lastarc committed Jul 19, 2024
1 parent 6b740e3 commit 300d1a4
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/routes/auth/callback/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script>
<script lang="ts">
import { pb } from '$lib/pocketbase';
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import type { RecordModel } from 'pocketbase';
onMount(async () => {
const params = new URL(window.location.href).searchParams;
Expand Down Expand Up @@ -38,6 +39,24 @@
localStorage.removeItem('provider');
}
if (!pb.authStore.model) {
throw new Error('User not found');
}
let profile: RecordModel | null = null;
try {
profile = await pb
.collection('profiles')
.getFirstListItem(pb.filter('user = {:user}', { user: pb.authStore.model.id }));
} catch (e) { /* noop */ }
if (!profile) {
await pb.collection('profiles').create({
user: pb.authStore.model.id,
name: pb.authStore.model.username || pb.authStore.model.email.split('@')[0],
});
}
window.location.replace('/');
});
</script>

0 comments on commit 300d1a4

Please sign in to comment.