Expo sdk 29/30:登录后的会话Cookie [英] Expo sdk 29/30: session cookie from login

查看:86
本文介绍了Expo sdk 29/30:登录后的会话Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有先前版本的Expo中,我使用RN fetch方法将用户名/密码发布到Flask登录端点.该端点将用户信息保存到会话中并设置会话cookie.它始终是透明的,我再也不需要通过任何额外的内容.

In all prev versions of Expo, I used RN fetch method to POST username/password to a Flask login endpoint. This endpoint saved user info to the session and set session cookie. It was always transparent and I never had to pass anything extra.

此功能自第28届世博会开始有效.但是,当我升级到Expo 30时,服务器不再将客户端视为已登录.是否需要更改获取方式?我需要传递一些额外的参数吗?

This worked as of Expo v28. But when I upgraded to Expo 30, the server no longer sees the client as logged in. Do I need to change how I use fetch? Do I need to pass some extra parameters?

推荐答案

首先,在登录之前,我们需要使用以下命令清除旧的Cookie:

First, before login, we need to clear the old cookies using:

import { NativeModules } from 'react-native'
const Networking = NativeModules.Networking;
Networking.clearCookies((cleared) => {
    console.debug('cleared hadCookies: ' + cleared.toString());
    ApiUtils.login(your_login_parameters); // your login function
});

我们必须清除cookie,因为本机cookie管理器正在发送过时的cookie.现在,我们将自行管理Cookie.

We have to clear the cookies because the native cookie manager is sending stale cookies. Now, we will manage the cookie ourselves.

在我的登录处理程序中,我使用set-cookie-parser来解析服务器在"set-cookie"响应标头中发送的cookie.

In my login handler, I use set-cookie-parser to parse the cookies sent by the server in the 'set-cookie' response header.

import setCookie from 'set-cookie-parser';

// insider my login handler
const response = await fetch(login_endpoint, credentials_payload);
let cookies, cookieHeader, serverData;
if (response.ok) {
    serverData = await response.json();   
    cookieHeader = setCookie.splitCookiesString(response.headers.get('set-cookie'));
    cookies = setCookie.parse(cookieHeader);
    console.log(cookies); // array
    // Save cookies array to SecureStore or AsyncStorage
}

最后,对于到服务器的所有需要​​会话cookie的请求,我们都会在fetch标头中发送该cookie.

Finally, for all requests to the server that require session cookie, we send the cookie in the fetch headers.

const makeFetchParams = (form_data) => {
    const cookies = get_from_storage_of_choice // SecureStore or AsyncStorage
    const c_arr = cookies.map(d => { return d.name+'='+d.value; });
    const cookieStr = c_arr.join(';');
    return {
        method: 'POST',
        credentials: 'omit',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'cookie': cookieStr
        },
        body: form_data
    };
};

// inside endpoints that require the session cookie
const response = await fetch(your_url, makeFetchParams(some_form_data));

我使用Expo SDK v30和Android对此进行了测试.这是为我工作"的解决方案,可能对其他人有用.

I tested this with Expo SDK v30 and Android. This is works-for-me solution may be of use to others.

这篇关于Expo sdk 29/30:登录后的会话Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆