简体   繁体   English

Firebase 身份验证有效,但 Ui 在 svelte 中将用户名闪烁为未定义

[英]Firebase auth works, but Ui is blinking username to undefined in svelte

  1. It seems working shortly似乎很快就会起作用
  2. The problem is blinking screen, when i do form action, ui shows email very shortly, but change to undefined问题是屏幕闪烁,当我执行表单操作时,ui 很快显示 email,但变为未定义
  • +page.svelte +page.svelte
<div>
    <div>
        {$fbUser?.email}
    </div>
    {#if !$fbUser}
        <a href="/auth">go auth</a>
    {/if}
</div>
  • +page.server.ts +页面.server.ts
export const actions: Actions = {
    register: async ({ request }) => {
        const data = await request.formData();
        const email = String(data.get('email'));
        const password = String(data.get('password'));
        try {
            await fbUser.createUser({ email, password });
        } catch (e) {
            if (e instanceof FirebaseError) {
                return fail(409, {
                    failed: true,
                    error: e.message
                });
            }
        }
        throw redirect(307, "/home")
    },
  • writable store可写存储
const userWritable = writable<User | null>(fb_auth.currentUser);

function authStore() {
    const { set, subscribe, update } = userWritable;

    const changes = () => onAuthStateChanged(fb_auth, (u) => {
        if (u) set(u)
        else update((curr) => curr = u)
    })

    return {
        subscribe,
        createUser: async (user: Register) => {
            await createUserWithEmailAndPassword(fb_auth, user.email, user.password);
            changes()
        },

I tried installing onAuthStateChanged in +page.svelte instead of store using load function for onAuthStateChanged... but all result are same.我尝试在 +page.svelte 中安装 onAuthStateChanged 而不是使用 load function for onAuthStateChanged 安装 onAuthStateChanged ... 但所有结果都是相同的。 email shows very shortly after that it is changed to undefined. email 不久后显示为未定义。 seems serverside(firebase) works, but i think im missing something on ui concepts似乎服务器端(firebase)有效,但我认为我在 ui 概念上遗漏了一些东西

Try to add an await block surround your div in +page.svelte尝试在 +page.svelte 中的 div 周围添加一个等待块

{#await fbUser}
<div>
    <div>
        {$fbUser?.email}
    </div>
    {#if !$fbUser}
        <a href="/auth">go auth</a>
    {/if}
</div>
{/await}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM