You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遵循Expo认证文档后,登出再登录需重复操作的异常问题

Expo认证流程登出后再次登录首次session为null问题

我基于Expo官方认证流程文档实现认证功能,仅修改了Sign-In函数:调用API获取JSON数据并将完整数据存入session。登录流程正常,但登出后再次登录时,首次登录的session始终为null,需再次登录才能完成跳转。即使将signIn简化为以下代码,问题仍存在:

signIn: async (username, password) => {
    setSession("abc")
    router.replace({pathname: `/user`})
}

以下是我实现的完整相关代码:

return (
    <AuthContext.Provider
        value={{
            signIn: async (username, password) => {
                await AuthResp(username, password).then((promise) => {
                    if (promise) {
                        const sessionDecode = JSON.parse(promise)
                        console.log(sessionDecode.usr_type)

                        switch (sessionDecode.usr_type) {
                            case "usr":
                                router.replace({pathname: `/user`})
                                break;
                            case "admin":
                                router.replace({pathname: `/admin`})
                                break;
                        }
                    }
                })
            },

            signOut: () => {
                setSession(null)
            },

            session,
            isLoading,
        }}>
        {props.children}
    </AuthContext.Provider>
);

const AuthResp = async(username, password) => {
    console.log(`Trying login for ${username}:${password}...`)
    let userSessionJSON = null

    try {
        const knackPromise = await fetch(`APILINK`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                "email": username,
                "password": password
            })
        })

        await knackPromise.json().then(async (promise) => {
            console.log(promise.session)
            if (promise) {
                if (promise.errors && promise.errors[0]) {
                    alert(promise.errors[0].message)
                } else {
                    const userSessionData = {
                        // sessionData
                    }
                    userSessionJSON = JSON.stringify(userSessionData)
                    await SecureStore.setItemAsync("session", userSessionJSON).then( () => {
                        setSession(userSessionJSON)
                    })
                }
            }
        });

        setSession(userSessionJSON)
        return userSessionJSON

    } catch (error) {
        alert(error)
        console.error(error)
    }
}

(注:代码中多次调用setSession仅为测试用,可忽略)

内容的提问来源于stack exchange,提问作者Ghimbo21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 06:55:12