Skip to content

Commit f8d8b33

Browse files
committed
Merge pull request #56 from OzzieOrca/autoLoadStoredToken
Auto load stored token
2 parents 164d562 + 86bccf2 commit f8d8b33

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

lib/src/browser/google_oauth2.dart

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GoogleOAuth2 extends OAuth2<Token> {
3232
String provider: "https://accounts.google.com/o/oauth2/",
3333
tokenLoaded(Token token),
3434
bool autoLogin: false,
35+
bool autoLoadStoredToken: true,
3536
String approval_prompt: null}) :
3637
_provider = provider,
3738
_tokenLoaded = tokenLoaded,
@@ -44,6 +45,10 @@ class GoogleOAuth2 extends OAuth2<Token> {
4445
login(immediate:true)
4546
.then((t) => print("Automatic login successful"))
4647
.catchError((e) => print("$e"));
48+
}else if (autoLoadStoredToken) {
49+
login(immediate:true, onlyLoadToken:true)
50+
.then((t) => print("Automatic login from stored token successful"))
51+
.catchError((e) => print("$e"));
4752
}
4853
}
4954

@@ -104,7 +109,7 @@ class GoogleOAuth2 extends OAuth2<Token> {
104109
/// If you have no token, a popup prompt will be displayed.
105110
/// If the user declines, closes the popup, or the service returns a token
106111
/// that cannot be validated, an exception will be delivered.
107-
Future<Token> login({bool immediate: null}) {
112+
Future<Token> login({bool immediate: null, bool onlyLoadToken: false}) {
108113
if (token != null) {
109114
if (token.expired) {
110115
if (immediate == null) {
@@ -149,23 +154,27 @@ class GoogleOAuth2 extends OAuth2<Token> {
149154

150155
completeByPromptingUser() {
151156
_tokenCompleter = _wrapValidation(tokenCompleter);
157+
if (onlyLoadToken){
158+
//Remove current login attempt because prompting user is disabled by onlyLoadToken
159+
_tokenCompleter.completeError("Google OAuth2 token not saved in Local Storage");
160+
} else {
161+
// Synchronous if the channel is already open -> avoids popup blocker
152162

153-
// Synchronous if the channel is already open -> avoids popup blocker
154-
155-
_channel
156-
.then((value) {
157-
String uri = _getAuthorizeUri(immediate);
158-
if (immediate) {
159-
IFrameElement iframe = _iframe(uri);
160-
_tokenCompleter.future.whenComplete(() => iframe.remove());
161-
} else {
162-
WindowBase popup = _popup(uri);
163-
new _WindowPoller(_tokenCompleter, popup).poll();
164-
}
165-
})
166-
.catchError((e) {
167-
return _tokenCompleter.completeError(e);
168-
});
163+
_channel
164+
.then((value) {
165+
String uri = _getAuthorizeUri(immediate);
166+
if (immediate) {
167+
IFrameElement iframe = _iframe(uri);
168+
_tokenCompleter.future.whenComplete(() => iframe.remove());
169+
} else {
170+
WindowBase popup = _popup(uri);
171+
new _WindowPoller(_tokenCompleter, popup).poll();
172+
}
173+
})
174+
.catchError((e) {
175+
return _tokenCompleter.completeError(e);
176+
});
177+
}
169178
}
170179

171180
final stored = _storedToken;

0 commit comments

Comments
 (0)