I am working on javascript to authenticate the user through google using OAuth. I am getting the user to sign in but the account chooser is opened twice, it selects the account once and again asks for the account then asks for offline access everytime.
I need to select the account once and select offline access, here's the code.
GoogleAuth.signIn({
scope: 'profile email'
}).then(function (result: any) {
//if successful, then only call below. else, return error.
var user = GoogleAuth.currentUser.get();
var email = user.getBasicProfile().getEmail();
var userName = user.getBasicProfile().getName();
GoogleAuth.grantOfflineAccess({prompt: "consent"}).then(function (authResult: any) {
if (authResult['code']) {
// Send the code to the server
$.ajax({
type: 'POST',
url: self._config.apiEndPoint + 'v1/authorization/ProcessAuthorizationCode?authorizationCode=' + authResult['code'] + "&userId=" + email,
// Always include an `X-Requested-With` header in every AJAX request,
// to protect against CSRF attacks.
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
contentType: 'application/octet-stream; charset=utf-8',
success: function (result) {
//call register external now
$.ajax({
type: 'POST',
url: self._config.apiEndPoint + 'v1/account/registerexternal?client_id=' + self._config.appClientId,
// Always include an `X-Requested-With` header in every AJAX request,
// to protect against CSRF attacks.
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
contentType: 'application/json',
success: function (result) {
self._profileData.UserEmail = email;
self._profileData.UserName = userName;
self._router.navigateByUrl("/profile");
},
processData: false,
data: JSON.stringify({
ExternalUserId: email,
UserName: userName,
Provider: 'Google',
ExternalAccessToken: result.access_token,
RefreshToken: 'Not really important',
Email: email,
ProviderKey: 'Not really important'
}),
error: function (xhr) {
self._router.navigateByUrl("/error;type=" + JSON.parse(xhr.responseText).Message, {preserveQueryParams:true});
}
});
},
processData: false,
data: authResult['code']
});
} else {
// There was an error.
}
});
});