fix(web): resolve display name from userInfo, not just claims (#70) #89
1 changed files with 14 additions and 4 deletions
|
|
@ -4,16 +4,26 @@ import { logtoConfig } from './logto';
|
|||
|
||||
export const getAuthenticatedUser = cache(async () => {
|
||||
try {
|
||||
const context = await getLogtoContext(logtoConfig);
|
||||
const context = await getLogtoContext(logtoConfig, { fetchUserInfo: true });
|
||||
|
||||
if (!context.isAuthenticated || !context.claims?.sub) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { claims, userInfo } = context;
|
||||
|
||||
// Mirror the vitrine's display-name resolution (la-compagnie-maximus#80):
|
||||
// prefer the userInfo endpoint over ID token claims, falling back to email
|
||||
// in the layout. The `name` claim is often absent from the ID token while
|
||||
// present in userInfo, which is why the user saw their email instead of "Max".
|
||||
return {
|
||||
userId: context.claims.sub,
|
||||
email: context.claims.email,
|
||||
name: context.claims.name,
|
||||
userId: claims.sub,
|
||||
email: userInfo?.email || claims.email,
|
||||
name:
|
||||
userInfo?.name ||
|
||||
userInfo?.username ||
|
||||
claims.name ||
|
||||
claims.username,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[auth] getLogtoContext error:', error);
|
||||
|
|
|
|||
Loading…
Reference in a new issue