I am currently developing a Vue.js web application that allows users to connect with their Google accounts.
The login process, both front-end and back-end, is functioning properly: the Google sign-in button is displayed, the user clicks on it, and their avatar appears without the page needing to be refreshed.
However, I encountered a visual issue when a user who is already logged in refreshes the page. The problem lies in the fact that the request to the back-end is made towards the end because I cannot initialize gapi.auth2 before the Vue.js component/page has finished loading.
I attempted a solution mentioned in this post, but I faced difficulty calling gapi.auth2 within the main app data section:
<script>
function onLoad() {
gapi.load('auth2', function() {
gapi.auth2.init();
});
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/platform.js?onload=onLoad"></script>
...
<script type="module" src="main.js"></script>
</body>
</html>
Main.js:
var app = new Vue({
el: '#app',
data: {
user: gapi.auth2.getAuthInstance().currentUser.get()
}
This results in the error message:
TypeError: gapi.auth2 is undefined
Is there a way to retrieve this information directly before the entire page finishes loading?