101 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-12-04 20:29:35 +01:00
/**
* @fileoverview Authentication Service for Rantii
* @author retoor <retoor@molodetz.nl>
* @description Handles user authentication and session management
* @keywords auth, authentication, login, session, token
*/
class AuthService {
constructor(apiClient, storageService) {
this.api = apiClient;
this.storage = storageService;
this.currentUser = null;
this.onAuthChange = null;
}
async init() {
const authData = this.storage.getAuth();
if (authData && authData.tokenId && authData.tokenKey) {
this.api.setAuth(authData.tokenId, authData.tokenKey, authData.userId);
this.currentUser = {
id: authData.userId,
username: authData.username
};
return true;
}
return false;
}
async login(username, password, remember = true) {
const result = await this.api.login(username, password);
if (result.success) {
const authData = {
tokenId: result.authToken.id,
tokenKey: result.authToken.key,
userId: result.authToken.user_id,
username: username,
expireTime: result.authToken.expire_time
};
if (remember) {
this.storage.setAuth(authData);
}
this.currentUser = {
id: authData.userId,
username: username
};
this.notifyAuthChange();
return { success: true, user: this.currentUser };
}
return { success: false, error: result.error };
}
logout() {
this.api.clearAuth();
this.storage.clearAuth();
this.currentUser = null;
this.notifyAuthChange();
}
isLoggedIn() {
return this.api.isAuthenticated() && this.currentUser !== null;
}
getUser() {
return this.currentUser;
}
getUserId() {
return this.currentUser?.id || null;
}
getUsername() {
return this.currentUser?.username || null;
}
setAuthChangeCallback(callback) {
this.onAuthChange = callback;
}
notifyAuthChange() {
if (this.onAuthChange) {
this.onAuthChange(this.isLoggedIn(), this.currentUser);
}
window.dispatchEvent(new CustomEvent('rantii:auth-change', {
detail: {
isLoggedIn: this.isLoggedIn(),
user: this.currentUser
}
}));
}
requireAuth() {
if (!this.isLoggedIn()) {
window.dispatchEvent(new CustomEvent('rantii:require-auth'));
return false;
}
return true;
}
}
export { AuthService };