I am working on a react-redux application where the user can submit their feedback/complaint to respective organization. The idea is to send emails on user's consent for which I am using Gmail API. I have integrated a feedback form.
Now the user has to be logged in before submission of the message. I do not want to add Gmail send (https://www.googleapis.com/auth/gmail.send) scope with login. Login(on the header) only works with the profile and read-only scopes. Send email permission should only be asked separately when submit is clicked and then take further action on that consent. How can I achieve this send email scope separately? I am writing down my login code as I am not sure what has to be done on submit.
var clientId = 'xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx';
var scopes = 'https://www.googleapis.com/auth/gmail.readonly '+'https://www.googleapis.com/auth/userinfo.profile';
var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"];
componentDidMount() {
this.handleClientLoad();
}
handleClientLoad = () => {
const script = document.createElement("script");
script.src = "https://apis.google.com/js/client.js";
script.async = true
script.onload = () => {
window.gapi.load('client:auth2', () => {
window.gapi.client.setApiKey(API_KEY);
window.setTimeout(this.initClient(), 1);
});
}
document.body.appendChild(script)
}
initClient = () => {
window.gapi.client.init({
apiKey: API_KEY,
discoveryDocs: DISCOVERY_DOCS,
clientId: clientId,
scope: scopes,
}).then(() => {
window.gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus());
this.updateSigninStatus(window.gapi.auth2.getAuthInstance().isSignedIn.get());
});
}
updateSigninStatus = (isSignedIn) => {
if (isSignedIn) {
window.gapi.auth2.getAuthInstance().currentUser.listen((user) => {
console.log(user);
});
} else {
console.log("error");
}
}
authClick = () => {
window.gapi.auth2.getAuthInstance().signIn().then((user) =>
console.log(user)
)}
Following is the order:
- User logs in
- Fill out the feedback form
- Hit submit (ask user's consent using Gmail send scope)
- Perform actions on the basis of consent.
I am stuck on the 3rd step. How to add scope separately to get user consent?