Skip to content

Commit

Permalink
Merge pull request #44 from teamq-ec/DM2-2770-pagina-de-pagos
Browse files Browse the repository at this point in the history
DM2-2791-mejoras-de-interfaz-y-funcionalidad
  • Loading branch information
fe222004 authored Aug 1, 2024
2 parents ff524d1 + c3aa8f7 commit 19aecba
Show file tree
Hide file tree
Showing 27 changed files with 843 additions and 297 deletions.
4 changes: 2 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const routes: Routes = [
path: 'pages/:userId',
loadChildren: () =>
import('./pages/pages.module').then((m) => m.PagesModule),
canActivate : [loginGuard]
canActivate: [loginGuard],
},
{
path: '**',
redirectTo: '/home',
redirectTo: '',
},
];

Expand Down
35 changes: 32 additions & 3 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { AuthService } from './components/service/auth.service';
import { NavigationEnd, Router } from '@angular/router';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
styleUrls: ['./app.component.css'],
})
export class AppComponent {
export class AppComponent implements OnInit {
title = 'Appliance Care';
isLoggedIn = false;

constructor(private authService: AuthService, private router: Router) {}

ngOnInit(): void {
this.isLoggedIn = this.authService.isLoggedIn();

if (this.isLoggedIn) {
this.authService.redirectIfLoggedIn();
} else {
this.router.navigate(['']);
}

this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.isLoggedIn = this.authService.isLoggedIn();
if (this.isLoggedIn && event.urlAfterRedirects === '/') {
this.authService.redirectIfLoggedIn();
}
}
});
}

logout(): void {
this.authService.logout();
this.isLoggedIn = false;
}
}
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ApplianceRegistrationModule } from './pages/appliance-registration/appl
import { AlertConfirmationModule } from './pages/components/alert-confirmation/alert-confirmation.module';
import { ProductModule } from './pages/product/product.module';
import { ModalProductModule } from './pages/components/modal-product/modal-product.module';
import { PaymentModule } from './pages/payment/payment.module';

export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
Expand All @@ -33,6 +34,7 @@ export function HttpLoaderFactory(http: HttpClient) {
HomeModule,
DashboardModule,
ProductModule,
PaymentModule,
ReactiveFormsModule,
HttpClientModule,
TranslateModule.forRoot({
Expand Down
17 changes: 11 additions & 6 deletions src/app/components/guards/login.guard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Router } from '@angular/router';
import { inject } from '@angular/core';

export const loginGuard = () => {
const router = inject(Router);

if ( localStorage.getItem('token')){
return true;
} else {
return false;
}
}
if (localStorage.getItem('token')) {
return true;
} else {
router.navigate(['auth/login']);
return false;
}
};
25 changes: 20 additions & 5 deletions src/app/components/service/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { map, Observable } from 'rxjs';
import { User } from 'src/app/models/user';
import { environment } from 'src/environments/environment';
Expand All @@ -12,10 +13,10 @@ export class AuthService {
private apiUrlAuthRegister = `${environment.apiUrl}/register`;
private apiUrlAuthLogin = `${environment.apiUrl}/login`;

constructor() {}
constructor(private router: Router) {}

register(playload: User): Observable<User> {
return this.httpClient.post<User>(this.apiUrlAuthRegister, playload);
register(payload: User): Observable<User> {
return this.httpClient.post<User>(this.apiUrlAuthRegister, payload);
}

login(email: string, password: string): Observable<any> {
Expand All @@ -25,6 +26,7 @@ export class AuthService {
map((response) => {
if (response && response.token) {
localStorage.setItem('token', response.token);
localStorage.setItem('userId', response.user.id.toString());
}
return response;
})
Expand All @@ -33,11 +35,24 @@ export class AuthService {

logout() {
localStorage.removeItem('token');
localStorage.removeItem('userName');
localStorage.removeItem('userLastName');
localStorage.removeItem('userId');
this.router.navigate(['auth/login']);
}

isLoggedIn(): boolean {
return !!localStorage.getItem('token');
}

getUserId(): string | null {
return localStorage.getItem('userId');
}

redirectIfLoggedIn() {
const userId = localStorage.getItem('userId');
if (userId) {
this.router.navigate([`/pages/${userId}/home`]);
} else {
this.router.navigate(['auth/login']);
}
}
}
3 changes: 1 addition & 2 deletions src/app/constants/routes.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export const RoutesConstants = {
dashboard: '/pages/:userId/home',
form: '/pages/:userId/form',
product: '/pages/:userId/product',
root: '/',
payment: '/pages/:userId/payment/:id',
};

11 changes: 11 additions & 0 deletions src/app/models/payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Payment {
user_id: number;
full_name: string;
email: string;
telephone_number: string;
card_type: string;
card_number: string;
security_code: string;
amount_payable: number;
product_id: number;
}
Loading

0 comments on commit 19aecba

Please sign in to comment.