fix: resolve username mismatch in login by fetching profile for canonical name

- Add notifyAuthChange() call after successful token validation in setAuthFromToken
- Fetch user profile during login to obtain canonical username instead of using raw input
- Update both authData and currentUser objects with resolved username from profile API
- Handle profile fetch failure gracefully by falling back to provided username
This commit is contained in:
retoor 2025-12-04 21:36:01 +00:00
parent 17f001975f
commit 8e02a2d053

View File

@ -21,6 +21,7 @@ class AuthService {
id: authData.userId, id: authData.userId,
username: authData.username username: authData.username
}; };
this.notifyAuthChange();
return true; return true;
} }
return false; return false;
@ -29,11 +30,18 @@ class AuthService {
async login(username, password, remember = true) { async login(username, password, remember = true) {
const result = await this.api.login(username, password); const result = await this.api.login(username, password);
if (result.success) { if (result.success) {
let actualUsername = username;
try {
const profileResult = await this.api.getProfile(result.authToken.user_id);
if (profileResult?.success && profileResult.profile?.username) {
actualUsername = profileResult.profile.username;
}
} catch (e) {}
const authData = { const authData = {
tokenId: result.authToken.id, tokenId: result.authToken.id,
tokenKey: result.authToken.key, tokenKey: result.authToken.key,
userId: result.authToken.user_id, userId: result.authToken.user_id,
username: username, username: actualUsername,
expireTime: result.authToken.expire_time expireTime: result.authToken.expire_time
}; };
if (remember) { if (remember) {
@ -41,7 +49,7 @@ class AuthService {
} }
this.currentUser = { this.currentUser = {
id: authData.userId, id: authData.userId,
username: username username: actualUsername
}; };
this.notifyAuthChange(); this.notifyAuthChange();
return { success: true, user: this.currentUser }; return { success: true, user: this.currentUser };