使用Firebase,React和服务器端渲染

时间:2021-03-11 19:38:17

I'm building a project with react and firebase, with an node express server. What's the best way to handle routes that need to retrieve the auth status of the user? If I understand correctly firebase doesn't store its auth data in a cookie, so I'm not sure I'll be able to know the logged in user or even if a user is logged in at all, when on the server.

我正在使用react和firebase构建一个项目,其中包含一个node express服务器。处理需要检索用户身份验证状态的路由的最佳方法是什么?如果我理解正确firebase不会将其身份验证数据存储在cookie中,那么我不确定我是否能够知道登录用户,或者即使用户在服务器上登录也是如此。

2 个解决方案

#1


0  

If you are using firebase client side, just use the firebase Js library. You may let your server know about the use using id tokens.

如果您使用的是firebase客户端,请使用firebase Js库。您可以使用id令牌让服务器了解使用情况。

#2


0  

My experience with React Redux is that everything is stored in state. When you login , it stores it in state and being passed from Actions to Reducers to the components.

我对React Redux的体验是一切都存储在状态中。当您登录时,它将其存储在状态中,并从Actions传递到Reducers到组件。

Actions:

操作:

export function login(email,password){
    return dispatch => auth.signInWithEmailAndPassword(email, password);
}

export function logout(){
    return dispatch => auth.signOut();
}

export function createAccount(email, password){
    return dispatch => auth.createUserWithEmailAndPassword(email, password);
}

#1


0  

If you are using firebase client side, just use the firebase Js library. You may let your server know about the use using id tokens.

如果您使用的是firebase客户端,请使用firebase Js库。您可以使用id令牌让服务器了解使用情况。

#2


0  

My experience with React Redux is that everything is stored in state. When you login , it stores it in state and being passed from Actions to Reducers to the components.

我对React Redux的体验是一切都存储在状态中。当您登录时,它将其存储在状态中,并从Actions传递到Reducers到组件。

Actions:

操作:

export function login(email,password){
    return dispatch => auth.signInWithEmailAndPassword(email, password);
}

export function logout(){
    return dispatch => auth.signOut();
}

export function createAccount(email, password){
    return dispatch => auth.createUserWithEmailAndPassword(email, password);
}