Skip to content

Make all of the login functions return Promise<boolean> #339

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

Merged
merged 4 commits into from
Sep 19, 2018
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"docs:watch": "npm run docs:build -- -s -w",
"format": "prettier --single-quote --write projects/**/*.ts",
"copy:readme": "cpr README.md dist/lib/README.md --overwrite"
},
},
"private": true,
"dependencies": {
"@angular/animations": "6.0.0",
Expand Down Expand Up @@ -49,6 +49,7 @@
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.0.51",
"codelyzer": "~4.0.1",
"cpr": "^3.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
Expand Down
18 changes: 11 additions & 7 deletions projects/lib/src/oauth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ export class OAuthService extends AuthConfig {
this.restartRefreshTimerIfStillLoggedIn();
}


/**
* Convenience method that first calls `loadDiscoveryDocument(...)` and
* directly chains using the `then(...)` part of the promise to call
* the `tryLogin(...)` method.
*
* @param options LoginOptions to pass through to `tryLogin(...)`
*/
public loadDiscoveryDocumentAndTryLogin(options: LoginOptions = null) {
public loadDiscoveryDocumentAndTryLogin(options: LoginOptions = null): Promise<boolean> {
return this.loadDiscoveryDocument().then(doc => {
return this.tryLogin(options);
});
Expand All @@ -191,7 +192,7 @@ export class OAuthService extends AuthConfig {
*
* @param options LoginOptions to pass through to `tryLogin(...)`
*/
public loadDiscoveryDocumentAndLogin(options: LoginOptions = null) {
public loadDiscoveryDocumentAndLogin(options: LoginOptions = null): Promise<boolean> {
return this.loadDiscoveryDocumentAndTryLogin(options).then(_ => {
if (!this.hasValidIdToken() || !this.hasValidAccessToken()) {
this.initImplicitFlow();
Expand Down Expand Up @@ -1258,7 +1259,7 @@ export class OAuthService extends AuthConfig {
*
* @param options Optional options.
*/
public tryLogin(options: LoginOptions = null): Promise<void> {
public tryLogin(options: LoginOptions = null): Promise<boolean> {
options = options || {};

let parts: object;
Expand Down Expand Up @@ -1303,13 +1304,13 @@ export class OAuthService extends AuthConfig {
}

if (this.requestAccessToken && !accessToken) {
return Promise.resolve();
return Promise.resolve(false);
}
if (this.requestAccessToken && !options.disableOAuth2StateCheck && !state) {
return Promise.resolve();
return Promise.resolve(false);
}
if (this.oidc && !idToken) {
return Promise.resolve();
return Promise.resolve(false);
}

if (this.sessionChecksEnabled && !sessionState) {
Expand Down Expand Up @@ -1346,8 +1347,10 @@ export class OAuthService extends AuthConfig {
if (this.clearHashAfterLogin && !options.preventClearHashAfterLogin) {
location.hash = '';
}

this.callOnTokenReceivedIfExists(options);
return Promise.resolve();
return Promise.resolve(true);

}

return this.processIdToken(idToken, accessToken)
Expand All @@ -1373,6 +1376,7 @@ export class OAuthService extends AuthConfig {
this.eventsSubject.next(new OAuthSuccessEvent('token_received'));
this.callOnTokenReceivedIfExists(options);
this.inImplicitFlow = false;
return true;
})
.catch(reason => {
this.eventsSubject.next(
Expand Down