Auth0 Home Blog Docs

Auth0 Integration with GoogleHome

I’m trying to integrate Auth0 with the sample (https://github.com/actions-on-google/smart-home-nodejs), but found no examples with TypeScript.

I know that concepts are missing for me, but the example is 100% working, it only needs to integrate the authentication.

The Original Code:

import * as express from 'express'
import * as util from 'util'
import { Headers } from 'actions-on-google'

import * as Firestore from './firestore'

/**
 * A function that gets the user id from an access token.
 * Replace this functionality with your own OAuth provider.
 *
 * @param headers HTTP request headers
 * @return The user id
 */
export async function getUser(headers: Headers): Promise<string> {
  const authorization = headers.authorization
  const accessToken = (authorization as string).substr(7)
  return await Firestore.getUserId(accessToken)
}

/**
 * A function that adds /login, /fakeauth, /faketoken endpoints to an
 * Express server. Replace this with your own OAuth endpoints.
 *
 * @param expressApp Express app
 */
export async function registerAuthEndpoints(expressApp: express.Express) {
  expressApp.use('/login', express.static('./frontend/login.html'))

  expressApp.post('/login', async (req, res) => {
    const {username, password} = req.body
    console.log('/login ', username, password)
    // Here, you should validate the user account.
    // In this sample, we do not do that.
    return res.redirect(util.format(
      '%s?client_id=%s&redirect_uri=%s&state=%s&response_type=code',
      '/frontend', req.body.client_id,
      encodeURIComponent(req.body.redirect_uri), req.body.state))
  })

  expressApp.get('/fakeauth', async (req, res) => {
    const responseurl = util.format('%s?code=%s&state=%s',
      decodeURIComponent(req.query.redirect_uri), 'xxxxxx',
      req.query.state)
    console.log(responseurl)
    return res.redirect(responseurl)
  })

  expressApp.all('/faketoken', async (req, res) => {
    const grantType = req.query.grant_type
      ? req.query.grant_type : req.body.grant_type
    const secondsInDay = 86400 // 60 * 60 * 24
    const HTTP_STATUS_OK = 200
    console.log(`Grant type ${grantType}`)

    let obj
    if (grantType === 'authorization_code') {
      obj = {
        token_type: 'bearer',
        access_token: '123access',
        refresh_token: '123refresh',
        expires_in: secondsInDay,
      }
    } else if (grantType === 'refresh_token') {
      obj = {
        token_type: 'bearer',
        access_token: '123access',
        expires_in: secondsInDay,
      }
    }
    res.status(HTTP_STATUS_OK).json(obj)
  })
}

Hey there @marcelo.arnaldi!

Unfortunately don’t have much of experience with Google Home but I was able to find this article from one of Google Developer Advocates on how to tie Google Home with Auth0:

You may find it helpful!

Hello, thank you for the answer, I saw this tutorial and my difficulty was to use the Auth0 library in TypeScript, but after a lot of fighting I finally got it!

It really was my lack of knowledge.

No worries! Glad you have it working now! If you can share the code snippets you used, with the community it would be perfect!

Oi Konrad,
This is part of the code:

import { AuthenticationClient } from 'auth0'

const auth0 = new AuthenticationClient({
   domain: '<my-domin-auth0>.auth0.com',
   clientId: '<clientId>',
   clientSecret: '<clientSecret>'
});

export async function getUser(headers: Headers): Promise<string> {       
   const authorization = headers.authorization
   const accessToken = (authorization as string).substr(7)
   const {email} = await auth0.getProfile(accessToken)
   console.log(email)
   return await Firestore.getUserId(accessToken)
}

Is there another more correct way to advertise?

Thanks a a lot for sharing it with us!

So when you build something complete using Auth0 stack it’s worth showcasing it within “Show your Auth0” category.