import { CommonModule } from '@angular/common'; import { Component, inject } from '@angular/core'; import { Auth } from '@angular/fire/auth'; import { GoogleAuthProvider, signInWithPopup, UserCredential } from 'firebase/auth'; import { PopoverComponent } from './components/popover.component'; import { DeckListComponent } from './deck-list.component'; import { ClickOutsideDirective } from './service/click-outside.directive'; import { PopoverService } from './services/popover.service'; import { UserService } from './services/user.service'; @Component({ selector: 'app-root', template: `

Master Your Learning

Learn smarter, not harder. Start your journey today

Logo Haiky
User Photo
`, standalone: true, styles: ` img { border: 2px solid #fff; transition: transform 0.2s; } img:hover { transform: scale(1.1); } /* Stile für das Dropdown-Menü */ .dropdown { display: none; position: absolute; right: 0; margin-top: 0.5rem; background-color: white; border: 1px solid #e2e8f0; border-radius: 0.375rem; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06); } .dropdown button { width: 100%; text-align: left; padding: 0.5rem 1rem; color: #4a5568; } .dropdown button:hover { background-color: #f7fafc; } `, imports: [CommonModule, DeckListComponent, ClickOutsideDirective, PopoverComponent], }) export class AppComponent { isLoggedIn = false; private auth: Auth = inject(Auth); showDropdown = false; photoURL: string = 'https://placehold.co/40'; // user: User | null = null; popoverVisible = false; popoverTitle = ''; popoverMessage = ''; popoverShowInput = false; popoverShowCancel = true; popoverInputValue = ''; popoverConfirmText = 'Confirm'; private confirmCallback?: (inputValue?: string) => void; private cancelCallback?: () => void; constructor(public popoverService: PopoverService, private userService: UserService) { this.popoverService.popoverState$.subscribe(options => { this.popoverVisible = true; this.popoverTitle = options.title; this.popoverMessage = options.message; this.popoverShowInput = options.showInput; this.popoverShowCancel = options.showCancel; this.popoverInputValue = options.inputValue; this.popoverConfirmText = options.confirmText; this.confirmCallback = options.onConfirm; this.cancelCallback = options.onCancel; }); } ngOnInit() { // Überprüfen des Login-Status beim Start der Anwendung const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'; const accessToken = localStorage.getItem('accessToken'); const refreshToken = localStorage.getItem('refreshToken'); this.photoURL = localStorage.getItem('photoURL'); if (isLoggedIn && accessToken && refreshToken) { this.isLoggedIn = true; } } async loginWithGoogle() { const provider = new GoogleAuthProvider(); try { const result: UserCredential = await signInWithPopup(this.auth, provider); this.isLoggedIn = true; this.photoURL = result.user.photoURL; // Speichern des Login-Status und Tokens im Local Storage localStorage.setItem('isLoggedIn', 'true'); localStorage.setItem('accessToken', await result.user.getIdToken()); localStorage.setItem('refreshToken', result.user.refreshToken); localStorage.setItem('photoURL', result.user.photoURL); this.showDropdown = false; await this.userService.logIn({ name: result.user.displayName, email: result.user.email, sign_in_provider: result.providerId }); console.log('Logged in with Google', result.user); } catch (error) { console.error('Google Login failed', error); } } logout() { this.isLoggedIn = false; localStorage.removeItem('isLoggedIn'); localStorage.removeItem('accessToken'); localStorage.removeItem('refreshToken'); // Optional: Firebase-Logout durchführen this.auth.signOut(); } toggleDropdown() { this.showDropdown = !this.showDropdown; } handleConfirm(inputValue?: string) { this.popoverVisible = false; if (this.confirmCallback) { this.confirmCallback(inputValue); } } handleCancel() { this.popoverVisible = false; if (this.cancelCallback) { this.cancelCallback(); } } }