-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogleAuth.js
128 lines (121 loc) · 5.62 KB
/
googleAuth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Filename: d:\code\src\github.com\loitd\vue-quick-template\src\modules\vue-all-auth\googleAuth.js
* Path: d:\code\src\github.com\loitd\vue-quick-template\src\modules\vue-all-auth
* Created Date: Wednesday, March 13th 2019, 6:28:07 pm
* Author: loi_t
*
* Copyright (c) 2019 Your Company
*/
let googleAuth = {
// Define functions
ggInstallClient: function(){
// Installing client mean append client script to head (and make sure it loaded)
console.log("Begin installing gapi ...");
return new Promise(function(resolve, reject){
// do something here
let ggClientScript = document.createElement("script")
ggClientScript.setAttribute("src", "https://apis.google.com/js/api:client.js")
// Not handle connect timeout case yet
// Append the script onto the head element
// document.head.appendChild(ggClientScript)
// another way
document.getElementsByTagName('head')[0].appendChild(ggClientScript)
// just a log
console.log("gapi is installed!")
// using promise and you HAVE TO declare resolve() to announce its done! otherwise all others still wait
// but you have to wait for sometime for the js to be applied
setTimeout(function(){
resolve()
}, 500)
});
},
ggInitClient: function(configs){
console.log("Begin gapi initialization ...");
return new Promise(function(resolve, reject){
// do something. Do init as: https://developers.google.com/identity/sign-in/web/build-button
// By default, the fetch_basic_profile parameter of gapi.auth2.init() is set to true, which will automatically add 'email profile openid' as scope.
// Do not use the Google IDs returned by getId() or the user's profile information to communicate the currently signed in user to your backend server.
// Instead, send ID tokens, which can be securely validated on the server.
// console.log(configs) //--> to check if configs
// console.log(window.gapi)
if (configs !== undefined){
window.gapi.load("auth2", function(){
auth2 = window.gapi.auth2.init(configs);
})
// Just a log
console.log("gapi is initialized!")
// using promise and you HAVE TO declare resolve() to announce its done!
setTimeout(function(){
resolve()
}, 500)
} else {
console.log("Undefined configs for gapi! Initialized failed!")
reject()
}
});
},
// signIn function
/*
https://developers.google.com/identity/protocols/OAuth2
Server-side webapp # Javascript client side webapp: https://developers.google.com/identity/protocols/OAuth2UserAgent
This topo is for client side web app
Your App Google
------------request token------------>
------------user login & consent----->
<-----------token---------------------
------------Validate token----------->
<-----------Validation response-------
------------Use token to call API---->
*/
signIn: function(successCallback, errorCallback){
// this step happen when all init and installation above done
// which mean all thing ready to be called
// // Sample token/code response: 4/-ABYX3-wfXTznLcS6prVHU5mqYbCsTvAbGKKtxw00k559VMVwLIC1Pt_L7Mmpt24bec3bJXpLH2MY1Eoh98Eg6g
window.gapi.auth2.getAuthInstance().signIn().then(function(googleUser){
// when success. Do anything you want
// You can send the authorizationCode to your backend server for further processing,
// ex: redirect to the dashboard
// this.$router.push({ name: 'home' });
successCallback(googleUser)
}, function(error){
// things to do when sign-in fails
// Sample response: {error: "popup_closed_by_user"}
// Sample: Object error: "access_denied" __proto__:
errorCallback(error)
})
},
// signOut function
signOut: function(successCallback, errorCallback){
window.gapi.auth2.getAuthInstance().signOut().then(function(){
successCallback()
}, function(error){
errorCallback(error)
})
},
// printInfo
// https://developers.google.com/identity/sign-in/web/people
printInfo: function(){
if (window.gapi.auth2.getAuthInstance().isSignedIn.get()){
// All printing should be removed in production.
// console.log("This is auth2");
// console.log(window.gapi.auth2);
// Ok, you signed in
// console.log("This is ins");
let ins = window.gapi.auth2.getAuthInstance();
// console.log(ins);
//yes
// console.log("This is googleUser");
let googleUser = window.gapi.auth2.getAuthInstance().currentUser.get();
// console.log(googleUser);
//yes
// console.log("This is profile");
let profile = googleUser.getBasicProfile();
// console.log(profile);
//
console.log("Result: ID:"+profile.getId() + "FullName:"+profile.getName()+"Email:"+profile.getEmail()+"Img:"+profile.getImageUrl()+"FamilyName:"+profile.getFamilyName()+"GivenName:"+profile.getGivenName())
} else {
console.log("Yes, you haven't logged in yet!")
}
},
}
module.exports = googleAuth;