Skip to content

Commit

Permalink
fix: logout feature
Browse files Browse the repository at this point in the history
  • Loading branch information
makimenko committed Nov 29, 2023
1 parent ee85622 commit 0639e84
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/app/general/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {environment} from '../../../environments/environment';
import TokenClient = google.accounts.oauth2.TokenClient;
import {DynamicScriptLoaderService} from './dynamic-script-loader.service';
import {GoogleDriveService} from '../../data-access/service/google-drive.service';
import {Router} from '@angular/router';

export type Profile = {
name: string,
Expand All @@ -28,7 +29,8 @@ export class AuthService {
public profile?: Profile;

constructor(
protected dynamicScriptLoader: DynamicScriptLoaderService
protected dynamicScriptLoader: DynamicScriptLoaderService,
private router: Router
) {
this.handleTokenResponse = this.handleTokenResponse.bind(this);
this.handleProfileResponse = this.handleProfileResponse.bind(this);
Expand Down Expand Up @@ -85,11 +87,6 @@ export class AuthService {
}
if (res && res.access_token) {
await this.requestProfile(res.access_token);

// wrong location?
// gapi.client.setApiKey(environment.gapi.api_key);
// gapi.client.load('drive', 'v3');

this.accessToken = res.access_token;
localStorage.setItem(AUTH_KEY, res.access_token);
}
Expand Down Expand Up @@ -154,6 +151,15 @@ export class AuthService {
return this.inited;
}

public logout() {
console.log('AuthService.Logout');
localStorage.removeItem(AUTH_KEY);
this.userAuthenticated = false;
this.accessToken = undefined;
this.profile = undefined;
this.router.navigate(["/login"]);
}

}


Expand Down
8 changes: 6 additions & 2 deletions src/app/general/service/logged-in.guard.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import {Injectable} from '@angular/core';
import {CanActivate} from '@angular/router';
import {CanActivate, Router} from '@angular/router';
import {AuthService} from './auth.service';


@Injectable()
export class LoggedInGuard implements CanActivate {

constructor(
private authService: AuthService
private authService: AuthService,
private router: Router
) {
console.log("LoggedInGuard.constructor");
}

public async canActivate(): Promise<boolean> {
const userAuthenticated = await this.authService.checkIfUserAuthenticated();
console.log("LoggedInGuard.canActivate", userAuthenticated);
if (!userAuthenticated) {
this.router.navigate(["/login"]);
}
return userAuthenticated;
}

Expand Down
3 changes: 3 additions & 0 deletions src/app/manager/manager-panel/manager-panel.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<mat-form-field>
<mat-label>User Name</mat-label>
<input matInput [value]="name" readonly>
<button mat-icon-button matSuffix (click)="auth.logout()">
<mat-icon>logout</mat-icon>
</button>
</mat-form-field>
</form>

6 changes: 5 additions & 1 deletion src/app/manager/manager-panel/manager-panel.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
height: 100%;
}

.mat-form-field {
form {
width:100%
}

mat-form-field {
width: 100%;
}

Expand Down
1 change: 0 additions & 1 deletion src/app/manager/manager-panel/manager-panel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ export class ManagerPanelComponent implements OnInit {
});
}


}

0 comments on commit 0639e84

Please sign in to comment.