简体   繁体   English

前后端访问相同的firebase用户

[英]Access same firebase user from front and backend

I'm trying to understand how to continue a user session started client side in my backend server.我试图了解如何在我的后端服务器中继续用户 session 启动的客户端。

I'm using NextJS for my client app and go Gin for backend api calls.我为我的客户端应用程序使用NextJS ,为后端 api 调用使用 go Gin

I have some client code that allows a user to login using firebase auth:我有一些客户端代码允许用户使用 firebase 身份验证登录:

import { getAuth, signInWithRedirect, GoogleAuthProvider } from 'firebase/auth'

export default function Login() {
  const provider = new GoogleAuthProvider()
  const auth = getAuth()

  return (
    <div>
      <ul>
        <li>
          <button onClick={() => signInWithRedirect(auth, provider)}>Sign In With Google</button>
        </li>
      </ul>
    </div>
  )
}

This is great and works with very little work on my part.这很棒,而且我的工作量很小。 Once a user clicks the button, and follows the login instructions, my client app understands who they are using the firebase libraries.一旦用户单击按钮并按照登录说明进行操作,我的客户端应用程序就会了解他们正在使用 firebase 库的用户。

Now I want to make a request to my go gin backend as this current user.现在我想以当前用户身份向我的 go gin 后端发出请求。 How can I continue the session in my backend code as the user who is logged in to my client app?如何以登录到我的客户端应用程序的用户身份继续我的后端代码中的 session? Ideally I would be able to:理想情况下,我将能够:

  1. Log in the client app as user123 .user123登录客户端应用程序。
  2. Make a request to my backend.向我的后端发出请求。
  3. My backend understands that the request came from my client as user123 .我的后端知道请求来自我的客户端user123
  4. My backend makes some request to other firebase services as user123 like creating or updating a Firestore document.我的后端以 user123 身份向其他user123服务发出一些请求,例如创建或更新 Firestore 文档。

I'm not sure how to go about doing #3.我不确定 go 如何执行 #3。 This seems like a common thing and I'm actively looking into what I need to do.这似乎很常见,我正在积极研究我需要做什么。 I'll answer my own question if I can, but any advice is appreciated.如果可以的话,我会回答我自己的问题,但我们将不胜感激任何建议。

You'll want to pass the user's ID token from your client to the server, which can then verify the ID token and based on that determine whether the user is authorized.您需要将用户的 ID 令牌从客户端传递到服务器,然后服务器可以验证 ID 令牌并根据此确定用户是否已获得授权。

But there's no way to then impersonate that user from your server in calls to Firebase. So you'll have to do the authorization checks for that yourself.但是没有办法在调用 Firebase 时从您的服务器模拟该用户。因此您必须自己进行授权检查。

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

相关问题 如何在前端和后端之间持久化 Firebase Auth state? (Firebase 身份验证 + React + Express) - How to persist the Firebase Auth state between front-end and backend ? (Firebase Auth + React + Express) 验证从后端发出的 firebase 实时数据库请求(无用户) - Authenticate firebase real time database requests made from backend (without a user) 删除用户的 firebase 数据,如果后端没有使用推荐码 - Remove firebase data of user, if referral code is not utilized in the backend NextAuth Firebase 后端“ReferenceError:初始化前无法访问‘app’” - NextAuth Firebase Backend "ReferenceError: Cannot access 'app' before initialization" 在使用 firebase 谷歌登录进行身份验证之前在后端注册用户 - Register user in backend before authenticating with firebase google sign in 如何防止同一用户与 Firebase 同时登录? - How to prevent simultaneous logins of the same user with Firebase? 管理员和用户从反应和后端的同一登录页面登录我正在使用 firebase - Admin and user login from same login page in react and back end i am using firebase 是否可以从后端服务 REST 查询 Firebase 远程配置? - Is it possible Firebase Remote Config to be queried from backend service REST? 我如何从 firebase 后端获取错误? - How can i fetch the error from the firebase backend? 从 Firebase 中删除特定用户 - Delete a specific user from Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM