firebase auth
This commit is contained in:
parent
f3c165173f
commit
01401c2652
@ -3,11 +3,11 @@ import { provideRouter } from '@angular/router';
|
|||||||
|
|
||||||
import { routes } from './app-routing.module';
|
import { routes } from './app-routing.module';
|
||||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||||
import { initializeApp, provideFirebaseApp } from '@angular/fire/app';
|
import { AngularFireModule } from '@angular/fire/compat';
|
||||||
import { getAuth, provideAuth } from '@angular/fire/auth';
|
import { getAuth, provideAuth } from '@angular/fire/auth';
|
||||||
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
|
import { getFirestore, provideFirestore } from '@angular/fire/firestore';
|
||||||
import { getStorage, provideStorage } from '@angular/fire/storage';
|
import { getStorage, provideStorage } from '@angular/fire/storage';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [provideRouter(routes), provideAnimationsAsync(), importProvidersFrom(provideFirebaseApp(() => initializeApp({ "projectId": "quack-1", "appId": "1:697758733170:web:433bd10dcb3566c06683eb", "storageBucket": "quack-1.appspot.com", "apiKey": "AIzaSyA4GSbuT6SNs_SPXvwVxyRyLcU_tlV9qo0", "authDomain": "quack-1.firebaseapp.com", "messagingSenderId": "697758733170" }))), importProvidersFrom(provideAuth(() => getAuth())), importProvidersFrom(provideFirestore(() => getFirestore())), importProvidersFrom(provideStorage(() => getStorage()))]
|
providers: [provideRouter(routes), provideAnimationsAsync(), importProvidersFrom(AngularFireModule.initializeApp({ "projectId": "quack-1", "appId": "1:697758733170:web:433bd10dcb3566c06683eb", "storageBucket": "quack-1.appspot.com", "apiKey": "AIzaSyA4GSbuT6SNs_SPXvwVxyRyLcU_tlV9qo0", "authDomain": "quack-1.firebaseapp.com", "messagingSenderId": "697758733170" })), importProvidersFrom(provideAuth(() => getAuth())), importProvidersFrom(provideFirestore(() => getFirestore())), importProvidersFrom(provideStorage(() => getStorage()))]
|
||||||
};
|
};
|
||||||
|
@ -3,7 +3,6 @@ import { CommonModule } from '@angular/common';
|
|||||||
|
|
||||||
import { AppRoutingModule } from './app-routing.module';
|
import { AppRoutingModule } from './app-routing.module';
|
||||||
|
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [],
|
declarations: [],
|
||||||
imports: [
|
imports: [
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<label for="username">Username:</label>
|
<label for="username">Email:</label>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" formControlName="username">
|
<input type="text" formControlName="email">
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -10,7 +10,7 @@ import { AuthService } from '../../services/auth/auth.service';
|
|||||||
})
|
})
|
||||||
export class LoginComponent implements OnInit {
|
export class LoginComponent implements OnInit {
|
||||||
loginForm = new FormGroup({
|
loginForm = new FormGroup({
|
||||||
username: new FormControl(''),
|
email: new FormControl('', [Validators.email]),
|
||||||
password: new FormControl('')
|
password: new FormControl('')
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -19,17 +19,18 @@ export class LoginComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.auth.getToken().then(()=> { this.router.navigateByUrl("/feed") })
|
if (!this.auth.checkAllowed())
|
||||||
|
this.router.navigateByUrl("/feed");
|
||||||
}
|
}
|
||||||
|
|
||||||
login() {
|
login() {
|
||||||
if (this.loginForm.valid)
|
if (this.loginForm.valid)
|
||||||
this.auth.login(
|
this.auth.login(
|
||||||
this.loginForm.get('username')?.value as String,
|
this.loginForm.get('email')?.value as String,
|
||||||
this.loginForm.get('password')?.value as String
|
this.loginForm.get('password')?.value as String
|
||||||
)
|
)
|
||||||
.then(()=>{ this.router.navigateByUrl("/feed") })
|
.then(() => { this.router.navigateByUrl("/feed") })
|
||||||
.catch(()=>{ alert("wrong username or password") });
|
.catch(() => { alert("wrong username or password") });
|
||||||
else
|
else
|
||||||
alert("missing username or password");
|
alert("missing username or password");
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<label for="username">Username:</label>
|
<label for="username">Email:</label>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" formControlName="username">
|
<input type="text" formControlName="email">
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -10,7 +10,7 @@ import { Router } from '@angular/router';
|
|||||||
})
|
})
|
||||||
export class RegisterComponent {
|
export class RegisterComponent {
|
||||||
registerForm = new FormGroup({
|
registerForm = new FormGroup({
|
||||||
username: new FormControl(''),
|
email: new FormControl('', [Validators.email]),
|
||||||
password: new FormControl(''),
|
password: new FormControl(''),
|
||||||
rePassword: new FormControl('')
|
rePassword: new FormControl('')
|
||||||
});
|
});
|
||||||
@ -22,7 +22,7 @@ export class RegisterComponent {
|
|||||||
register() {
|
register() {
|
||||||
if (this.registerForm.valid)
|
if (this.registerForm.valid)
|
||||||
this.auth.register(
|
this.auth.register(
|
||||||
this.registerForm.get('username')?.value as String,
|
this.registerForm.get('email')?.value as String,
|
||||||
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,
|
||||||
)
|
)
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
import { Injectable, inject } from '@angular/core';
|
import { Injectable, inject } from '@angular/core';
|
||||||
import { ActivatedRouteSnapshot, CanActivateFn, GuardResult, MaybeAsync, Router, RouterStateSnapshot } from '@angular/router';
|
import { AngularFireAuth } from '@angular/fire/compat/auth';
|
||||||
|
import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
token?: String;
|
token?: String;
|
||||||
|
constructor(private router: Router, private auth: AngularFireAuth) { }
|
||||||
constructor(private router: Router) { }
|
|
||||||
|
|
||||||
checkAllowed() {
|
checkAllowed() {
|
||||||
// TODO: check if the token is still valid
|
if (this.auth.user)
|
||||||
if (this.token)
|
|
||||||
return true;
|
return true;
|
||||||
else {
|
else {
|
||||||
this.router.navigateByUrl('/auth/login');
|
this.router.navigateByUrl('/auth/login');
|
||||||
@ -19,46 +18,20 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getToken(): Promise<String> {
|
getToken() {
|
||||||
// TODO: check if the token is still valid
|
return this.auth.user;
|
||||||
return new Promise<String>((resolve, reject) => {
|
|
||||||
if (this.token)
|
|
||||||
resolve(this.token);
|
|
||||||
else
|
|
||||||
reject();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
login(username: String, password: String): Promise<Boolean> {
|
login(username: String, password: String) {
|
||||||
return new Promise<Boolean>((resolve, reject) => {
|
return this.auth.signInWithEmailAndPassword(username as string, password as string);
|
||||||
// TODO: authenticate
|
|
||||||
if (username === "test" && password === "test") {
|
|
||||||
this.token = "valid_token";
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.token = undefined;
|
|
||||||
reject();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logout(): Promise<Boolean> {
|
logout() {
|
||||||
return new Promise<Boolean>((resolve, reject) => {
|
return this.auth.signOut();
|
||||||
// TODO: logout
|
|
||||||
this.token = undefined;
|
|
||||||
resolve(true);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
register(username: String, password: String, rePassword: String): Promise<Boolean> {
|
register(username: String, password: String, rePassword: String) {
|
||||||
return new Promise<Boolean>((resolve, reject) => {
|
return this.auth.createUserWithEmailAndPassword(username as string, password as string);
|
||||||
// TODO: register
|
|
||||||
if (username !== "test" && password === rePassword)
|
|
||||||
resolve(true);
|
|
||||||
else
|
|
||||||
reject();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,30 +59,29 @@ export class PostService {
|
|||||||
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
|
// TODO: fetch feed
|
||||||
this.auth.getToken()
|
if (this.auth.checkAllowed()) {
|
||||||
.then((token) => {
|
resolve(this.examplePosts)
|
||||||
resolve(this.examplePosts);
|
} else {
|
||||||
})
|
reject()
|
||||||
.catch(() => { reject() });
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getPosts(username: String, lenght: Number, start: Number) {
|
getPosts(username: String, lenght: Number, start: Number) {
|
||||||
return new Promise<Array<Post>>((resolve, reject) => {
|
return new Promise<Array<Post>>((resolve, reject) => {
|
||||||
// TODO: fetch feed
|
// TODO: fetch feed
|
||||||
this.auth.getToken()
|
if (this.auth.checkAllowed()) {
|
||||||
.then((token) => {
|
resolve(this.examplePosts)
|
||||||
resolve(this.examplePosts);
|
} else {
|
||||||
})
|
reject()
|
||||||
.catch(() => { reject() });
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
newPost(content: String, image: String) {
|
newPost(content: String, image: String) {
|
||||||
return new Promise<Boolean>((resolve, reject) => {
|
return new Promise<Boolean>((resolve, reject) => {
|
||||||
// TODO: make post
|
// TODO: make post
|
||||||
this.auth.getToken()
|
if (this.auth.checkAllowed()) {
|
||||||
.then((token) => {
|
|
||||||
this.examplePosts.push({
|
this.examplePosts.push({
|
||||||
user: {
|
user: {
|
||||||
username: "Zámbó Jimmy",
|
username: "Zámbó Jimmy",
|
||||||
@ -93,10 +92,11 @@ export class PostService {
|
|||||||
image: image,
|
image: image,
|
||||||
likes: 0,
|
likes: 0,
|
||||||
liked: false
|
liked: false
|
||||||
})
|
});
|
||||||
resolve(true);
|
resolve(true);
|
||||||
})
|
} else {
|
||||||
.catch(() => { reject() })
|
reject();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,11 @@ export class UserService {
|
|||||||
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
|
// TODO: get user
|
||||||
this.auth.getToken()
|
if (this.auth.checkAllowed()) {
|
||||||
.then((token) => {
|
|
||||||
resolve(this.exampleUser);
|
resolve(this.exampleUser);
|
||||||
})
|
} else {
|
||||||
.catch(() => { reject() });
|
reject();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user