github鉴权,无法将获取到的用户名和头像传入web前端并显示 #4232
-
📦 部署环境Docker 📌 软件版本lobehub/lobe-chat-database:1.19.34 💻 系统环境Other Linux 🌐 浏览器Chrome 🐛 问题描述github鉴权,无法将获取到的用户名和头像传入web前端,只能将获取到邮箱作为用户名,头像则为默认占位符。 我通过脚本确认了通过使用和 测试脚本如下:
相关讨论: #4116 📷 复现步骤数据库版,使用github鉴权,登录。 🚦 期望结果web前端正常显示对应github账户的用户名和头像 📝 补充信息github app只开放了邮箱权限,用户名和头像是公开访问的,不用额外授权。 |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments
-
Thank you for raising an issue. We will investigate into the matter and get back to you as soon as possible. |
Beta Was this translation helpful? Give feedback.
-
I found a similar unsolved discussion regarding GitHub login and retrieving the username and avatar: 希望github登录能获取到用户名和头像 [1]. To resolve the issue where GitHub authentication fails to pass the retrieved username and avatar to the web frontend, ensure that the import { Avatar, type AvatarProps } from '@lobehub/ui';
import qs from 'query-string';
import { memo } from 'react';
import urlJoin from 'url-join';
interface GitHubAvatarProps extends Omit<AvatarProps, 'avatar'> {
username: string;
}
const GitHubAvatar = memo<GitHubAvatarProps>(({ username, size = 24 }) => {
const url = qs.stringifyUrl({
query: { size: size * 2 },
url: urlJoin('https://github.com', `${username}.png`),
});
return <Avatar alt={username} avatar={url} size={size} />;
});
export default GitHubAvatar; Ensure that when you use the <GitHubAvatar username={retrievedUsername} size={48} /> If Additionally, verify that the GitHub provider configuration in your authentication setup is correctly mapping the profile fields. Here is an example configuration using import GitHub from 'next-auth/providers/github';
import { authEnv } from '@/config/auth';
import { CommonProviderConfig } from './sso.config';
const provider = {
id: 'github',
provider: GitHub({
...CommonProviderConfig,
authorization: { params: { scope: 'read:user user:email' } },
clientId: authEnv.GITHUB_CLIENT_ID ?? process.env.AUTH_GITHUB_ID,
clientSecret: authEnv.GITHUB_CLIENT_SECRET ?? process.env.AUTH_GITHUB_SECRET,
profile: (profile) => {
return {
email: profile.email,
id: profile.id.toString(),
image: profile.avatar_url,
name: profile.name,
providerAccountId: profile.id.toString(),
};
},
}),
};
export default provider; Ensure that the There are also middleware or hooks in place that modify or handle the user data retrieved from GitHub before it reaches the frontend. Specifically, the callbacks: {
async jwt({ token, account, profile }) {
if (account) {
token.userId = account.providerAccountId;
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.userId ?? session.user.id;
}
return session;
},
} Additionally, the For error handling and logging, the console.log('[NextAuth] Error:', props.error); This logging can provide insights into why the username and avatar are not being passed correctly to the frontend by capturing and displaying relevant error information [5]. |
Beta Was this translation helpful? Give feedback.
-
有尝试过使用一个空的数据库去复现这个bug吗? |
Beta Was this translation helpful? Give feedback.
-
Have you tried using an empty database to reproduce this bug? |
Beta Was this translation helpful? Give feedback.
-
试过了,发现是Cloudflare Zero Trust的问题,空数据库首先使用github原生鉴权的话没问题。请问前者该怎么设置才能显示连接的github账户的用户名和头像? |
Beta Was this translation helpful? Give feedback.
-
I tried it and found that it was a problem with Cloudflare Zero Trust. If the empty database first uses github native authentication, there will be no problem. How can I set the former to display the username and avatar of the connected github account? |
Beta Was this translation helpful? Give feedback.
-
@hezhijie0327 大佬可以帮忙看看这个问题嘛? |
Beta Was this translation helpful? Give feedback.
-
@hezhijie0327 Can you guys help me figure this out? |
Beta Was this translation helpful? Give feedback.
-
Cloudflare Zero Trust 不支持返回头像参数,这个问题我印象里在哪个 PR 也提到过 现在数据库好像存在键值的话后续就不更新了 |
Beta Was this translation helpful? Give feedback.
-
Cloudflare Zero Trust does not support returning avatar parameters. I remember this problem was mentioned in a PR. Now it seems that if the key value exists in the database, it will not be updated in the future. |
Beta Was this translation helpful? Give feedback.
-
啊这,看来只能先用github原生注册了。现在lobe不支持导出聊天记录,也懒得进数据库里改,看来只能慢慢等了 |
Beta Was this translation helpful? Give feedback.
-
Ah, it seems that I can only register natively with github first. Now Lobe does not support exporting chat records, and I am too lazy to change it in the database. It seems that I can only wait slowly. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
试过了,发现是Cloudflare Zero Trust的问题,空数据库首先使用github原生鉴权的话没问题。请问前者该怎么设置才能显示连接的github账户的用户名和头像?