-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d736bc2
commit 0360a5a
Showing
45 changed files
with
1,713 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, Router } from '@angular/router'; | ||
import { AuthService } from '../services/auth.service'; | ||
import { Observable, of } from 'rxjs'; | ||
import { map, take, switchMap } from 'rxjs/operators'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AdminGuard implements CanActivate { | ||
constructor(private authService: AuthService, private router: Router) {} | ||
|
||
canActivate(): Observable<boolean> { | ||
return this.authService.user$.pipe( | ||
take(1), | ||
switchMap(user => { | ||
if (!user) { | ||
this.router.navigate(['/login']); | ||
return of(false); | ||
} | ||
return this.authService.isAdmin$; | ||
}), | ||
map(isAdmin => { | ||
if (isAdmin) { | ||
return true; | ||
} else { | ||
this.router.navigate(['/index/games']); | ||
return false; | ||
} | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,39 @@ | ||
import { inject, Injectable } from '@angular/core'; | ||
import { Auth, createUserWithEmailAndPassword, deleteUser, getAuth, sendEmailVerification, sendPasswordResetEmail, signInWithCredential, signInWithCustomToken, signInWithEmailAndPassword, updateCurrentUser, updateEmail, updatePassword, updateProfile, User, user } from '@angular/fire/auth'; | ||
import { Subscription } from 'rxjs'; | ||
import { Auth, signInWithEmailAndPassword, createUserWithEmailAndPassword, signOut, user } from '@angular/fire/auth'; | ||
import { BehaviorSubject, Observable, map } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class AuthService { | ||
private auth: Auth = inject(Auth); | ||
private isAdminSubject = new BehaviorSubject<boolean>(false); | ||
isAdmin$ = this.isAdminSubject.asObservable(); | ||
user$ = user(this.auth); | ||
userSubscription: Subscription; | ||
isLoggedIn$: Observable<boolean>; | ||
|
||
constructor() { | ||
this.userSubscription = this.user$.subscribe((aUser: any | null) => { | ||
console.log(aUser); | ||
}) | ||
this.isLoggedIn$ = this.user$.pipe(map(user => !!user)); | ||
this.user$.subscribe(user => this.checkAdminStatus(user?.email)); | ||
} | ||
|
||
getCurrentUser(){ | ||
return this.auth.currentUser; | ||
} | ||
|
||
async register(email : string,password : string){ | ||
var userCredential = await createUserWithEmailAndPassword(this.auth, email, password) | ||
|
||
return sendEmailVerification(userCredential.user) | ||
async login(email: string, password: string): Promise<void> { | ||
await signInWithEmailAndPassword(this.auth, email, password); | ||
} | ||
|
||
login(email : string,password : string){ | ||
return signInWithEmailAndPassword(this.auth,email, password); | ||
async register(email: string, password: string): Promise<void> { | ||
await createUserWithEmailAndPassword(this.auth, email, password); | ||
} | ||
|
||
logout(){ | ||
this.auth.signOut() | ||
async logout(): Promise<void> { | ||
await signOut(this.auth); | ||
} | ||
|
||
passwordReset(email : string){ | ||
sendPasswordResetEmail(this.auth, email) | ||
.then(() => { | ||
// Password reset email sent! | ||
// .. | ||
}) | ||
.catch((error) => { | ||
const errorCode = error.code; | ||
const errorMessage = error.message; | ||
// .. | ||
}); | ||
private checkAdminStatus(email: string | null | undefined): void { | ||
this.isAdminSubject.next(email?.toLowerCase() === '[email protected]'); | ||
} | ||
|
||
isLoggedIn(): boolean { | ||
return !!this.auth.currentUser; | ||
} | ||
|
||
ngOnDestroy() { | ||
this.auth.signOut() | ||
getCurrentUser() { | ||
return this.auth.currentUser; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Language } from "./language.model"; | ||
|
||
export interface Game { | ||
id: string; | ||
img: string; | ||
alt: string; | ||
name: string; | ||
genre: string; | ||
description: string; | ||
language: Language; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Game } from "./game.model"; | ||
|
||
export interface Language { | ||
id?: string; | ||
name: string; | ||
flag: string; | ||
culturalCuriosities: string[]; | ||
games: Game[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface Review { | ||
id?: string; | ||
userId: string; | ||
gameId: string; | ||
rating: number; | ||
comment: string; | ||
userName: string; | ||
createdAt: Date; | ||
} |
2 changes: 1 addition & 1 deletion
2
...app/core/services/product.service.spec.ts → ...p/core/services/firestore.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { inject, Injectable } from '@angular/core'; | ||
import { collection, collectionData, Firestore, doc, docData, setDoc, deleteDoc } from '@angular/fire/firestore'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class FirestoreService { | ||
firestore: Firestore = inject(Firestore); | ||
|
||
constructor() {} | ||
|
||
getCollection<T>(path: string): Observable<T[]> { | ||
return collectionData(collection(this.firestore, path), { | ||
idField: 'id', | ||
}) as Observable<T[]>; | ||
} | ||
|
||
getDocument<T>(path: string, id: string): Observable<T> { | ||
return docData(doc(this.firestore, path, id), { idField: 'id' }) as Observable<T>; | ||
} | ||
|
||
createDocument<T>( | ||
path: string, | ||
id: string, | ||
data: any | ||
): Observable<void> { // Alterado para retornar um Observable | ||
const docRef = doc(this.firestore, path, id); | ||
return new Observable<void>((observer) => { | ||
setDoc(docRef, { ...data }).then(() => { | ||
observer.next(); // Notifica que a operação foi concluída | ||
observer.complete(); // Completa o Observable | ||
}).catch((error) => observer.error(error)); // Notifica erro, se ocorrer | ||
}); | ||
} | ||
|
||
generateId(path: string, id?: string): Observable<string> { | ||
const taskCollection = collection(this.firestore, path); | ||
const docRef = id ? doc(taskCollection, id) : doc(taskCollection); | ||
return new Observable<string>((observer) => { | ||
observer.next(docRef.id); | ||
observer.complete(); | ||
}); | ||
} | ||
|
||
updateDocument<T>(path: string, id: string, data: Partial<T>): Observable<void> { | ||
const docRef = doc(this.firestore, path, id); | ||
return new Observable<void>((observer) => { | ||
setDoc(docRef, data, { merge: true }).then(() => { | ||
observer.next(); | ||
observer.complete(); | ||
}).catch((error) => observer.error(error)); | ||
}); | ||
} | ||
|
||
deleteDocument(path: string, id: string): Observable<void> { | ||
const docRef = doc(this.firestore, path, id); | ||
return new Observable<void>((observer) => { | ||
deleteDoc(docRef).then(() => { | ||
observer.next(); | ||
observer.complete(); | ||
}).catch((error) => observer.error(error)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { GameService } from './game.service'; | ||
|
||
describe('GameService', () => { | ||
let service: GameService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(GameService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { FirestoreService } from './firestore.service'; | ||
import { Observable } from 'rxjs'; | ||
import { Game } from '../models/game.model'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class GameService { | ||
private path = 'games'; // Nome da coleção no Firestore | ||
|
||
constructor(private firestoreService: FirestoreService) {} | ||
|
||
getGames(): Observable<Game[]> { | ||
return this.firestoreService.getCollection<Game>(this.path); | ||
} | ||
|
||
getGame(id: string): Observable<Game> { | ||
return this.firestoreService.getDocument<Game>(this.path, id); | ||
} | ||
|
||
createGame(game: Game): Observable<void> { | ||
const id = this.firestoreService.generateId(this.path); | ||
return new Observable<void>((observer) => { | ||
id.subscribe((generatedId) => { | ||
this.firestoreService.createDocument<Game>(this.path, generatedId, { ...game, id: generatedId }) | ||
.subscribe({ | ||
next: () => { | ||
observer.next(); | ||
observer.complete(); | ||
}, | ||
error: (error) => observer.error(error) | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
updateGame(id: string, game: Partial<Game>): Observable<void> { | ||
return this.firestoreService.updateDocument<Game>(this.path, id, game); | ||
} | ||
|
||
deleteGame(id: string): Observable<void> { | ||
return this.firestoreService.deleteDocument(this.path, id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { LanguageService } from './language.service'; | ||
|
||
describe('LanguageService', () => { | ||
let service: LanguageService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(LanguageService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.