-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor user authentication and state management features (#795)
* ✨ Seed the storage.buckets table with default buckets in seed.sql * ➕ Add `snakecase-keys` * 🔧 Change `let` to `const` for `isSignUpPage` variable in AdminHeaderMessage.svelte * 🔧 [backend] Update --schema option for `pnpm pull` command * ♻️ import paths to use `$lib/supabase` instead of `$lib/supabaseClient` * ♻️ Rename `[comment|user]Utils.ts` to `[comment|user]Requests.ts` * ♻️ Refactor user authentication and profile management features * Update getUser function to include 'id' in the selected fields and use camelcaseKeys * Remove user input management and authentication methods from userStore.svelte.ts * Add userInputs state management in UserInputs.svelte
- Loading branch information
Showing
19 changed files
with
221 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Seed the storage.buckets table with the default buckets | ||
insert into storage.buckets | ||
(id, name, public, avif_autodetection, file_size_limit) | ||
values | ||
('comments', 'comments', true, false, 5242880); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../src/lib/features/comment/commentUtils.ts → ...c/lib/features/comment/commentRequests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<script lang="ts"> | ||
import type { AuthSession } from '@supabase/supabase-js'; | ||
import { onMount } from 'svelte'; | ||
import { getUser } from '$lib/features/user/userRequests'; | ||
import { userStore } from '$lib/features/user/userStore.svelte'; | ||
import { supabase } from '$lib/supabase'; | ||
let session = $state<AuthSession | null>(null); | ||
$effect(() => { | ||
if (!session) { | ||
userStore.user = null; | ||
return; | ||
} | ||
getUser(session.user.id).then(({ user }) => { | ||
userStore.user = user; | ||
}); | ||
}); | ||
onMount(() => { | ||
supabase.auth.getSession().then(({ data }) => { | ||
session = data.session; | ||
}); | ||
supabase.auth.onAuthStateChange((_event, _session) => { | ||
session = _session; | ||
}); | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { AuthError, PostgrestError } from '@supabase/supabase-js'; | ||
import camelcaseKeys from 'camelcase-keys'; | ||
|
||
import { supabase } from '$lib/supabase'; | ||
|
||
import type { User } from './userStore.svelte'; | ||
|
||
/** | ||
* Sign up a user | ||
* @param inputs User inputs | ||
* @param inputs.email User email | ||
* @param inputs.password User password | ||
* @param inputs.displayName User display name | ||
* @returns Error | ||
*/ | ||
export async function signUp(inputs: { | ||
email: string; | ||
password: string; | ||
displayName: string; | ||
}): Promise<{ error: AuthError | null }> { | ||
const { email, password, displayName } = inputs; | ||
const { error } = await supabase.auth.signUp({ | ||
email, | ||
password, | ||
options: { | ||
data: { display_name: displayName }, | ||
}, | ||
}); | ||
return { error }; | ||
} | ||
|
||
/** | ||
* Sign in a user | ||
* @param inputs User inputs | ||
* @param inputs.email User email | ||
* @param inputs.password User password | ||
* @returns Error | ||
*/ | ||
export async function signIn(inputs: { | ||
email: string; | ||
password: string; | ||
}): Promise<{ error: AuthError | null }> { | ||
const { error } = await supabase.auth.signInWithPassword(inputs); | ||
return { error }; | ||
} | ||
|
||
/** | ||
* Sign out a user | ||
* @returns Error | ||
*/ | ||
export async function signOut(): Promise<{ error: AuthError | null }> { | ||
const { error } = await supabase.auth.signOut(); | ||
return { error }; | ||
} | ||
|
||
/** | ||
* Get a user (profile) by id | ||
* @param id user id | ||
* @returns user (profile) and error | ||
*/ | ||
export async function getUser( | ||
id: string, | ||
): Promise<{ user: User | null; error: PostgrestError | null }> { | ||
const { data: profiles, error } = await supabase | ||
.from('profiles') | ||
.select('id, email, display_name, bio, created_at') | ||
.eq('id', id); | ||
|
||
const user = profiles?.length ? (camelcaseKeys(profiles[0]) satisfies User) : null; | ||
|
||
return { user, error }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.