Skip to main content
Deno 2 is finally here 🎉️
Learn more

Deno GOAuth2

deno module deno compatibility Release

A minimalist google oauth2 client library with zero dependency

Example

Minimal usage login and logout, you can think for more complex logic

import {
  getRedirectUrl,
  type OAuth2Config,
  exchangeToken,
  revokeAccessToken
} from 'https://deno.com/x/deno_goauth2/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',
  REDIRECT_URL: 'https://example.com/auth/callback', // example callback url
  SCOPES: ['https://www.googleapis.com/auth/userinfo.profile'] // example scopes
}

function login() {
  const url = getRedirectUrl(CONFIG)

  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(
    CONFIG,
    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)
}

More Examples

Need more examples real project ? see here