User Profile
+Hello: {{(user.displayName) ? user.displayName : 'User'}}
+User ID: {{user.uid}}
+Email: {{user.email}}
+Email Verified: {{user.emailVerified}}
+diff --git a/angular.json b/angular.json index aa62810..c6a04ca 100644 --- a/angular.json +++ b/angular.json @@ -28,6 +28,7 @@ "src/assets" ], "styles": [ + "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/styles.scss" ], "scripts": [] @@ -104,4 +105,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/app/account/forgot-password/forgot-password.component.html b/src/app/account/forgot-password/forgot-password.component.html new file mode 100644 index 0000000..ad2533a --- /dev/null +++ b/src/app/account/forgot-password/forgot-password.component.html @@ -0,0 +1,19 @@ +
Please enter your email address to request a password reset.
+We have sent a confirmation email to {{user.email}}.
+Please check your email and click on the link to verfiy your email address.
+User ID: {{user.uid}}
+Email: {{user.email}}
+Email Verified: {{user.emailVerified}}
+header works!
diff --git a/src/app/header/header.component.ts b/src/app/header/header.component.ts deleted file mode 100644 index 7ab4cf7..0000000 --- a/src/app/header/header.component.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Component, OnInit } from '@angular/core'; - -@Component({ - selector: 'app-header', - templateUrl: './header.component.html', - styleUrls: ['./header.component.scss'] -}) -export class HeaderComponent implements OnInit { - - constructor() { } - - ngOnInit(): void { - } - -} diff --git a/src/app/services/auth.service.spec.ts b/src/app/services/auth.service.spec.ts new file mode 100644 index 0000000..f1251ca --- /dev/null +++ b/src/app/services/auth.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts new file mode 100644 index 0000000..2d61597 --- /dev/null +++ b/src/app/services/auth.service.ts @@ -0,0 +1,129 @@ +import { Injectable, NgZone } from '@angular/core'; +import { AngularFireAuth } from '@angular/fire/compat/auth'; +import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/compat/firestore'; +import * as auth from 'firebase/auth'; +import { Router } from '@angular/router'; +import { User } from './user'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthService { + userData: any; + + + // Inject Firestore service, Firebase auth service, NgZone service to remove outside scope warning + constructor(public firestore: AngularFirestore, public authentication: AngularFireAuth, public router: Router, public ngZone: NgZone) { + /* Saving user data in localstorage when + logged in and setting up null when logged out */ + + this.authentication.authState.subscribe((user) => { + if (user) { + this.userData = user; + localStorage.setItem('user', JSON.stringify(this.userData)); + JSON.parse(localStorage.getItem('user')!); + } else { + localStorage.setItem('user', 'null'); + JSON.parse(localStorage.getItem('user')!); + } + }); + } + // Sign in with email/password + SignIn(email: string, password: string) { + return this.authentication + .signInWithEmailAndPassword(email, password) + .then((result) => { + this.SetUserData(result.user); + this.authentication.authState.subscribe((user) => { + if (user) { + this.router.navigate(['dashboard']); + } + }); + }) + .catch((error) => { + window.alert(error.message); + }); + } + // Sign up with email/password + SignUp(email: string, password: string) { + return this.authentication + .createUserWithEmailAndPassword(email, password) + .then((result) => { + /* Call the SendVerificaitonMail() function when new user sign + up and returns promise */ + this.SendVerificationMail(); + this.SetUserData(result.user); + }) + .catch((error) => { + window.alert(error.message) + }); + } + + // Send email verfificaiton when new user sign up + SendVerificationMail() { + return this.authentication.currentUser + .then((u: any) => u.sendEmailVerification()) + .then(() => { + this.router.navigate(['verify-email-address']); + }); + } + // Reset Forggot password + ForgotPassword(passwordResetEmail: string) { + return this.authentication + .sendPasswordResetEmail(passwordResetEmail) + .then(() => { + window.alert('Password reset mail sent, check your inbox.'); + }) + .catch((error) => { + window.alert(error) + }); + } + // Returns true when user is looged in and email is verified + get isLoggetIn(): boolean { + const user = JSON.parse(localStorage.getItem('user')!); + return (user !== null && user.emailVerified !== false) ? true : false; + } + // Sign in with Google + GoogleAuth() { + return this.AuthLogin(new auth.GoogleAuthProvider()).then((res: any) => { + this.router.navigate(['dashboard']); + }); + } + // Auth logic to run auth providers + AuthLogin(provider: any) { + return this.authentication + .signInWithPopup(provider) + .then((result) => { + this.router.navigate(['dashboard']); + this.SetUserData(result.user); + }) + .catch((error) => { + window.alert(error); + }); + } + /* Setting up user data when sign in with username/password, + sign up with username/password and sign in with social auth + provider in Firestore database using AngularFirestore + AngularFirestoreDocument service */ + SetUserData(user: any) { + const userRef: AngularFirestoreDocument