Skip to content

Commit

Permalink
Merge pull request #1114 from ORCID/fix-auth-guard-on-login-page
Browse files Browse the repository at this point in the history
fix auth guard on login page
  • Loading branch information
bobcaprice authored Feb 8, 2024
2 parents deec148 + 8d1621c commit 2de390e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions ui/src/app/account/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const AuthGuard = (route: ActivatedRouteSnapshot, state: RouterStateSnaps
return false
}
} else {
if (state.url === '/login') {
return true
}

router.navigate(['/login'])
stateStorageService.storeUrl(state.url)
return false
Expand Down
12 changes: 7 additions & 5 deletions ui/src/app/account/password/password.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ export class PasswordComponent implements OnInit {

ngOnInit() {
this.accountService.getAccountData().subscribe((account) => {
this.account = account
this.username = this.accountService.getUsername()
this.passwordForUsernameString = $localize`:@@password.title.string:Password for ${this.username} (You)`
if (account) {
this.account = account
this.username = this.accountService.getUsername()
this.passwordForUsernameString = $localize`:@@password.title.string:Password for ${this.username} (You)`
}
})
}

Expand All @@ -50,8 +52,8 @@ export class PasswordComponent implements OnInit {
error: () => {
this.success = undefined
this.error = 'ERROR'
}
})
},
})
}
}
}
7 changes: 5 additions & 2 deletions ui/src/app/account/service/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BehaviorSubject, EMPTY, Observable, Subject, catchError, map, of, takeU

import { IAccount } from '../model/account.model'
import { LanguageService } from 'src/app/shared/service/language.service'
import { Router } from '@angular/router'
// TODO: uncomment when memberservice is added or change the account service so that this logic is absent from the account service
//import { MSMemberService } from 'app/entities/member/member.service';

Expand All @@ -21,6 +22,7 @@ export class AccountService {
//private languageService: JhiLanguageService,
private languageService: LanguageService,
private sessionStorage: SessionStorageService,
private router: Router,
private http: HttpClient // TODO: uncomment when memberservice is added or change the account service so that this logic is absent from the account service //private memberService: MSMemberService
) {}

Expand Down Expand Up @@ -104,6 +106,7 @@ export class AccountService {
clearAccountData() {
this.accountData.next(null)
this.authenticated = false
this.router.navigate(['/login'])
}

hasAnyAuthority(authorities: string[]): boolean {
Expand Down Expand Up @@ -159,10 +162,10 @@ export class AccountService {
let username: string | null = null

if (this.isIdentityResolved()) {
if (this.accountData.value!.firstName) {
if (this.accountData.value?.firstName) {
username = this.accountData.value!.firstName
}
if (this.accountData.value!.lastName) {
if (this.accountData.value?.lastName) {
if (username) {
username = username + ' ' + this.accountData.value!.lastName
} else {
Expand Down
6 changes: 2 additions & 4 deletions ui/src/app/layout/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,11 @@ export class NavbarComponent implements OnInit {
this.username = undefined
if (this.isLoggedAs()) {
this.accountService.logoutAs().subscribe(() => {
window.location.href = '/'
this.router.navigate(['/'])
})
} else {
this.memberService.setManagedMember(null)
this.loginService.logout()
console.log('logged out now')
this.router.navigate(['/login'])
}
}

Expand All @@ -126,7 +124,7 @@ export class NavbarComponent implements OnInit {
this.memberCallDone = false
this.username = undefined
this.accountService.logoutAs().subscribe((res) => {
window.location.href = '/'
this.router.navigate(['/'])
})
}

Expand Down

0 comments on commit 2de390e

Please sign in to comment.