v1.1.2
Minimal Google OAuth2 client library
Repository
Current version released
2 years ago
Minimal Google OAuth2 Client Library for Deno
A minimalist google oauth2 client library with zero dependency
Example
Minimal usage login and logout, you can think for more complex logic
import GoogleOAuth2Client from 'https://deno.com/x/google_oauth2_client/mod.ts'
import type { oAuth2Config } from 'https://deno.com/x/google_oauth2_client/mod.ts'
import { getCookies, setCookie } from 'https://deno.land/std/http/cookie.ts'
const CONFIG: oAuth2Config = {
CLIENT_ID: 'example-client-id',
CLIENT_SECRET: 'example-client-secret',
AUTH_ENDPOINT: 'https://accounts.google.com/o/oauth2/v2/auth',
TOKEN_ENDPOINT: 'https://oauth2.googleapis.com/token',
REDIRECT_URL: 'https://example.com/auth/callback', // example callback url
SCOPES: ['https://www.googleapis.com/auth/userinfo.profile'], // example scopes
REVOKE_TOKEN_ENDPOINT: 'https://oauth2.googleapis.com/revoke'
}
const oAuth2Client = new GoogleOAuth2Client(CONFIG)
function login() {
const url = oAuth2Client.getRedirectUrl()
return new Response('REDIRECTING', {
headers: { Location: url },
status: 307
})
}
async function callback({ headers, url }: Headers) {
const code = new URL(url).searchParams.get('code')!
const { access_token, expires_in, refresh_token } = await exchangeToken(code)
const res = Response.json({ message: 'Welcome..' })
setCookie(res.headers, {
name: 'access_token',
value: access_token,
maxAge: expires_in,
path: '/',
httpOnly: true,
secure: Deno.env.get('MODE') === 'production' ? true : false
})
}
async function logout({ headers }: Headers) {
const { access_token } = getCookies(headers)
await revokeAccessToken(access_token)
}