Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don 1065 logout in menu #1804

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"@angular/platform-server": "^18.2.8",
"@angular/router": "^18.2.8",
"@angular/ssr": "^18.2.8",
"@biggive/components": "^202412041511.0.0",
"@biggive/components-angular": "^202412041511.0.0",
"@biggive/components": "^202412181130.0.0",
"@biggive/components-angular": "^202412181130.0.0",
"@fortawesome/angular-fontawesome": "~0.15.0",
"@fortawesome/free-brands-svg-icons": "^6.6.0",
"@fortawesome/free-solid-svg-icons": "^6.6.0",
Expand Down
13 changes: 13 additions & 0 deletions src/app/app-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ const routes: Routes = [
redirectIfAlreadyLoggedIn,
],
},
/** For use when donor clicks logout in the menu on the wordpress site **/
{
component: LoginComponent, // Angular requires we set a component but it will never be used as `canActivate` always
// redirects.
path: 'logout',
pathMatch: 'full',
canActivate: [
async () => {
inject(IdentityService).logout();
await inject(Router).navigate(['/'], {replaceUrl: true});
},
],
},
{
path: 'login',
pathMatch: 'full',
Expand Down
6 changes: 6 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ export class AppComponent implements AfterViewInit, OnDestroy, OnInit {
this.cookiePreferenceService.agreeToAll();
}

@HostListener('logoutClicked', ['$event'])
onLogoutClicked(_event: CustomEvent) {
this.identityService.logout();
void this.router.navigate(['']);
}

@HostListener('preferenceModalClosed', ['$event'])
async onCookieBannerPreferenceModalClosed(_event: CustomEvent) {
if (this.showingDedicatedCookiePreferencesPage) {
Expand Down
21 changes: 20 additions & 1 deletion src/app/identity.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {EventEmitter, Inject, Injectable, InjectionToken} from '@angular/core';
import jwtDecode from 'jwt-decode';
import {CookieService} from 'ngx-cookie-service';
Expand All @@ -20,6 +20,10 @@ export const TBG_DONATE_ID_STORAGE = new InjectionToken<StorageService>('TBG_DON
providedIn: 'root',
})
export class IdentityService {
/** Cookie to share with WP which needs to know whether we're logged in as a donor to be able to show the
* correct menu variant but does not need access to the actual session cookie that holds the JWT.
*/
private readonly isLoggedInCookieName = 'IS_LOGGED_IN';
private readonly cookieName = 'SESSION';
private readonly loginPath = '/auth';
private readonly peoplePath = '/people';
Expand Down Expand Up @@ -160,10 +164,12 @@ export class IdentityService {

logout() {
this.cookieService.delete(this.cookieName);
this.cookieService.delete(this.isLoggedInCookieName);
this.cookieService.delete(STRIPE_SESSION_SECRET_COOKIE_NAME);

// delete didn't seem to work reliably, so also directly setting an empty cookie that expires in the past here:
this.cookieService.set(this.cookieName, '', new Date('1970-01-01'), '/')
this.cookieService.set(this.isLoggedInCookieName, '', new Date('1970-01-01'), '/');
this.cookieService.set(STRIPE_SESSION_SECRET_COOKIE_NAME, '', new Date('1970-01-01'), '/')
this.storage.remove(this.storageKey);
this.loginStatusChanged.emit(false);
Expand Down Expand Up @@ -208,6 +214,19 @@ export class IdentityService {
saveJWT(id: string, jwt: string) {
const daysTilExpiry = 1;
this.cookieService.set(this.cookieName, JSON.stringify({id, jwt}), daysTilExpiry, '/');
if (this.probablyHaveLoggedInPerson()) {
this.cookieService.set(this.isLoggedInCookieName, 'true', daysTilExpiry, '/', this.domainSharedWithWordpress());
}
}

private domainSharedWithWordpress() {
const hostname = new URL(environment.donateUriPrefix).hostname;
const parts = hostname.split('.');
parts.shift();

// length will be zero when domain is localhost. In other cases we have a shared domain above that
// with our WordPress site.
return parts.length === 0 ? hostname : parts.join('.');
}

getFundingInstructions(id: string, jwt: string): Observable<FundingInstruction> {
Expand Down
12 changes: 0 additions & 12 deletions src/app/my-account/my-account.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,5 @@ <h3>
}
</biggive-page-section>
}

<div class="logout-row">
<biggive-button
space-above="5"
colour-scheme="white"
label="Log out"
full-width="true"
size="medium"
rounded="false"
(click)="logout()"
/>
</div>
</div>
</main>
16 changes: 0 additions & 16 deletions src/app/my-account/my-account.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ main > div {
background-color: $colour-primary;
}

#logout {
width: 80%;
text-align: center;
margin: auto;
@media #{$breakpoint-lg} {
max-width: 10em;
}
}

.account-actions {
display: flex;
flex-wrap: wrap;
Expand All @@ -44,13 +35,6 @@ main > div {
flex: 0 0 auto;
}

.logout-row {
display: flex;
align-items: flex-end;
justify-content: right;
gap: 17px;
}

table#personal-details, table#paymentMethods {
width: 100%;
td {
Expand Down
5 changes: 0 additions & 5 deletions src/app/my-account/my-account.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ export class MyAccountComponent implements OnDestroy, OnInit {
);
}

logout() {
this.identityService.logout();
void this.router.navigate(['']);
}

deleteMethod(method: PaymentMethod) {
this.paymentMethods = undefined;

Expand Down
7 changes: 0 additions & 7 deletions src/app/transfer-funds/transfer-funds.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<main>
@if (donor) {
<div id="loggedInHeader">
<p>Logged in as {{ donor.first_name }} {{donor.last_name}}</p>
<button id="my-account-link" mat-raised-button routerLink="/my-account">My Account</button>
<button mat-raised-button (click)="logout()">Log out</button>
</div>
}
<div class="b-container">
<h3 class="b-rh-1 b-bold">Transfer Donation Funds</h3>

Expand Down
13 changes: 0 additions & 13 deletions src/app/transfer-funds/transfer-funds.component.scss
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
@use '@angular/material' as mat;
@import '../../abstract';

#loggedInHeader {
background-color: mat.m2-get-color-from-palette($donate-primary);
color: white;
margin: 0px;
padding: 10px 2rem;
display: flex;
justify-content: flex-end;

p {
margin: 0 1rem 0 0;
}
}

.auth-options {
display: flex;
justify-content: space-between;
Expand Down
14 changes: 0 additions & 14 deletions src/app/transfer-funds/transfer-funds.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,20 +335,6 @@ export class TransferFundsComponent implements AfterContentInit, OnInit {
return Math.floor(creditAmount * (tipPercentage / 100));
}

logout() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

this.donor = undefined;
this.isLoading = false;

this.accountHolderName = undefined;
this.accountNumber = undefined;
this.sortCode = undefined;

this.isPurchaseComplete = false;
this.creditForm.reset();
this.identityService.logout();
window.location.href = "/";
}

cancelPendingTips() {
this.donationService.cancelDonationFundsToCampaign(environment.creditTipsCampaign).subscribe(() => {
// Theoretically this could be multiple tips, but in practice almost always 0 or 1, so singular is the less confusing copy.
Expand Down
Loading