简体   繁体   English

同构应用程序中的用户配置文件

[英]User profile in isomorphic applications

I'm currently in the process of adding login, plus a set of advanced features related to login, to a website using React, Redux, Express and Node. 我目前正在使用React,Redux,Express和Node向网站添加登录以及一组与登录相关的高级功能。 For the actual login, I'm using Passport, with Facebook, Google and Twitter strategies. 对于实际的登录,我使用的是Passport,以及Facebook,Google和Twitter策略。 After getting the required token, it's sent to AWS Cognito Federated Identities to get synchronized data for the user. 获取所需令牌后,它将发送到AWS Cognito联合身份以获取用户的同步数据。 Data from the website is also synchronized back to Cognito whenever needed. 网站数据也将在需要时同步回Cognito。

After the user has logged in, I can construct a complete user profile on the Node server, but now what? 用户登录后,我可以在Node服务器上构建完整的用户配置文件,但是现在呢?

// server.jsx
// imports
.............

// variables
.............

var userProfile;
// logging the user with Passport and adding it to the profile
.............

app.use((req, res) => {
    // I can do whatever I want with the profile here
    .............

    // handling the request and creating an initial state ..
    .............
}

Back in the days, this would be fairly easy to solve using either session variables (Java/C#) or cookies (JS), but in isomorphic applications it's not quite that simple. 过去,使用会话变量(Java / C#)或cookie(JS)都可以很容易地解决此问题,但是在同构应用程序中并不是那么简单。 Let's say that the user gets access to profile.jsx via routes after logging in: A cookie (set via client-sessions for example) is only available when the jsx file is run on the client; 假设用户登录后可以通过路由访问profile.jsx:cookie(例如,通过客户端会话设置)仅在客户端上运行jsx文件时可用; it's not accessible whenever it's server rendered. 服务器渲染后就无法访问。

Custom code appears to be required even when using things like isomorphic-cookie or universal-cookie, which pretty much defeats the purpose of an isomorphic application. 即使使用同构cookie或通用cookie之类的东西,似乎也需要自定义代码,这几乎违反了同构应用程序的目的。 I also thought of using Redux, but Redux appears to only have an initial state on Node, which doesn't help much. 我还考虑过使用Redux,但是Redux似乎在Node上只有一个初始状态,没有太大帮助。

// Profile.jsx
import React , { Component } from 'react';
import Cookies from 'universal-cookie';

class Profile extends Component {
    // Stuff
    .............

    render() {
        // Would love to be able to access to the profile without custom code like this
        if (typeof window != 'undefined' && window.document) {
            const cookies = new Cookies();
            console.log(cookies.get('user'));
        }
    }

So is there a smooth way of handling user profiles or other variables that you want access to at all times, without writing a huge amount of custom code for each scenario? 因此,是否有一种不间断的方式来处理您想要随时访问的用户配置文件或其他变量,而无需为每种情况编写大量的自定义代码? It seems most tutorials and examples handle this sort of thing exclusively either on the server or the client, which would require a re-write of the website in this particular case. 似乎大多数教程和示例都在服务器或客户端上专门处理这种事情,在这种情况下,这需要重新编写网站。

Isomorphic applications are fairly straight forward in most cases, but the complexity really shoots through the roof when authentication, profiles and so on is introduced, so I would greatly appreciate any feedback on the matter. 同构应用程序在大多数情况下是相当简单的,但是当引入身份验证,配置文件等时,复杂性的确冒了顶峰,因此,我对此表示感谢。

I think a common scenario here would be to add an authentication step, then to store a successful authentication token either as a cookie or in your app state that you supply back to the profile API on subsequent queries. 我认为这里的常见情况是添加身份验证步骤,然后将成功的身份验证令牌存储为cookie或应用状态,然后在后续查询中提供给配置文件API。

Any data that is returned form your API can also be added to your Redux store so you always have access to it and subsequently updated whenever you need to persist changes or want to force update the current state data. 从您的API返回的所有数据也可以添加到Redux存储中,因此您始终可以访问它,并在需要持久更改或强制更新当前状态数据时进行更新。

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

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