-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(me): redirect to login if needed
- Loading branch information
Showing
9 changed files
with
173 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { AuthGuardService } from './auth-guard.service' | ||
import { MockBuilder, MockProvider } from 'ng-mocks' | ||
import { AuthService } from '@geonetwork-ui/api/repository' | ||
import { TestBed } from '@angular/core/testing' | ||
import { of } from 'rxjs' | ||
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' | ||
|
||
Object.defineProperty(window, 'location', { | ||
value: new URL('http://localhost'), | ||
}) | ||
|
||
describe('AuthGuardService', () => { | ||
let service: AuthGuardService | ||
beforeEach(() => { | ||
return MockBuilder(AuthGuardService) | ||
}) | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
MockProvider(AuthService, { | ||
loginUrl: 'http://login.com/bla?', | ||
}), | ||
MockProvider(PlatformServiceInterface, { | ||
isAnonymous: () => of(true), | ||
}), | ||
], | ||
}) | ||
window.location.href = 'http://original.path' | ||
service = TestBed.inject(AuthGuardService) | ||
}) | ||
|
||
it('returns true if the user is logged in', async () => { | ||
jest | ||
.spyOn(TestBed.inject(PlatformServiceInterface), 'isAnonymous') | ||
.mockReturnValue(of(false)) | ||
expect(await service.canActivate()).toBe(true) | ||
expect(window.location.href).toBe('http://original.path/') | ||
}) | ||
it('redirects the user to the login page if the user is not logged in', async () => { | ||
expect(await service.canActivate()).toBe(false) | ||
expect(window.location.href).toBe('http://login.com/bla?') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from '@angular/core' | ||
import { AuthService } from '@geonetwork-ui/api/repository' | ||
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' | ||
import { firstValueFrom } from 'rxjs' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class AuthGuardService { | ||
constructor( | ||
private platformService: PlatformServiceInterface, | ||
private authService: AuthService | ||
) {} | ||
|
||
// this will redirect the user to the authentication form if required | ||
async canActivate(): Promise<boolean> { | ||
const notLoggedIn = await firstValueFrom(this.platformService.isAnonymous()) | ||
if (notLoggedIn) { | ||
window.location.href = this.authService.loginUrl | ||
return false | ||
} | ||
return true | ||
} | ||
} |
Empty file.
1 change: 0 additions & 1 deletion
1
apps/metadata-editor/src/app/sign-in/sign-in-page.component.html
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
apps/metadata-editor/src/app/sign-in/sign-in-page.component.spec.ts
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
apps/metadata-editor/src/app/sign-in/sign-in-page.component.ts
This file was deleted.
Oops, something went wrong.