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