英文:
Supabase User Returning Null + Auth State Not Saved After Closing App
问题
以下是您提供的内容的中文翻译:
我开始学习React Native,并在React中使用Supabase
与使用Flutter时有很大不同。目前,我已经成功实现了电子邮件登录/注册功能,并监听身份验证更改以切换导航堆栈。我认为我的两个问题可能相关,因为在更新时用户/会话可能没有保存。
第一个问题是每次打开应用程序时都会显示登录屏幕,即使我在关闭应用程序之前已经登录。我尝试设置一些选项,包括localStorage
,但没有帮助。以下是我的supabaseClient.tsx
文件。
const options = {
auth: {
localStorage: AsyncStorage as any,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
};
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, options);
export { supabase };
第二个问题是我正在尝试做一些简单的事情,只是在HomeScreen
上显示已登录用户在displayName
中设置的内容,但getUser()
和getSession()
中的用户都返回null,因此我无法获取user.id
。这导致displayName
为未定义,屏幕为空白。以下是我用于获取displayName
的代码。
export function HomeScreen() {
const [displayName, setDisplayName] = useState();
useEffect(() => {
(async () => {
const user = (await supabase.auth.getSession()).data.session?.user;
console.log("User: " + user);
const { data, error } = await supabase
.from("profiles")
.select()
.eq("uid", user?.id);
if (error === null) {
data!.map((data) => {
const name = data.displayName;
console.log(name);
setDisplayName(name);
});
} else {
console.log(error.message);
}
console.log("Name: " + displayName);
})();
}, [setDisplayName]);
return (
<View>
<Text>{displayName}</Text>
</View>
);
}
请注意,代码中的一些标记(如>
和<
)似乎是HTML或XML转义字符,需要根据需要进行处理。
英文:
I am starting to learn React Native and using Supabase
with React is pretty different than using it with Flutter. Right now, I have gotten email sign-in/up working and listening for auth changes to switch navigation stacks. I believe my two issues are related because the user/session might not be getting saved when updated.
The first issue is that every time the app is opened, the sign-in screen is shown, even though I signed in before closing it. I tried to set som options, including localStorage
but that did not help. Below is my supabaseClient.tsx
file.
const options = {
auth: {
localStorage: AsyncStorage as any,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
};
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, options);
export { supabase };
The second issue is that I am trying something simple, just showing the displayName
that the signed-in user has set on the HomeScreen
, but the getUser()
and the user from getSession()
both return null so I cannot get the user.id
. This causes the displayName
to be undefined and a blank screen. Below is the code I am using to get the displayName
.
export function HomeScreen() {
const [displayName, setDisplayName] = useState();
useEffect(() => {
(async () => {
const user = (await supabase.auth.getSession()).data.session?.user;
console.log("User: " + user);
const { data, error } = await supabase
.from("profiles")
.select()
.eq("uid", user?.id);
if (error === null) {
data!.map((data) => {
const name = data.displayName;
console.log(name);
setDisplayName(name);
});
} else {
console.log(error.message);
}
console.log("Name: " + displayName);
})();
}, [setDisplayName]);
return (
<View>
<Text>{displayName}</Text>
</View>
);
}
答案1
得分: 0
我之前因为看到的一个教程将localStorage
定义为any
类型。
我需要将这个部分进行修改:
const options = {
auth: {
localStorage: AsyncStorage as any,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
};
改成这样:
const options = {
auth: {
localStorage: AsyncStorage,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
};
英文:
I had defined localStorage
as any because of a tutorial I saw.
I needed to change this:
const options = {
auth: {
localStorage: AsyncStorage as any,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
};
to this:
const options = {
auth: {
localStorage: AsyncStorage,
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true,
},
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论