Skip to content

Commit

Permalink
fix: trying launchWebauthflow to logout
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafasalfiti committed May 1, 2024
1 parent ae77e0f commit 047365b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="content">
<router-outlet></router-outlet>
</div>
<mat-toolbar fxLayout="row" fxLayoutAlign="space-between center">
<mat-toolbar *ngIf="authService.isAuthenticated()" fxLayout="row" fxLayoutAlign="space-between center">
<div fxLayout="column" fxLayoutAlign=" center">
<a mat-icon-button routerLink="/scan" routerLinkActive="active-link"
><mat-icon>qr_code_scanner</mat-icon></a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { decodeJwt } from 'jose';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';

@Injectable({
providedIn: 'root',
})
export class AuthService {
constructor() {}
constructor(
private router: Router,
private http: HttpClient,
) {}

/**
* Checks if the access token exists and if it is expired
Expand All @@ -24,6 +29,7 @@ export class AuthService {
* Launches the web auth flow to authenticate the user
*/
async login() {
console.log(this.getAuthUrl());
if (typeof chrome.identity !== 'undefined') {
await chrome.identity
.launchWebAuthFlow({
Expand All @@ -36,11 +42,35 @@ export class AuthService {
const accessToken = this.extractTokenFromRedirectUri(redirectUri);
localStorage.setItem('accessToken', accessToken);
},
(err) => console.log(err)
(err) => console.log(err),
);
}
}

/**
* Logs out the user
*/
async logout() {
// this.http.post(`${environment.keycloakHost}/realms/${environment.keycloakRealm}/protocol/openid-connect/logout`, null).subscribe(() => {
// console.log('successfully logged out');
// this.router.navigateByUrl('/login');
// });

await chrome.identity
.launchWebAuthFlow({
interactive: false,
url: this.getLogoutUrl(),
})
.then(
() => {
window.sessionStorage.clear();
localStorage.clear();
this.router.navigateByUrl('/login');
},
(err) => console.log(err),
);
}

/**
* Generates the auth url that will be used for login.
* @returns
Expand Down Expand Up @@ -78,4 +108,15 @@ export class AuthService {
// Returning the extracted values
return accessToken;
}

/**
* Generates the logout URL that will be used for logging out.
* @returns {string} The logout URL.
*/
private getLogoutUrl() {
let logoutUrl = `${environment.keycloakHost}/realms/${environment.keycloakRealm}/protocol/openid-connect/logout`;
logoutUrl += `?redirect_uri=${encodeURIComponent(chrome.identity.getRedirectURL())}`;
console.log(logoutUrl);
return logoutUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// eslint-disable-next-line @typescript-eslint/no-namespace
export declare namespace globalThis {
let environment: {
backendUrl: string;
keycloakHost: string;
keycloakClient: string;
keycloakRealm: string;
demoIssuer: string;
demoVerifier: string;
};
}

export const environment = {
backendUrl: 'http://localhost:3000',
keycloakHost: 'http://localhost:8080',
Expand All @@ -6,3 +18,5 @@ export const environment = {
demoIssuer: 'http://localhost:3001',
demoVerifier: 'http://localhost:3002',
};

globalThis.environment = environment;

0 comments on commit 047365b

Please sign in to comment.