-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Popovkov57/development
Development
- Loading branch information
Showing
11 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="bg-info p-2 text-white"> | ||
<h3>Placeholder for Admin Features</h3> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Component } from "@angular/core"; | ||
|
||
@Component({ | ||
templateUrl: "admin.component.html" | ||
}) | ||
|
||
export class AdminComponent {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { NgModule } from "@angular/core"; | ||
import { CommonModule } from "@angular/common"; | ||
import { FormsModule } from "@angular/forms"; | ||
import { RouterModule } from "@angular/router"; | ||
import { AuthComponent } from "./auth.component"; | ||
import { AdminComponent } from "./admin.component"; | ||
import { AuthGard } from "./auth.gard"; | ||
|
||
let routing = RouterModule.forChild([ | ||
{ path: "auth", component: AuthComponent }, | ||
{ path: "main", component: AdminComponent, canActivate: [AuthGard] }, | ||
{ path: "**", redirectTo: "auth" }, | ||
]) | ||
|
||
@NgModule({ | ||
imports: [CommonModule, FormsModule, routing], | ||
providers: [AuthGard], | ||
declarations: [AuthComponent, AdminComponent] | ||
}) | ||
export class AdminModule{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="bg-info p-2 text-center"> | ||
<h3>SportsStore Admin</h3> | ||
</div> | ||
<div class="bg-danger mt-2 p-2 text-center text-white" *ngIf="errorMessage != null"> | ||
{{ errorMessage }} | ||
</div> | ||
<div class="p-2"> | ||
<form novalidate #form="ngForm" (ngSubmit)="authenticate(form)"> | ||
<div class="form-group"> | ||
<label>Name</label> | ||
<input class="form-control" name="username" [(ngModel)]="username" required /> | ||
</div> | ||
<div class="form-group"> | ||
<label>Password</label> | ||
<input class="form-control" type="password" name="password" [(ngModel)]="password" required /> | ||
</div> | ||
<div class="text-center"> | ||
<button class="btn btn-secondary m-1" routerLink="/">Go back></button> | ||
<button class="btn btn-primary m-1" type="submit">Log in</button> | ||
</div> | ||
</form> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Component } from "@angular/core"; | ||
import { NgForm } from "@angular/forms"; | ||
import { Router } from "@angular/router"; | ||
import { AuthService } from "../model/auth.service"; | ||
|
||
@Component({ | ||
templateUrl: "auth.component.html" | ||
}) | ||
|
||
export class AuthComponent { | ||
|
||
public username: string; | ||
public password: string; | ||
public errorMessage: string; | ||
|
||
constructor(private router: Router, private auth: AuthService){} | ||
|
||
authenticate(form: NgForm) { | ||
if (form.valid) { | ||
//perform authentication | ||
this.auth.authenticate(this.username, this.password).subscribe(response => { | ||
if (response) { | ||
this.router.navigateByUrl("/admin/main"); | ||
} | ||
this.errorMessage = "Authentication Failed" | ||
}) | ||
} else { | ||
this.errorMessage = "Form data invalid"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Inject, Injectable } from "@angular/core"; | ||
import { ActivatedRouteSnapshot, RouterStateSnapshot, Router } from "@angular/router"; | ||
import { AuthService } from "../model/auth.service"; | ||
|
||
@Injectable() | ||
export class AuthGard { | ||
|
||
constructor(private router: Router, private auth: AuthService) {} | ||
|
||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { | ||
if (!this.auth.authenticated) { | ||
this.router.navigateByUrl("/admin/auth"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { Observable } from "rxjs"; | ||
import { RestDataSource } from "./rest.datasource"; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
|
||
constructor(private datasource: RestDataSource) {} | ||
|
||
authenticate(username: string, password: string): Observable<boolean> { | ||
return this.datasource.authenticate(username, password); | ||
} | ||
|
||
get authenticated(): boolean { | ||
return this.datasource.auth_token != null; | ||
} | ||
|
||
clear() { | ||
this.datasource.auth_token = null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable } from "@angular/core"; | ||
import { HttpClient } from "@angular/common/http"; | ||
import { Observable } from "rxjs"; | ||
import { Product } from "./product.model"; | ||
import { Cart } from "./cart.model"; | ||
import { Order } from "./order.model"; | ||
import { map } from "rxjs"; | ||
|
||
const PROTOCOL = "http"; | ||
const PORT = 3500; | ||
|
||
@Injectable() | ||
export class RestDataSource { | ||
|
||
baseUrl: string; | ||
auth_token: string; | ||
|
||
constructor(private http: HttpClient) { | ||
this.baseUrl = `${PROTOCOL}://${location.hostname}:${PORT}/` | ||
} | ||
|
||
getProducts(): Observable<Product[]> { | ||
return this.http.get<Product[]>(this.baseUrl + "products"); | ||
} | ||
|
||
saveOrder(order: Order): Observable<Order> { | ||
console.log(JSON.stringify(order)); | ||
return this.http.post<Order>(this.baseUrl + "orders", order); | ||
} | ||
|
||
authenticate(user: string, pass: string): Observable<boolean> { | ||
return this.http.post<any>(this.baseUrl + "login", { | ||
name: user, password: pass | ||
}).pipe(map(response => { | ||
this.auth_token = response.success ? response.token : null; | ||
return response.success; | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters