diff --git a/angular.json b/angular.json
index a15f0b33..e43e8a41 100644
--- a/angular.json
+++ b/angular.json
@@ -99,6 +99,10 @@
}
}
}
- }},
- "defaultProject": "kyoo"
-}
+ }
+ },
+ "defaultProject": "kyoo",
+ "cli": {
+ "analytics": false
+ }
+}
\ No newline at end of file
diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts
index 185c71ab..f1fc0ea5 100644
--- a/src/app/app-routing.module.ts
+++ b/src/app/app-routing.module.ts
@@ -15,6 +15,7 @@ import { ShowDetailsComponent } from './show-details/show-details.component';
import {LoginComponent} from "./login/login.component";
import {UnauthorizedComponent} from "./unauthorized/unauthorized.component";
import {LogoutComponent} from "./logout/logout.component";
+import {AutologinComponent} from "./autologin/autologin.component";
const routes: Routes = [
{ path: "browse", component: BrowseComponent, pathMatch: "full", resolve: { shows: LibraryResolverService } },
@@ -26,6 +27,7 @@ const routes: Routes = [
{ path: "search/:query", component: SearchComponent, resolve: { items: SearchResolverService } },
{ path: "login", component: LoginComponent },
{ path: "logout", component: LogoutComponent },
+ { path: "autologin", component: AutologinComponent },
{ path: "unauthorized", component: UnauthorizedComponent },
{ path: "**", component: NotFoundComponent }
];
diff --git a/src/app/app.component.html b/src/app/app.component.html
index 337d1184..c3ac453a 100644
--- a/src/app/app.component.html
+++ b/src/app/app.component.html
@@ -19,7 +19,7 @@
autologin works!
diff --git a/src/app/autologin/autologin.component.scss b/src/app/autologin/autologin.component.scss new file mode 100644 index 00000000..e69de29b diff --git a/src/app/autologin/autologin.component.ts b/src/app/autologin/autologin.component.ts new file mode 100644 index 00000000..952b7242 --- /dev/null +++ b/src/app/autologin/autologin.component.ts @@ -0,0 +1,25 @@ +import {Component, OnInit} from '@angular/core'; +import {AuthService} from "../services/auth.service"; + +@Component({ + selector: 'app-autologin', + templateUrl: './autologin.component.html', + styleUrls: ['./autologin.component.scss'] +}) +export class AutologinComponent implements OnInit +{ + constructor(private authManager: AuthService) + { + this.authManager.oidcSecurityService.onModuleSetup.subscribe(() => + { + this.authManager.login(); + }) + } + + ngOnInit(): void + { + if (this.authManager.oidcSecurityService.moduleSetup) { + this.authManager.login(); + } + } +} diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index ba130cc0..3a03552b 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -1,17 +1,18 @@ -import { Injectable } from '@angular/core'; -import {OidcSecurityService} from "angular-auth-oidc-client"; +import {Injectable} from '@angular/core'; +import {AuthorizationResult, AuthorizationState, OidcSecurityService, ValidationResult} from "angular-auth-oidc-client"; import {HttpClient} from "@angular/common/http"; import {Account} from "../../models/account"; +import {Router} from "@angular/router"; @Injectable({ providedIn: 'root' }) export class AuthService { - isAuthenticated: boolean; + isAuthenticated: boolean = false; user: any; - constructor(public oidcSecurityService: OidcSecurityService, private http: HttpClient) + constructor(public oidcSecurityService: OidcSecurityService, private http: HttpClient, private router: Router) { if (this.oidcSecurityService.moduleSetup) this.authorizeCallback(); @@ -21,12 +22,13 @@ export class AuthService this.authorizeCallback(); }); - this.oidcSecurityService.getIsAuthorized().subscribe(auth => + this.oidcSecurityService.onAuthorizationResult.subscribe((authorizationResult: AuthorizationResult) => { - this.isAuthenticated = auth; + this.getUser(); + this.isAuthenticated = authorizationResult.authorizationState == AuthorizationState.authorized; + this.router.navigate(["/"]); }); - this.getUser(); } getUser() @@ -34,6 +36,13 @@ export class AuthService this.oidcSecurityService.getUserData().subscribe(userData => { this.user = userData; + console.log("Got user data"); + console.log(this.user); + // this.zone.run(() => + // { + // this.user = userData; + // this.isAuthenticated = userData !== undefined; + // }); }); } @@ -44,6 +53,7 @@ export class AuthService logout() { + document.cookie = "Authenticated=false; expires=" + new Date(2147483647 * 1000).toUTCString(); this.http.get("api/account/logout").subscribe(() => { this.oidcSecurityService.logoff(); @@ -52,7 +62,23 @@ export class AuthService private authorizeCallback() { - this.oidcSecurityService.authorizedCallbackWithCode(window.location.toString()); + if (window.location.href.indexOf("?code=") != -1) + this.oidcSecurityService.authorizedCallbackWithCode(window.location.toString()); + else if (window.location.href.indexOf("/login") == -1) + { + this.oidcSecurityService.getIsAuthorized().subscribe((authorized: boolean) => + { + console.log("Is authorized: " + authorized); + this.isAuthenticated = authorized; + if (!authorized) + { + if (document.cookie.indexOf("Authenticated=true") != -1) + this.router.navigate(['/autologin']); + } + else + document.cookie = "Authenticated=true; expires=" + new Date(2147483647 * 1000).toUTCString(); + }); + } } getAccount(): Account