Skip to content

Commit

Permalink
Merge branch 'release/v1.3.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
guruahn committed May 9, 2019
2 parents 9ce3bfc + a953342 commit 30d5f94
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 68 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# vue-google-oauth2
Handling Google sign-in and sign-out for Vue.js applications.

![vue-google-oauth2](https://img.shields.io/npm/dt/vue-google-oauth2.svg)

## Installation
### Installation with npm
Expand Down
154 changes: 154 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import _Vue from "vue";

var googleAuth = (function () {

function installClient() {
var apiUrl = 'https://apis.google.com/js/api.js'
return new Promise((resolve) => {
var script = document.createElement('script')
script.src = apiUrl
script.onreadystatechange = script.onload = function () {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
setTimeout(function () {
resolve()
}, 500)
}
}
document.getElementsByTagName('head')[0].appendChild(script)
})
}

function initClient(config) {
return new Promise((resolve) => {
window.gapi.load('auth2', () => {
window.gapi.auth2.init(config)
.then(() => {
resolve(window.gapi)
})
})
})

}

function Auth() {
if (!(this instanceof Auth))
return new Auth()
this.GoogleAuth = null /* window.gapi.auth2.getAuthInstance() */
this.isAuthorized = false
this.isInit = false
this.prompt = null
this.isLoaded = function () {
/* eslint-disable */
console.warn('isLoaded() will be deprecated. You can use "this.$gAuth.isInit"')
return !!this.GoogleAuth
};

this.load = (config, prompt) => {
installClient()
.then(() => {
return initClient(config)
})
.then((gapi) => {
this.GoogleAuth = gapi.auth2.getAuthInstance()
this.isInit = true
this.prompt = prompt
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
})
};

this.signIn = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signIn()
.then(googleUser => {
if (typeof successCallback === 'function') successCallback(googleUser)
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
resolve(googleUser)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};

this.getAuthCode = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.grantOfflineAccess({ prompt: this.prompt })
.then(function (resp) {
if (typeof successCallback === 'function') successCallback(resp.code)
resolve(resp.code)
})
.catch(function (error) {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};

this.signOut = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signOut()
.then(() => {
if (typeof successCallback === 'function') successCallback()
this.isAuthorized = false
resolve(true)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};
}

return new Auth()
})();




function installGoogleAuthPlugin(Vue: typeof _Vue, options?: any): void {
//set config
let GoogleAuthConfig = null
let GoogleAuthDefaultConfig = { scope: 'profile email', discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'] }
let prompt = 'select_account'
if (typeof options === 'object') {
GoogleAuthConfig = Object.assign(GoogleAuthDefaultConfig, options)
if (options.scope) GoogleAuthConfig.scope = options.scope
if (options.prompt) prompt = options.prompt
if (!options.clientId) {
/* eslint-disable */
console.warn('clientId is required')
}
} else {
console.warn('invalid option type. Object type accepted only')
}

//Install Vue plugin
Vue.gAuth = googleAuth
Object.defineProperties(Vue.prototype, {
$gAuth: {
get: function () {
return Vue.gAuth
}
}
})
Vue.gAuth.load(GoogleAuthConfig, prompt)
}

export default installGoogleAuthPlugin
128 changes: 64 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

var googleAuth = (function () {

function installClient () {
function installClient() {
var apiUrl = 'https://apis.google.com/js/api.js'
return new Promise((resolve) => {
var script = document.createElement('script')
Expand All @@ -17,127 +17,127 @@ var googleAuth = (function () {
})
}

function initClient (config) {
function initClient(config) {
return new Promise((resolve) => {
window.gapi.load('auth2', () => {
window.gapi.auth2.init(config)
.then(() => {
resolve(window.gapi)
})
.then(() => {
resolve(window.gapi)
})
})
})

}
function Auth(){
if(!(this instanceof Auth))

function Auth() {
if (!(this instanceof Auth))
return new Auth()
this.GoogleAuth = null /* window.gapi.auth2.getAuthInstance() */
this.isAuthorized = false
this.isInit = false
this.prompt = null
this.isLoaded = function(){
this.isLoaded = function () {
/* eslint-disable */
console.warn('isLoaded() will be deprecated. You can use "this.$gAuth.isInit"')
return !!this.GoogleAuth
};

this.load = (config, prompt) => {
installClient()
.then(() => {
return initClient(config)
})
.then((gapi) => {
this.GoogleAuth = gapi.auth2.getAuthInstance()
this.isInit = true
this.prompt = prompt
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
})
.then(() => {
return initClient(config)
})
.then((gapi) => {
this.GoogleAuth = gapi.auth2.getAuthInstance()
this.isInit = true
this.prompt = prompt
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
})
};

this.signIn = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if(!this.GoogleAuth) {
if(typeof errorCallback === 'function') errorCallback(false)
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signIn()
.then(googleUser => {
if(typeof successCallback === 'function') successCallback(googleUser)
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
resolve(googleUser)
})
.catch(error => {
if(typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
.then(googleUser => {
if (typeof successCallback === 'function') successCallback(googleUser)
this.isAuthorized = this.GoogleAuth.isSignedIn.get()
resolve(googleUser)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};

this.getAuthCode = (successCallback, errorCallback) => {
this.getAuthCode = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if(!this.GoogleAuth) {
if(typeof errorCallback === 'function') errorCallback(false)
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.grantOfflineAccess({prompt: this.prompt})
.then(function(resp) {
if(typeof successCallback === 'function') successCallback(resp.code)
resolve(resp.code)
})
.catch(function(error) {
if(typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
this.GoogleAuth.grantOfflineAccess({ prompt: this.prompt })
.then(function (resp) {
if (typeof successCallback === 'function') successCallback(resp.code)
resolve(resp.code)
})
.catch(function (error) {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};

this.signOut = (successCallback, errorCallback) => {
return new Promise((resolve, reject) => {
if(!this.GoogleAuth) {
if(typeof errorCallback === 'function') errorCallback(false)
if (!this.GoogleAuth) {
if (typeof errorCallback === 'function') errorCallback(false)
reject(false)
return
}
this.GoogleAuth.signOut()
.then(() => {
if(typeof successCallback === 'function') successCallback()
this.isAuthorized = false
resolve(true)
})
.catch(error => {
if(typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
.then(() => {
if (typeof successCallback === 'function') successCallback()
this.isAuthorized = false
resolve(true)
})
.catch(error => {
if (typeof errorCallback === 'function') errorCallback(error)
reject(error)
})
})
};
}

return new Auth()
})();
function installGoogleAuthPlugin (Vue, options) {




function installGoogleAuthPlugin(Vue, options) {
//set config
let GoogleAuthConfig = null
let GoogleAuthDefaultConfig = { scope: 'profile email', discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest']}
let GoogleAuthDefaultConfig = { scope: 'profile email', discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest'] }
let prompt = 'select_account'
if (typeof options === 'object') {
GoogleAuthConfig = Object.assign(GoogleAuthDefaultConfig, options)
if(options.scope) GoogleAuthConfig.scope = options.scope
if(options.prompt) prompt = options.prompt
if(!options.clientId) {
if (options.scope) GoogleAuthConfig.scope = options.scope
if (options.prompt) prompt = options.prompt
if (!options.clientId) {
/* eslint-disable */
console.warn('clientId is required')
}
}else{
} else {
console.warn('invalid option type. Object type accepted only')
}

//Install Vue plugin
Vue.gAuth = googleAuth
Object.defineProperties(Vue.prototype, {
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "vue-google-oauth2",
"version": "1.3.3",
"version": "1.3.4",
"description": "Handling Google Auth2 sign-in and sign-out",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand All @@ -18,13 +19,13 @@
"signin",
"signout",
"login",
"logout",
"oauth2"
"logout",
"oauth2"
],
"author": "Jeongwoo Ahn <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/guruahn/vue-google-oauth2/issues"
},
"homepage": "https://github.com/guruahn/vue-google-oauth2#readme"
}
}

0 comments on commit 30d5f94

Please sign in to comment.