finished login stg 1

This commit is contained in:
2018-05-10 09:27:21 -04:00
parent ca630dda67
commit 4bc9c83e38
14 changed files with 2900 additions and 84 deletions

View File

@@ -8,12 +8,10 @@ export class AuthGuard implements CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (localStorage.getItem('currentUser')) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['login'], { queryParams: { returnUrl: state.url }});
this.router.navigate( ['login'] );
return false;
}
}

View File

@@ -1,2 +1 @@
export * from './jwt.interceptor';
export * from './fake-backend';
export * from './fake-backend';

View File

@@ -1,20 +0,0 @@
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser && currentUser.token) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.token}`
}
});
}
return next.handle(request);
}
}

View File

@@ -11,7 +11,12 @@ const httpOptions = {
@Injectable()
export class AuthenticationService {
loginUrl = "http://192.241.155.78/interfaceServices/fake_loginInterface.php";
/* testing */
loginUrl = "http://pugludos.com/interfaceServices/loginInterface.php";
/* production */
//loginUrl = "/interfaceServices/fake_loginInterface.php";
params;
constructor(private http: HttpClient) { }
@@ -35,7 +40,6 @@ export class AuthenticationService {
}
logout() {
// remove user from local storage to log user out
localStorage.removeItem('currentUser');
}
}

View File

@@ -5,12 +5,13 @@ import { GameGridComponent } from './game-grid/game-grid.component'
import { ViewCardComponent } from './view-card/view-card.component'
import { LoginComponent } from './login/login.component'
import { RegisterComponent } from './register/register.component'
import { AuthGuard } from './_guards/index';
const routes: Routes = [
{ path: '', redirectTo: '/login', pathMatch: 'full' },
{ path: 'game-grid', component: GameGridComponent },
{ path: 'view-card', component: ViewCardComponent },
{ path: 'view-card/:gid', component: ViewCardComponent },
{ path: 'game-grid', component: GameGridComponent, canActivate: [AuthGuard] },
{ path: 'view-card', component: ViewCardComponent, canActivate: [AuthGuard] },
{ path: 'view-card/:gid', component: ViewCardComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent }

View File

@@ -34,7 +34,6 @@ import { fakeBackendProvider } from './_helpers/index';
import { AlertComponent } from './_directives/index';
import { AuthGuard } from './_guards/index';
import { JwtInterceptor } from './_helpers/index';
import { AlertService, AuthenticationService, UserService } from './_services/index';
@@ -73,12 +72,6 @@ import { AlertService, AuthenticationService, UserService } from './_services/in
AlertService,
AuthenticationService,
UserService,
{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
},
// provider used to create fake backend
fakeBackendProvider
],

View File

@@ -8,7 +8,7 @@
<button class="mat-button userButton" mat-button >
<img src="https://i.pinimg.com/280x280_RS/88/42/df/8842df04cd938aa654c865742253c276.jpg" >&nbsp;Christopher
<img src="https://i.pinimg.com/280x280_RS/88/42/df/8842df04cd938aa654c865742253c276.jpg" >&nbsp;<span *ngIf="isEmptyObject(currentUser)">{{currentUser.username}}</span>
</button>
@@ -17,7 +17,7 @@
<mat-menu #settingsMenu="matMenu">
<button mat-menu-item>Settings</button>
<button mat-menu-item>Help</button>
<button mat-menu-item>Log out</button>
<button mat-menu-item (click)="logOut()">Log out</button>
</mat-menu>
<button class="mat-icon-button" mat-icon-button [matMenuTriggerFor]="settingsMenu">

View File

@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';
import { ActivatedRoute, Router } from '@angular/router';
import { GamesService } from '../games.service';
import {PageEvent} from '@angular/material';
@@ -26,16 +26,19 @@ export class GameGridComponent implements OnInit {
length = 100;
pageSize = 10;
pageSizeOptions = [5, 10, 25, 50, 100];
currentUser;
constructor(
private route: ActivatedRoute,
private gamesService: GamesService
private gamesService: GamesService,
private router: Router
){
}
ngOnInit() {
this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.getGamesList();
}
@@ -81,6 +84,11 @@ export class GameGridComponent implements OnInit {
}
}
logOut(){
localStorage.removeItem('currentUser');
this.router.navigate( ['login'] );
}
}

View File

@@ -20,7 +20,7 @@ const httpOptionsPut = {
@Injectable()
export class GamesService {
APIURL = "http://192.241.155.78/api.php";
APIURL = "http://pugludos.com/interfaceServices/api.php";
constructor(
private http: HttpClient
@@ -36,7 +36,8 @@ export class GamesService {
}
getGames( queryFilters, querryPage, queryOrder, queryRecordMax ): Observable<any> {
return this.http.get( this.APIURL + "/games?filter="+ queryFilters +"&page="+ querryPage + "," + queryRecordMax +"&order="+ queryOrder +"&transform=1" )
let currentUser = JSON.parse(localStorage.getItem('currentUser'));
return this.http.get( this.APIURL + "/games?filter="+ queryFilters +"&page="+ querryPage + "," + queryRecordMax +"&order="+ queryOrder +"&transform=1&token=" + currentUser.token )
.map(res => {
return(
res

View File

@@ -62,20 +62,6 @@ export class LoginComponent implements OnInit {
return formValidators;
}
onSubmit( form ){
this.registrationService.loginUser( form ).subscribe( data => {
//this.router.navigate([this.returnUrl]);
console.log( "valid" );
},
error => {
console.log( "you suck, no long for you!" );
//this.alertService.error(error);
//this.loading = false;
});
}
login( form ) {
this.loading = true;
this.authenticationService.login( form )