Login if user exist

This commit is contained in:
Walter Jay 2019-06-04 11:07:28 +09:00
parent 38afecced9
commit 3592ba57bc
10 changed files with 184 additions and 175 deletions

130
package-lock.json generated
View File

@ -430,9 +430,9 @@
}
},
"@angular/fire": {
"version": "5.1.3",
"resolved": "https://registry.npmjs.org/@angular/fire/-/fire-5.1.3.tgz",
"integrity": "sha512-s9tdSDS0ZLWKax5atMSxpJWehQ59bKgbzy8uZJVo1vyJw/eshBduuCZbETBj6RmwWhKgrBfgS2odNwKxArE25w=="
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/@angular/fire/-/fire-5.2.1.tgz",
"integrity": "sha512-pS4zWhLLANzMbYVIKNtebDQKhm9+KANUDvDV6DwKP24XHzkZdvazKe1HC6uXWlf7QmDxSPFleCSBkn0tnQQzdQ=="
},
"@angular/flex-layout": {
"version": "7.0.0-beta.24",
@ -649,13 +649,13 @@
}
},
"@firebase/app": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/@firebase/app/-/app-0.4.1.tgz",
"integrity": "sha512-NHzEMPXWRXmDMMhFbCqElOkoVo5mScw81KzhqiEkU7e0v1Ia0P0fztfwqIzawvJtYW158UFSU7Q+EbpzthImqQ==",
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/@firebase/app/-/app-0.4.3.tgz",
"integrity": "sha512-UB/CLBU9ONA0m9ajPJHtHHSl/6nNQfQ0wvnpTuHFuy7e/0jeKIuBeE+18DGyCBetv20T1/1EXDtv8YF3KISong==",
"requires": {
"@firebase/app-types": "0.4.0",
"@firebase/logger": "0.1.14",
"@firebase/util": "0.2.15",
"@firebase/util": "0.2.17",
"dom-storage": "2.1.0",
"tslib": "1.9.3",
"xmlhttprequest": "1.8.0"
@ -680,13 +680,13 @@
"integrity": "sha512-QEG9azYwssGWcb4NaKFHe3Piez0SG46nRlu76HM4/ob0sjjNpNTY1Z5C3IoeJYknp2kMzuQi0TTW8tjEgkUAUA=="
},
"@firebase/database": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/@firebase/database/-/database-0.4.1.tgz",
"integrity": "sha512-3hCq5m1JOvg037Yci471LA+1B7PnmiZDiwztNZ9a2U7t28VXGXkf4ECriG7UyxWoR6yTFWUUQeaBTr4SJ78fhA==",
"version": "0.4.3",
"resolved": "https://registry.npmjs.org/@firebase/database/-/database-0.4.3.tgz",
"integrity": "sha512-dJm76D/+L5o0h61B1CoM039/h2SxppvbKV9HbDKo4JsGbN2FOru27XXC3/JLWauILq38bxk95vzIpMqlPWcDSw==",
"requires": {
"@firebase/database-types": "0.4.0",
"@firebase/logger": "0.1.14",
"@firebase/util": "0.2.15",
"@firebase/util": "0.2.17",
"faye-websocket": "0.11.1",
"tslib": "1.9.3"
},
@ -707,9 +707,9 @@
"integrity": "sha512-2piRYW7t+2s/P1NPpcI/3+8Y5l2WnJhm9KACoXW5zmoAPlya8R1aEaR2dNHLNePTMHdg04miEDD9fEz4xUqzZA=="
},
"@firebase/firestore": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/@firebase/firestore/-/firestore-1.3.1.tgz",
"integrity": "sha512-cr7qpGIphewqB4uMi/JeXFaHaqA+v0VeHRkTn/f8LC4npQgbq9r1yOumQpcJEJH0POfkMGZjVdpb+knzjM8qUQ==",
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/@firebase/firestore/-/firestore-1.3.4.tgz",
"integrity": "sha512-qETQ+vTVrTmP66fI8RFvfGLp+xFzgXWqR5IXuSWtF4+xk69gMImE6KkT2KnpzEbpu2AqZ2JFko/lXsk5OtQDbA==",
"requires": {
"@firebase/firestore-types": "1.3.0",
"@firebase/logger": "0.1.14",
@ -725,12 +725,12 @@
"integrity": "sha512-XPnfAaYsKgYivgl/U1+M5ulBG9Hxv52zrZR5TuaoKCU791t/E3K85rT1ZGtEHu9Fj4CPTep2NSl8I30MQpUlHA=="
},
"@firebase/functions": {
"version": "0.4.7",
"resolved": "https://registry.npmjs.org/@firebase/functions/-/functions-0.4.7.tgz",
"integrity": "sha512-k0Rn2c1Nsb90Qwk9O2JCSPoH6ozxyp0WZgIPU2OoLzYrrg+fVdcEnG3Sb1YhlOU5yRA9Nuxh1pPBQA3/e/A98g==",
"version": "0.4.8",
"resolved": "https://registry.npmjs.org/@firebase/functions/-/functions-0.4.8.tgz",
"integrity": "sha512-b/56f5ThflgyL3Hmd7ifKBYRbrr97MRMVHr1WTvHKM/Dg5K7yWO219mG8Er9z4PuRQh2aulfJ1KQD6X7d+Aqfg==",
"requires": {
"@firebase/functions-types": "0.3.5",
"@firebase/messaging-types": "0.2.11",
"@firebase/messaging-types": "0.3.0",
"isomorphic-fetch": "2.2.1",
"tslib": "1.9.3"
}
@ -741,19 +741,20 @@
"integrity": "sha512-3hTMqfSugCfxzT6vZPbzQ58G4941rsFr99fWKXGKFAl2QpdMBCnKmEKdg/p5M47xIPyzIQn6NMF5kCo/eICXhA=="
},
"@firebase/installations": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@firebase/installations/-/installations-0.1.1.tgz",
"integrity": "sha512-7qg0iSYs/XhvfXF8m8fMc44ViEPbaTpKpvD6A6bN3VUjaTg+un7bROEGtfP8xboTbqdN80gjiKnHI+Ibs5KxCw==",
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/@firebase/installations/-/installations-0.1.4.tgz",
"integrity": "sha512-0yKKEXCtciBTe41hF77Q1aY5IB4QjohlupRRLHqw0CQQUZ2L94halNLqtw8iptltc2IHPSH68HGXlFTSdmJlHQ==",
"requires": {
"@firebase/installations-types": "0.1.0",
"@firebase/util": "0.2.15",
"idb": "3.0.2"
"@firebase/installations-types": "0.1.1",
"@firebase/util": "0.2.17",
"idb": "3.0.2",
"tslib": "1.9.3"
}
},
"@firebase/installations-types": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@firebase/installations-types/-/installations-types-0.1.0.tgz",
"integrity": "sha512-cw2UIvPa3+umy6w7dGj0LqQQ9v7WEOro5s+6B+v54Tw25WyLnR6cBIkyyahv3Uu9QPnGZCIsemlQwxIaIOMb9g=="
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@firebase/installations-types/-/installations-types-0.1.1.tgz",
"integrity": "sha512-M+plQIOt6p+/j/ExUgsfXe1JFAKymhBU0K3+cp7hzj52vLSpklOqNJi4LkFl41pgRFPZeKf7MrTkMhVowg3Ukw=="
},
"@firebase/logger": {
"version": "0.1.14",
@ -761,29 +762,29 @@
"integrity": "sha512-WREaY2n6HzypeoovOjYefjLJqT9+zlI1wQlLMXnkSPhwuM+udIQ87orjVL6tfmuHW++u5bZh3JJAyvuRv/nciA=="
},
"@firebase/messaging": {
"version": "0.3.20",
"resolved": "https://registry.npmjs.org/@firebase/messaging/-/messaging-0.3.20.tgz",
"integrity": "sha512-0mJpaSFoineVFnaYTHVP4k/PANEEmZhES9fPd44Ir6b9mR4FhJip5ojZl0vlyMe5rZWCLuxYkfFcyzEn2afNKw==",
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/@firebase/messaging/-/messaging-0.4.0.tgz",
"integrity": "sha512-wi4t7i4VjYkXF9CBd9iL3gUe+DvhZbbmhuYzopBL3sWxKTVWmOG8W4WapYKgx9RyluscZqAZJbL3YtpUtqcKkw==",
"requires": {
"@firebase/messaging-types": "0.2.11",
"@firebase/util": "0.2.15",
"@firebase/messaging-types": "0.3.0",
"@firebase/util": "0.2.17",
"tslib": "1.9.3"
}
},
"@firebase/messaging-types": {
"version": "0.2.11",
"resolved": "https://registry.npmjs.org/@firebase/messaging-types/-/messaging-types-0.2.11.tgz",
"integrity": "sha512-uWtzPMj1mAX8EbG68SnxE12Waz+hRuO7vtosUFePGBfxVNNmPx5vJyKZTz+hbM4P77XddshAiaEkyduro4gNgA=="
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@firebase/messaging-types/-/messaging-types-0.3.0.tgz",
"integrity": "sha512-xCFMPy4C+WXFcshTnQEyddmqM6ZkzpTeJq7RUhrAvUnjlfFzOB92HOfKtjT6IpNk5W+jNbTTrqgrgReuPXsM2A=="
},
"@firebase/performance": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/@firebase/performance/-/performance-0.2.2.tgz",
"integrity": "sha512-wpH9Nac8CadEfyrr8u8QQO+HfQ7ndkrOSesXDtIbbVOify9A2sCdwWuIWPxMAclevgSp99zVj2yjd7sv6M3jVQ==",
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/@firebase/performance/-/performance-0.2.5.tgz",
"integrity": "sha512-9uN7kKAMMEWcO6mW2+JIAi8SUuAmS6QuVje623mnS/OE/oT/Fy43TsZI2yxq8wmQEj8i871vSPvA1ff6Kze1sg==",
"requires": {
"@firebase/installations": "0.1.1",
"@firebase/installations": "0.1.4",
"@firebase/logger": "0.1.14",
"@firebase/performance-types": "0.0.1",
"@firebase/util": "0.2.15",
"@firebase/util": "0.2.17",
"tslib": "1.9.3"
}
},
@ -815,23 +816,23 @@
}
},
"@firebase/storage": {
"version": "0.2.16",
"resolved": "https://registry.npmjs.org/@firebase/storage/-/storage-0.2.16.tgz",
"integrity": "sha512-R/qLIqp7ht7T0P2w3LWB5JvXUZMCWzrHOyublAa4iSVcuqN2SLMIpqnYXNKYk+o/NcW7jO8DE8c62mnpvsS+pw==",
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@firebase/storage/-/storage-0.3.0.tgz",
"integrity": "sha512-PnUvTIVHUskLuD9pO3pQ/NQPYpbq/uUOMuYCmPmW28/vRO+T0lCpwIu9zpuRtOLegmbePiFj2XXfrfA8p23Frw==",
"requires": {
"@firebase/storage-types": "0.2.11",
"@firebase/storage-types": "0.3.0",
"tslib": "1.9.3"
}
},
"@firebase/storage-types": {
"version": "0.2.11",
"resolved": "https://registry.npmjs.org/@firebase/storage-types/-/storage-types-0.2.11.tgz",
"integrity": "sha512-vGTFJmKbfScmCAVUamREIBTopr5Uusxd8xPAgNDxMZwICvdCjHO0UH0pZZj6iBQuwxLe/NEtFycPnu1kKT+TPw=="
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/@firebase/storage-types/-/storage-types-0.3.0.tgz",
"integrity": "sha512-zy24QU3xPXIOIAussB51fLID9F7j5NttKbs+3SqhKexU8kmNdwi1Lg91acSBuR1Oa/T8RRk5El9Jtd4dlTXjyQ=="
},
"@firebase/util": {
"version": "0.2.15",
"resolved": "https://registry.npmjs.org/@firebase/util/-/util-0.2.15.tgz",
"integrity": "sha512-XA6C4HkBOcUfDyvp/clq0MCFSuWSm3bPkolP689RCrGzjBs+bnCzC5a7by6Fq106zAQYeASwULJjTfVtZjKaaQ==",
"version": "0.2.17",
"resolved": "https://registry.npmjs.org/@firebase/util/-/util-0.2.17.tgz",
"integrity": "sha512-RvHkhQUkihI4JafJmB7S7Q8qVDFPD+kQdSmUyVTR2sEzxkk92MsIq4dBYnSjOMmnCe7L5lmB6hJdzkHa/TAP5A==",
"requires": {
"tslib": "1.9.3"
}
@ -3765,19 +3766,22 @@
}
},
"firebase": {
"version": "6.0.2",
"resolved": "https://registry.npmjs.org/firebase/-/firebase-6.0.2.tgz",
"integrity": "sha512-KA4VviZQJCzCIkCEvt3sJEsNe/HpqQdNE+ajy3wELHCxNV8PK8eBa10qxWDQhNgEtorHHltgYw3VwS0rbSvWrQ==",
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/firebase/-/firebase-6.1.0.tgz",
"integrity": "sha512-ewWFdhqI2vkOS7ij6U/tC5T4vwGeT0kiI3cs4LerrxQnMT+PMcl8RTeyvFwXf7nk6ExFr5dkrEeef6uKSqImPw==",
"requires": {
"@firebase/app": "0.4.1",
"@firebase/app": "0.4.3",
"@firebase/app-types": "0.4.0",
"@firebase/auth": "0.11.2",
"@firebase/database": "0.4.1",
"@firebase/firestore": "1.3.1",
"@firebase/functions": "0.4.7",
"@firebase/messaging": "0.3.20",
"@firebase/performance": "0.2.2",
"@firebase/database": "0.4.3",
"@firebase/firestore": "1.3.4",
"@firebase/functions": "0.4.8",
"@firebase/installations": "0.1.4",
"@firebase/messaging": "0.4.0",
"@firebase/performance": "0.2.5",
"@firebase/polyfill": "0.3.14",
"@firebase/storage": "0.2.16"
"@firebase/storage": "0.3.0",
"@firebase/util": "0.2.17"
}
},
"flatted": {
@ -8189,9 +8193,9 @@
},
"dependencies": {
"@types/node": {
"version": "10.14.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.14.7.tgz",
"integrity": "sha512-on4MmIDgHXiuJDELPk1NFaKVUxxCFr37tm8E9yN6rAiF5Pzp/9bBfBHkoexqRiY+hk/Z04EJU9kKEb59YqJ82A=="
"version": "10.14.8",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.14.8.tgz",
"integrity": "sha512-I4+DbJEhLEg4/vIy/2gkWDvXBOOtPKV9EnLhYjMoqxcRW+TTZtUftkHktz/a8suoD5mUL7m6ReLrkPvSsCQQmw=="
}
}
},

View File

@ -17,7 +17,7 @@
"@angular/common": "~7.2.0",
"@angular/compiler": "~7.2.0",
"@angular/core": "~7.2.0",
"@angular/fire": "^5.1.3",
"@angular/fire": "^5.2.1",
"@angular/flex-layout": "^7.0.0-beta.24",
"@angular/forms": "~7.2.0",
"@angular/material": "^7.3.7",
@ -25,8 +25,7 @@
"@angular/platform-browser-dynamic": "~7.2.0",
"@angular/router": "~7.2.0",
"core-js": "^2.5.4",
"firebase": "^6.0.2",
"g": "^2.0.1",
"firebase": "^6.1.0",
"hammerjs": "^2.0.8",
"moment": "^2.24.0",
"rxjs": "~6.3.3",

View File

@ -10,6 +10,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from '@app/app-routing.module';
import { AngularFireModule } from '@angular/fire';
import { AngularFireDatabaseModule } from '@angular/fire/database';
import { AngularFireAuthModule } from "@angular/fire/auth";
import { AppMaterialModule } from '@app/app-material.module';
import { LogService, SEOService } from '@app/core/services';
@ -31,6 +32,7 @@ import { CardDashboardComponent } from './card-dashboard/card-dashboard.componen
BrowserAnimationsModule,
AppRoutingModule,
AngularFireModule.initializeApp( environment.firebaseConfig ),
AngularFireAuthModule,
AngularFireDatabaseModule,
AppMaterialModule,
],

View File

@ -18,7 +18,7 @@ export class AuthGuard implements CanActivate {
}
// not logged in so redirect to login page with the return url
this.router.navigate( [ '/login' ], { queryParams: { returnUrl: state.url } } );
this.router.navigate( [ '/register' ], { queryParams: { returnUrl: state.url } } );
return false;
}

View File

@ -2,7 +2,9 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { Router } from '@angular/router';
import { User } from '@app/core/model';
import { auth } from 'firebase/app';
import { AngularFireAuth } from "@angular/fire/auth";
import { User } from 'firebase';
import { StorageService } from '@app/core/services/storage/storage.service';
import { LogService } from '@app/core/services/log/log.service';
@ -10,14 +12,38 @@ import { LogService } from '@app/core/services/log/log.service';
@Injectable( { providedIn: 'root' } )
export class AuthService {
user: User;
public currentUser: Observable<User>;
// public emailRegex = new RegExp( );
private currentUserSubject: BehaviorSubject<User>;
constructor(public afAuth: AngularFireAuth, public router: Router) {
this.afAuth.authState.subscribe(user => {
if (user) {
this.user = user;
localStorage.setItem('user', JSON.stringify(this.user));
} else {
localStorage.setItem('user', null);
}
})
}
constructor( private http: HttpClient, private router: Router, private stor: StorageService, private looger: LogService ) {
this.currentUserSubject = new BehaviorSubject<User>( stor.getLocal( 'user' ) as User );
this.currentUser = this.currentUserSubject.asObservable();
async login(email: string, password: string) {
try {
await this.afAuth.auth.signInWithEmailAndPassword(email, password)
this.router.navigate(['register/list']);
} catch (e) {
alert("Error!" + e.message);
}
}
async logout(){
await this.afAuth.auth.signOut();
localStorage.removeItem('user');
this.router.navigate(['/register']);
}
get isLoggedIn(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
return user !== null;
}
public get currentUserValue(): User {
@ -34,55 +60,6 @@ export class AuthService {
// }
// } );
// }
return this.currentUserSubject.value;
return //this.currentUserSubject.value;
}
public login( username: string, password: string ): any {
// const data: FormData = new FormData();
// data.append( 'email', username );
// data.append( 'password', sha512.sha512( password ) );
// return this.http.post<any>( environment.baseUrl + config.url.auth, data )
// .pipe( map( result => {
// let user = new User();
//
// this.looger.logInfo( result );
// // login successful if there's a jwt token in the response
// if ( result && result.body && result.statusCode === 200 ) {
//
// user.token = result.body;
// user.timeGetToken = new Date().getTime();
//
// // store user details and jwt token in local storage to keep user logged in between page refreshes
// this.stor.setLocal( 'user', user );
// this.currentUserSubject.next( user );
// } else {
// user = result.statusCode;
// }
//
// return user;
// } ) );
}
public logout(): void {
// remove user from local storage to log user out
// this.stor.setLocal( 'user' );
// this.currentUserSubject.next( null );
// this.router.navigate( [ '/login' ] );
}
public createAccount( email: string, password: string, repeat: string ): any {
// const data: FormData = new FormData();
// if ( password === repeat && this.emailRegex.test( email ) ) {
// data.append( 'email', email );
// data.append( 'password', sha512.sha512( password ) );
// return this.http.post( environment.baseUrl + config.url.register, data );
// }
return null;
}
}

View File

@ -1,7 +1,6 @@
<header>
<div id="header-container-desktop" fxLayout="row" fxLayoutAlign="space-between" fxShow
fxHide.lt-md="true">
<div id="header-container-desktop" fxLayout="row" fxLayoutAlign="space-between" fxShow fxHide.lt-md="true">
<div fxFlex="200px" class="title">
<a [routerLink]="">DiscoTrip</a>
@ -18,6 +17,9 @@
<button mat-fab class="account-button">
<mat-icon>person</mat-icon>
</button>
<li *ngIf="authService.isLoggedIn" class="nav-item">
<a class="nav-link" (click)="authService.logout()">Logout</a>
</li>
</div>
<div fxFlex="50px">
<!-- Search input -->
@ -27,8 +29,8 @@
</div>
<div fxHide fxLayout="row" fxLayoutAlign="space-between" fxShow.lt-md="true"
id="header-container-mobile">
<div id="header-container-mobile" fxLayout="row" fxLayoutAlign="space-between" fxShow
fxHide.gt-sm="true">
<button mat-flat-button style="position: absolute;top: 0;left: 0;height: 60px;">
<mat-icon style="transform: scale(1.3)">menu</mat-icon>

View File

@ -1,4 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { AuthService } from '@app/core/services/auth/auth.service';
@Component( {
selector: 'app-header',
@ -7,7 +8,7 @@ import { Component, OnInit } from '@angular/core';
} )
export class HeaderComponent implements OnInit {
constructor() {
constructor(private authService: AuthService) {
}
ngOnInit() {

View File

@ -0,0 +1,3 @@
.card {
box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
}

View File

@ -1,3 +1,23 @@
<p>
register works!
</p>
<div class="container pt-3">
<div class="row justify-content-sm-center">
<div class="col-sm-10 col-md-6">
<div class="card border-info">
<div class="card-header">Login</div>
<div class="card-body">
<div class="row">
<div class="col-md-4 text-center">
<img src="https://placeimg.com/128/128/nature">
</div>
<div class="col-md-8">
<input type="text" class="form-control mb-2" placeholder="Email" #userEmail required autofocus>
<input type="password" class="form-control mb-2" placeholder="Password" #userPassword required>
<button class="btn btn-lg btn-primary btn-block mb-1" (click)="authService.login(userEmail.value, userPassword.value)">Login</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

View File

@ -1,4 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { AuthService } from '@app/core/services/auth/auth.service';
@Component( {
selector: 'app-register',
@ -7,7 +8,7 @@ import { Component, OnInit } from '@angular/core';
} )
export class RegisterComponent implements OnInit {
constructor() {
constructor(private authService: AuthService) {
}
ngOnInit() {