This one is starting to get under my skin...
I'm playing around with Firebase and Functions right now and made a very simple API with express as the middleware.
So I have this route:
...
app.get('/getAuthUrl', async (req, res) => {
const s: sessionManagement.ISessionManagement =
sessionManagement.SessionFactory.createSession(
functions.config().aproxy.session.mode, req, db)
// ...this work
const getback = req.query.getback;
await db.collection('tokens').doc('getback').set({getback});
// this not?!
s.setReturnURL(req.query.getback);
res.setHeader('Cache-Control', 'private');
res.status(200).json(new Message(redirectUri));
})
At first, I use the 2 lines under the comment "...this work" and it did work; writing an URL to a Firestore database. So far, so good.
Then I decided to get fancy and decide to use a object factory to manage the fact that using Express session on a localhost serving functions (with the emulator) on one port and the angular frontend on an other was causing some headache with session management. Long story short, I setup a factory that will return me a object that will manage if I was running localy or on Firebase cloud hosting and used a different strategy.
This is where Promise started to not holding their... promise!?
The line s.setReturnURL(..
call this method inside my factory:
export interface ISessionManagement {
setReturnURL: (value: string) => void
}
export class FirestoreSession implements ISessionManagement {
private database: FirebaseFirestore.Firestore
private fingerprint: string
constructor(database: FirebaseFirestore.Firestore, fingerprint: string) {
this.database = database
this.fingerprint = fingerprint
}
setReturnURL(value: string) {
console.log('setReturnURL')
this.writeDoc(value, 'getback')
}
writeDoc(value: string, document: string) {
(async () => {
console.log('inside async')
const doc = this.database.collection('tokens').doc(document).set({value})
const result = await doc
console.log(result)
})().catch(error => {
console.log(error)
})
console.log('finish writeDoc')
}
}
export class SessionFactory {
public static createSession(mode: string, req: express.Request, db: FirebaseFirestore.Firestore) : ISessionManagement {
if (mode === 'fingerprint' ) {
console.log('fingerprint mode');
// Use for local testing, since in multiport solution, cookie will not be unique to a session
return new FirestoreSession(db, fingerPrintMe(req));
} else {
// For production
return new ExpressSession(req);
}
}
}
So, here the console output of the flow of execution if a use my factory object:
✔ functions[app]: http function initialized (http://localhost:5000/myplayground/us-central1/app).
i functions: Beginning execution of "app"
> fingerprint mode
> setReturnURL
> inside async
> finish writeDoc
i functions: Finished "app" in ~1s
The code above is my latest attempt to make this work. I tried A LOT of variation, moving my async/await around but nothing budge!
What am I missing??? I feel the answer will make me crawl under some rock but I don't care, I need to take this one off my mind :-)
UPDATE
Turns out that I need to async/await all the way down to the call that return a promise to make this work AND ALSO, you cannot await a function that return void, so I had to change the interface signature. So here the code fragment that work :
app.get('/getAuthUrl', async (req, res) => {
...
await s.setReturnURL(req.query.getback)
...
})
...
export interface ISessionManagement {
setReturnURL: (value: string) => any
}
export class FirestoreSession implements ISessionManagement {
...
async setReturnURL(value: string) {
await this.writeDoc(value, 'getback')
}
...
async writeDoc(value: string, document: string) {
try {
const doc = await this.database.collection('tokens').doc(document).set({value})
console.log(doc)
} catch (error) {
console.log(error)
}
}
}