Skip to content

Commit

Permalink
feat: remove required login
Browse files Browse the repository at this point in the history
  • Loading branch information
69pmb committed Oct 27, 2023
1 parent d58de20 commit a8915b6
Show file tree
Hide file tree
Showing 22 changed files with 273 additions and 185 deletions.
6 changes: 0 additions & 6 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,34 @@ const routes: Routes = [
import('./application-modules/dashboard/dashboard.module').then(
m => m.DashboardModule
),
canActivate: [AuthGard],
},
{
path: 'movie',
loadChildren: () =>
import('./application-modules/movie-detail/movie-detail.module').then(
m => m.MovieDetailModule
),
canActivate: [AuthGard],
},
{
path: 'serie',
loadChildren: () =>
import('./application-modules/serie-detail/serie-detail.module').then(
m => m.SerieDetailModule
),
canActivate: [AuthGard],
},
{
path: 'person',
loadChildren: () =>
import('./application-modules/person-detail/person-detail.module').then(
m => m.PersonDetailModule
),
canActivate: [AuthGard],
},
{
path: 'release',
loadChildren: () =>
import('./application-modules/release/release.module').then(
m => m.ReleaseModule
),
canActivate: [AuthGard],
},
{
path: 'datas',
Expand All @@ -58,7 +53,6 @@ const routes: Routes = [
import('./application-modules/discover/discover.module').then(
m => m.DiscoverModule
),
canActivate: [AuthGard],
},
{
path: 'tags',
Expand Down
10 changes: 5 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {filter} from 'rxjs/operators';
import {Router, NavigationStart, NavigationEnd} from '@angular/router';
import {Router, NavigationStart} from '@angular/router';
import {Component, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';

Expand All @@ -8,6 +8,7 @@ import {TabsService} from './service/tabs.service';
import {AuthService} from './service/auth.service';
import {MyDatasService} from './service/my-datas.service';
import {MyTagsService} from './service/my-tags.service';
import {MenuService} from './service/menu.service';

@Component({
selector: 'app-root',
Expand All @@ -21,7 +22,8 @@ export class AppComponent implements OnInit {
private auth: AuthService,
private myDatasService: MyDatasService<Data>,
private myTagsService: MyTagsService,
private translate: TranslateService
private translate: TranslateService,
private menuService: MenuService
) {}

ngOnInit(): void {
Expand All @@ -30,9 +32,7 @@ export class AppComponent implements OnInit {
.subscribe((event: NavigationStart) => {
this.tabsService.onNavigation(event);
});
this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(() => window.scrollTo(0, 0));
this.menuService.event$.subscribe(() => window.scrollTo(0, 0));
this.auth.getCurrentUser(false);
this.auth.user$.subscribe(user => {
if (user) {
Expand Down
30 changes: 16 additions & 14 deletions src/app/app.gards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
RouterStateSnapshot,
} from '@angular/router';
import {Injectable, OnDestroy} from '@angular/core';
import {Subscription} from 'rxjs';
import {Observable, Subscription, of} from 'rxjs';
import {map} from 'rxjs/operators';

import {AuthService} from './service/auth.service';

Expand All @@ -21,23 +22,24 @@ export class AuthGard implements CanActivate, OnDestroy {
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Promise<boolean> {
): Observable<boolean> {
console.log('canActivate', state.url);
try {
return this.auth.isAuthenticated().then(isAuth => {
console.log('isAuth', isAuth);
if (!isAuth) {
console.log('not isAuthenticated');
this.router.navigate(['/login']);
return false;
}
console.log('logged');
// this.router.navigate([state.url]);
return true;
});
return this.auth.isAuthenticated().pipe(
map(isAuth => {
console.log('isAuth', isAuth);
if (!isAuth) {
console.log('not isAuthenticated');
this.auth.redirectToLogin(state.url);
return false;
}
console.log('logged');
return true;
})
);
} catch (err) {
console.log(err);
return new Promise(() => {});
return of(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class DiscoverComponent implements OnInit, OnDestroy {
page: PageEvent;
nbChecked = 0;
max = 300;
adult: boolean;
adult = false;
runtimeRange: any[] = [0, this.max];
formatter: NouiFormatter;
minYear = 1890;
Expand Down
152 changes: 81 additions & 71 deletions src/app/application-modules/login/connect/connect.component.html
Original file line number Diff line number Diff line change
@@ -1,73 +1,83 @@
<h2>{{ 'login.connect.title' | translate }}:</h2>
<div class="connect-content">
<form #connectForm="ngForm">
<mat-error>{{ message }}</mat-error>
<mat-form-field>
<input
matInput
[(ngModel)]="name"
placeholder="{{ 'global.username' | translate }}"
type="text"
#inputName="ngModel"
name="inputName"
required
/>
<mat-error
*ngIf="
inputName.errors != null &&
inputName.errors.required &&
!inputName.pristine
"
>{{ 'global.username_required' | translate }}</mat-error
>
</mat-form-field>
<ng-container *ngIf="{redirect: redirect$ | async} as context">
<h2 *ngIf="!context.redirect">{{ 'login.connect.title' | translate }}:</h2>
<h2 *ngIf="context.redirect">{{ 'login.connect.redirect' | translate }}:</h2>
<div class="connect-content">
<form #connectForm="ngForm">
<mat-error>{{ message }}</mat-error>
<mat-form-field>
<input
matInput
[(ngModel)]="name"
placeholder="{{ 'global.username' | translate }}"
type="text"
#inputName="ngModel"
name="inputName"
required
/>
<mat-error
*ngIf="
inputName.errors != null &&
inputName.errors.required &&
!inputName.pristine
"
>{{ 'global.username_required' | translate }}</mat-error
>
</mat-form-field>

<mat-form-field>
<input
matInput
[(ngModel)]="password"
placeholder="{{ 'login.password' | translate }}"
type="password"
#inputPassword="ngModel"
name="inputPassword"
required
/>
<mat-error
*ngIf="
inputPassword.errors != null &&
inputPassword.errors.required &&
!inputPassword.pristine
"
>{{ 'login.password_required' | translate }}</mat-error
>
</mat-form-field>
<mat-form-field>
<input
matInput
[(ngModel)]="password"
placeholder="{{ 'login.password' | translate }}"
type="password"
#inputPassword="ngModel"
name="inputPassword"
required
/>
<mat-error
*ngIf="
inputPassword.errors != null &&
inputPassword.errors.required &&
!inputPassword.pristine
"
>{{ 'login.password_required' | translate }}</mat-error
>
</mat-form-field>

<div class="connect-footer">
<button
type="submit"
class="btn btn-outline-primary"
(click)="login()"
[disabled]="connectForm.invalid"
>
{{ 'login.connect.log_in' | translate }}
</button>
<button
type="button"
class="btn btn-outline-primary"
routerLink="/login/forgot"
[queryParams]="{name: name}"
>
{{ 'login.connect.forgot' | translate }}
</button>
<button
type="button"
class="btn btn-outline-primary"
routerLink="/login/register"
[queryParams]="{name: name}"
>
{{ 'login.connect.sign_up' | translate }}
</button>
<app-dropdown-language class="dropdown-center"></app-dropdown-language>
</div>
</form>
</div>
<div class="connect-footer">
<button
type="submit"
class="btn btn-outline-primary"
(click)="login()"
[disabled]="connectForm.invalid"
>
{{ 'login.connect.log_in' | translate }}
</button>
<button
class="btn btn-outline-primary"
(click)="cancel()"
*ngIf="context.redirect"
>
{{ 'global.cancel' | translate }}
</button>
<button
type="button"
class="btn btn-outline-primary"
routerLink="/login/forgot"
[queryParams]="{name: name}"
>
{{ 'login.connect.forgot' | translate }}
</button>
<button
type="button"
class="btn btn-outline-primary"
routerLink="/login/register"
[queryParams]="{name: name}"
>
{{ 'login.connect.sign_up' | translate }}
</button>
<app-dropdown-language class="dropdown-center"></app-dropdown-language>
</div>
</form>
</div>
</ng-container>
43 changes: 34 additions & 9 deletions src/app/application-modules/login/connect/connect.component.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import * as crypto from 'crypto-js';
import {TranslateService} from '@ngx-translate/core';
import {Subscription, combineLatest} from 'rxjs';

import {AuthService} from '../../../service/auth.service';
import {TitleService} from '../../../service/title.service';
import {map} from 'rxjs/operators';
import {Constants} from '../../../constant/constants';
import {TabsService} from '../../../service/tabs.service';

@Component({
selector: 'app-connect',
templateUrl: './connect.component.html',
styleUrls: ['./connect.component.scss'],
})
export class ConnectComponent implements OnInit {
export class ConnectComponent implements OnInit, OnDestroy {
name: string;
password: string;
message: string;
subs: Subscription[] = [];
redirect$ = this.active.queryParams.pipe(
map(q => q[Constants.LOGIN_REDIRECT_URL])
);

constructor(
private auth: AuthService,
private router: Router,
private translate: TranslateService,
private title: TitleService
private title: TitleService,
private active: ActivatedRoute,
private tabs: TabsService
) {}

ngOnInit(): void {
Expand All @@ -29,16 +39,31 @@ export class ConnectComponent implements OnInit {

login(): void {
if (this.name && this.password) {
this.auth
.login(this.name, crypto.SHA512(this.password).toString())
.then(isAuth => {
this.subs.push(
combineLatest([
this.redirect$,
this.auth.login(this.name, crypto.SHA512(this.password).toString()),
]).subscribe(([redirect, isAuth]) => {
if (isAuth) {
this.message = this.translate.instant('login.connect.connected');
this.router.navigateByUrl('/');
this.router.navigateByUrl(redirect ?? this.tabs.activeLink.url);
} else {
this.message = this.translate.instant('login.connect.wrong');
}
});
})
);
}
}

cancel(): void {
this.subs.push(
this.redirect$.subscribe(redirect =>
this.router.navigateByUrl(redirect ?? '/')
)
);
}

ngOnDestroy(): void {
this.subs.forEach(sub => sub.unsubscribe());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ <h3 class="movie-time" *ngIf="movie.time > 0">
<ng-container *ngIf="movie.recommendations">
<app-list-datas
[datas]="movie.recommendations"
[hideUnknown]="!movie.adult"
[isMovie]="true"
[label]="('global.reco' | translate).concat(':')"
></app-list-datas>
Expand All @@ -262,6 +263,7 @@ <h3 class="movie-time" *ngIf="movie.time > 0">
<ng-container *ngIf="movie.similars">
<app-list-datas
[datas]="movie.similars"
[hideUnknown]="!movie.adult"
[isMovie]="true"
[label]="('global.similar' | translate).concat(':')"
></app-list-datas>
Expand Down
Loading

0 comments on commit a8915b6

Please sign in to comment.