0

I'm a noob so be gentle. I'm building a set of Firebase Functions for an app I will write shortly. The 1st set of Functions were written with ...https.OnRequest and the userid (uid) added as part of of the request. All the Functions work well. I plan on using Firebase Authentication in the app.

I'm now trying to make them slicker and more secure by using the Firebase Auth variables i.e. context.auth.uid

Here is my code:

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
const test = require('firebase-functions-test')()
admin.initializeApp();

export const getuserlastcheckin = functions //gets last checkin\checkout for a user
.region('europe-west1')     // test uid = KuhGbmNXQT5NYM2WkePP
.https.onCall( (data, context) =>{
  const uid = context.auth.uid
    admin.firestore().collection(`checkinstatus`).doc(`${uid}`).get()
    .then(snapshot =>{
      const data1 = snapshot.data()
       const merged = {uid, data1}
      return(merged)
  })
    .catch(error => {
        console.log(error)
       return(error)
    })
 })


export const somefunction = functions
.region('europe-west1')
.https.onCall(async(data, context) =>{
  const userId = 'KuhGbmNXQT5NYM2WkePP'
  const testWrappedFunction = test.wrap (getuserlastcheckin)
  // Execute our wrapped function to test it.
  await testWrappedFunction( 
    {},
    {
      auth: {
        id: userId,
      },
    },
  )
})

VS Code has a problem with context.auth in the line const uid = context.auth.uid

I have tried ignoring the error and deploy the functions anyway and this also errors:

src/index.ts:9:15 - error TS2532: Object is possibly 'undefined'.

9   const uid = context.auth.uid
                ~~~~~~~~~~~~


Found 1 error.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\.......\AppData\Roaming\npm-cache\_logs\2021-01-06T12_25_20_299Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

The log file says:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   '--prefix',
1 verbose cli   'C:\\Users\\Wyn\\firebaseprojects\\xyz\\functions',
1 verbose cli   'run',
1 verbose cli   'build'
1 verbose cli ]
2 info using npm@6.14.8
3 info using node@v14.15.1
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle functions@~prebuild: functions@
6 info lifecycle functions@~build: functions@
7 verbose lifecycle functions@~build: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~build: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\Wyn\firebaseprojects\trackapp\functions\node_modules\.bin;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\nodejs\;C:\Program Files\Microsoft VS Code\bin;C:\src\flutter\bin;C:\Program Files\Git\cmd;c:\curl;C:\Users\Wyn\AppData\Roaming\npm;c:\src\flutter\bin
9 verbose lifecycle functions@~build: CWD: C:\Users\Wyn\firebaseprojects\trackapp\functions
10 silly lifecycle functions@~build: Args: [ '/d /s /c', 'tsc' ]
11 silly lifecycle functions@~build: Returned: code: 2  signal: null
12 info lifecycle functions@~build: Failed to exec build script
13 verbose stack Error: functions@ build: `tsc`
13 verbose stack Exit status 2
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:315:20)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:315:20)
13 verbose stack     at maybeClose (internal/child_process.js:1048:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
14 verbose pkgid functions@
15 verbose cwd C:\Users\Wyn\firebaseprojects\xxxxx
16 verbose Windows_NT 10.0.10586
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "--prefix" "C:\\Users\\Wyn\\firebaseprojects\\xxxxxx\\functions" "run" "build"
18 verbose node v14.15.1
19 verbose npm  v6.14.8
20 error code ELIFECYCLE
21 error errno 2
22 error functions@ build: `tsc`
22 error Exit status 2
23 error Failed at the functions@ build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 2, true ]

I assume I've made a simple typo or silly mistake. But what is it?

Thanks in advance.

Wyn
  • 21
  • 2

0 Answers0