Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new auth errors + update oauth icons for sveltekit-auth example #126

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions sveltekit-auth/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,3 @@ export function transformSearchParams(searchParams: URLSearchParams): {

return params;
}

export function parseError(e: any) {
let err: any = e instanceof Error ? e.message : String(e);

try {
err = JSON.parse(err);
} catch {}

return err?.error?.message ?? JSON.stringify(err);
}
7 changes: 5 additions & 2 deletions sveltekit-auth/src/routes/forgot-password/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, type Actions } from "@sveltejs/kit";
import { parseError } from "$lib/utils";
import { UserError } from "@edgedb/auth-sveltekit/server";

export const load = async ({ locals }) => ({
providers: await locals.auth.getProvidersInfo(),
Expand All @@ -21,7 +21,10 @@ export const actions = {
};
} catch (e) {
return fail(400, {
error: `Error signing up: ${parseError(e)}`,
error:
e instanceof UserError
? `Error sending password reset: ${e.message}`
: `Unknown error occurred sending password reset`,
});
}
},
Expand Down
7 changes: 5 additions & 2 deletions sveltekit-auth/src/routes/reset-password/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, redirect } from "@sveltejs/kit";
import { parseError } from "$lib/utils";
import { UserError } from "@edgedb/auth-sveltekit/server";
import type { Actions } from "./$types";

export const load = async ({ locals, url }) => {
Expand Down Expand Up @@ -27,7 +27,10 @@ export const actions = {
await locals.auth.emailPasswordResetPassword(formData);
} catch (e) {
return fail(400, {
error: `Error signing up: ${parseError(e)}`,
error:
e instanceof UserError
? `Error resetting password: ${e.message}`
: `Unknown error occurred resetting password`,
});
}

Expand Down
7 changes: 5 additions & 2 deletions sveltekit-auth/src/routes/signin/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fail, redirect } from "@sveltejs/kit";
import { parseError } from "$lib/utils";
import { UserError } from "@edgedb/auth-sveltekit/server";
import type { Actions } from "./$types";

export const load = async ({ locals }) => ({
Expand All @@ -13,7 +13,10 @@ export const actions = {
await locals.auth.emailPasswordSignIn(formData);
} catch (e) {
return fail(400, {
error: `Error signing up: ${parseError(e)}`,
error:
e instanceof UserError
? `Error signing in: ${e.message}`
: `Unknown error occurred signing in`,
});
}

Expand Down
12 changes: 7 additions & 5 deletions sveltekit-auth/src/routes/signin/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import clientAuth from "$lib/auth.js";

const providerIcons = {
"builtin::oauth_apple": "apple",
"builtin::oauth_azure": "microsoft-azure",
"builtin::oauth_github": "github",
"builtin::oauth_google": "google",
"builtin::oauth_apple": "logos:apple",
"builtin::oauth_azure": "logos:microsoft-azure",
"builtin::oauth_discord": "logos:discord-icon",
"builtin::oauth_github": "logos:github-icon",
"builtin::oauth_google": "logos:google-icon",
"builtin::oauth_slack": "logos:slack-icon",
};

export let params = transformSearchParams($page.url.searchParams);
Expand All @@ -34,7 +36,7 @@
class="rounded-lg bg-slate-50 p-3 font-medium shadow-md shrink-0 hover:bg-white hover:scale-[1.03] transition-transform
flex items-center"
>
<Icon icon={`mdi:${providerIcons[provider.name]}`} />
<Icon icon={providerIcons[provider.name]} />
<span class="ml-3">{provider.display_name}</span>
</a>
{/each}
Expand Down
11 changes: 8 additions & 3 deletions sveltekit-auth/src/routes/signup/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fail, redirect } from "@sveltejs/kit";
import { UserError } from "@edgedb/auth-sveltekit/server";
import { createUser } from "$lib/server/utils";
import { parseError } from "$lib/utils";
import type { Actions } from "./$types";

export const load = async ({ locals }) => ({
Expand All @@ -18,7 +18,9 @@ export const actions = {
};
} catch (e) {
return fail(400, {
error: `Error signing up: ${parseError(e)}`,
error: `Error signing up: ${
e instanceof Error ? e.message : String(e)
}`,
});
}
},
Expand All @@ -40,7 +42,10 @@ export const actions = {
await createUser({ client, tokenData });
} catch (e) {
return fail(400, {
error: `Error signing up: ${parseError(e)}`,
error:
e instanceof UserError
? `Error signing up: ${e.message}`
: `Unknown error occurred signing up`,
});
}

Expand Down