firestore
This commit is contained in:
parent
01401c2652
commit
df27bf8d81
@ -9,6 +9,14 @@
|
||||
<input type="text" formControlName="email">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label for="username">Username:</label>
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" formControlName="username">
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label for="password">Password:</label>
|
||||
|
@ -2,6 +2,7 @@ import { Component } from '@angular/core';
|
||||
import { FormControl, FormGroup, Validators } from '@angular/forms';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
import { Router } from '@angular/router';
|
||||
import { UserService } from '../../services/data/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-register',
|
||||
@ -12,10 +13,11 @@ export class RegisterComponent {
|
||||
registerForm = new FormGroup({
|
||||
email: new FormControl('', [Validators.email]),
|
||||
password: new FormControl(''),
|
||||
username: new FormControl(''),
|
||||
rePassword: new FormControl('')
|
||||
});
|
||||
|
||||
constructor(private router: Router, private auth: AuthService) {
|
||||
constructor(private router: Router, private auth: AuthService, private user: UserService) {
|
||||
this.registerForm.addValidators([Validators.required, Validators.minLength(3)]);
|
||||
}
|
||||
|
||||
@ -26,9 +28,17 @@ export class RegisterComponent {
|
||||
this.registerForm.get('password')?.value as String,
|
||||
this.registerForm.get('rePassword')?.value as String,
|
||||
)
|
||||
.then(()=> { this.router.navigateByUrl('/auth/login') })
|
||||
.catch(()=> { alert('failed to register') });
|
||||
// TODO: better error messages
|
||||
.then((cred) => {
|
||||
this.user.createUser(
|
||||
this.registerForm.get('username')?.value as String,
|
||||
this.registerForm.get('email')?.value as String,
|
||||
cred.user?.uid as String
|
||||
).then(() => { this.router.navigateByUrl('/auth/login') }).catch(
|
||||
() => { alert('failed to create user'); }
|
||||
)
|
||||
})
|
||||
.catch(() => { alert('failed to register') });
|
||||
// TODO: better error messages
|
||||
else
|
||||
alert("failed to register");
|
||||
}
|
||||
|
@ -1,66 +1,29 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Post } from '../../../model/Post';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { AngularFirestore, Reference } from '@angular/fire/compat/firestore';
|
||||
import { User } from '../../../model/User';
|
||||
import { UserService } from './user.service';
|
||||
import { user } from '@angular/fire/auth';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class PostService {
|
||||
|
||||
constructor(private auth: AuthService) { }
|
||||
|
||||
examplePosts: Array<Post> = new Array<Post>(
|
||||
{
|
||||
user: {
|
||||
username: "Zámbó Jimmy",
|
||||
picture: "assets/placeholder-profile-picture.png",
|
||||
followed: true
|
||||
},
|
||||
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
|
||||
image: "assets/placeholder-profile-picture.png",
|
||||
likes: 1,
|
||||
liked: true
|
||||
},
|
||||
{
|
||||
user: {
|
||||
username: "Zámbó Jimmy",
|
||||
picture: "assets/placeholder-profile-picture.png",
|
||||
followed: true
|
||||
},
|
||||
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
|
||||
image: "assets/placeholder-profile-picture.png",
|
||||
likes: 1,
|
||||
liked: true
|
||||
},
|
||||
{
|
||||
user: {
|
||||
username: "Zámbó Jimmy",
|
||||
picture: "assets/placeholder-profile-picture.png",
|
||||
followed: true
|
||||
},
|
||||
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
|
||||
image: "assets/placeholder-profile-picture.png",
|
||||
likes: 1,
|
||||
liked: true
|
||||
},
|
||||
{
|
||||
user: {
|
||||
username: "Zámbó Jimmy",
|
||||
picture: "assets/placeholder-profile-picture.png",
|
||||
followed: true
|
||||
},
|
||||
content: "Bukott diák vagyok én, nem bírom el a szenvedést.",
|
||||
image: "assets/placeholder-profile-picture.png",
|
||||
likes: 1,
|
||||
liked: true
|
||||
},
|
||||
);
|
||||
constructor(private auth: AuthService, private afs: AngularFirestore) { }
|
||||
|
||||
getFeed(length: Number, start: Number): Promise<Array<Post>> {
|
||||
return new Promise<Array<Post>>((resolve, reject) => {
|
||||
// TODO: fetch feed
|
||||
if (this.auth.checkAllowed()) {
|
||||
resolve(this.examplePosts)
|
||||
this.afs.collection<Post>('post').valueChanges().subscribe(data => {
|
||||
data.forEach(i => {
|
||||
this.afs.doc((i as any).user).valueChanges().subscribe(user => {
|
||||
i.user = user as User;
|
||||
});
|
||||
});
|
||||
resolve(data);
|
||||
});
|
||||
} else {
|
||||
reject()
|
||||
}
|
||||
@ -68,32 +31,52 @@ export class PostService {
|
||||
}
|
||||
|
||||
getPosts(username: String, lenght: Number, start: Number) {
|
||||
console.log(username)
|
||||
return new Promise<Array<Post>>((resolve, reject) => {
|
||||
// TODO: fetch feed
|
||||
if (this.auth.checkAllowed()) {
|
||||
resolve(this.examplePosts)
|
||||
if (username == undefined) {
|
||||
this.auth.getToken().subscribe(cred => {
|
||||
this.afs.collection<User>('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => {
|
||||
this.afs.collection<Post>('post', ref => ref.where('user', '==', this.afs.doc('/user/' + user[0].username).ref)).valueChanges().subscribe(data => {
|
||||
data.forEach(i => {
|
||||
i.user = user[0] as User;
|
||||
})
|
||||
resolve(data)
|
||||
});
|
||||
})
|
||||
});
|
||||
} else {
|
||||
this.afs.collection<User>('user').doc(username as string).valueChanges().subscribe(user => {
|
||||
this.afs.collection<Post>('post', ref => ref.where('user', '==', this.afs.doc('/user/' + username).ref)).valueChanges().subscribe(data => {
|
||||
data.forEach(i => {
|
||||
i.user = user as User;
|
||||
})
|
||||
resolve(data)
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
reject()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// FIX: duplicate posts
|
||||
newPost(content: String, image: String) {
|
||||
return new Promise<Boolean>((resolve, reject) => {
|
||||
// TODO: make post
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
console.log('new post')
|
||||
if (this.auth.checkAllowed()) {
|
||||
this.examplePosts.push({
|
||||
user: {
|
||||
username: "Zámbó Jimmy",
|
||||
picture: "assets/placeholder-profile-picture.png",
|
||||
followed: true
|
||||
},
|
||||
content: content,
|
||||
image: image,
|
||||
likes: 0,
|
||||
liked: false
|
||||
this.auth.getToken().subscribe(data => {
|
||||
this.afs.collection<User>('user', ref => ref.where('id', '==', data?.uid)).valueChanges().subscribe(user => {
|
||||
this.afs.collection('post').add({
|
||||
user: this.afs.doc('/user/' + user[0].username).ref,
|
||||
content: content,
|
||||
image: image,
|
||||
})
|
||||
.then(() => resolve())
|
||||
.catch(() => reject());
|
||||
});
|
||||
});
|
||||
resolve(true);
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
|
@ -1,28 +1,40 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { User } from '../../../model/User';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { AngularFirestore } from '@angular/fire/compat/firestore';
|
||||
import { user } from '@angular/fire/auth';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserService {
|
||||
|
||||
exampleUser: User = {
|
||||
username: "Zámbó Jimmy",
|
||||
picture: "assets/placeholder-profile-picture.png",
|
||||
followed: true
|
||||
}
|
||||
|
||||
constructor(private auth: AuthService) { }
|
||||
constructor(private auth: AuthService, private afs: AngularFirestore) { }
|
||||
|
||||
getUser(username: String): Promise<User> {
|
||||
return new Promise<User>((resolve, reject) => {
|
||||
// TODO: get user
|
||||
if (this.auth.checkAllowed()) {
|
||||
resolve(this.exampleUser);
|
||||
if (username != undefined) {
|
||||
this.afs.collection<User>('user', ref => ref.where('username', '==', username)).valueChanges().subscribe(data => { resolve(data[0] as User) });
|
||||
} else {
|
||||
this.auth.getToken().subscribe(cred => {
|
||||
this.afs.collection<User>('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { resolve(user[0]) })
|
||||
});
|
||||
}
|
||||
} else {
|
||||
reject();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
createUser(username: String, email: String, id: String) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
this.afs.collection('user').doc(username as string).set({
|
||||
username: username,
|
||||
email: email,
|
||||
picture: "/assets/placeholder-profile-picture.png",
|
||||
id: id
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
export interface User {
|
||||
username: String;
|
||||
email: String;
|
||||
picture: String;
|
||||
followed: Boolean;
|
||||
}
|
Loading…
Reference in New Issue
Block a user