This library is reinventing the wheel for my own use.
You can find a better module.
npm i oauth2-implicitgrant
import OAuth2ImplicitGrant from "oauth2-implicitgrant"
const auth = OAuth2ImplicitGrant.forGoogleApi({
clientId: 'your client id that created above.',
redirectUrl: 'your redirect url',
scope: ['scopes that you want']
})
auth.init({
onDenied: () => {
// Something happen in authorization flow or token was expired.
// should route to login view.
}
}).then(isLoggedIn => {
if(isLoggedIn) {
// if once logged in, you can access api via "proxyFetch" method
auth.proxyFetch(`https://www.googleapis.com/youtube/v3/subscriptions?${new URLSearchParams({
part: "snippet",
mine: true
}).toString()}`)
.then(response => response.json())
.then(json => console.log(json))
} else {
// should route to login view.
}
})
const doLogin = () => {
// Go to authentication page.
auth.getToken()
}