Skip to content

Commit

Permalink
⚡ change authentication function region
Browse files Browse the repository at this point in the history
See #41 (comment). Also updates firebase-functions.
  • Loading branch information
hgwood committed Nov 5, 2020
1 parent 1f86596 commit ff350f5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 116 deletions.
100 changes: 18 additions & 82 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"auth0": "^2.27.1",
"cors": "2.8.5",
"firebase-admin": "8.10.0",
"firebase-functions": "3.5.0",
"firebase-functions": "3.11.0",
"mailgun-js": "0.22.0",
"node-fetch": "2.6.1"
},
Expand Down
72 changes: 39 additions & 33 deletions functions/src/exchange-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,45 @@ const errorResponse = (message: string) => ({ error: { message } });
* Cannot use functions.https.onCall here because this function is called
* before the user is authenticated to Firebase.
*/
export const exchangeToken = functions.https.onRequest((request, response) => {
const { userId, accessToken } = request.body.data as RequestPayload;
if (!userId || !accessToken) {
response.status(400).send(errorResponse("Missing fields in request body"));
return;
}
export const exchangeToken = functions
.region("europe-west1")
.https.onRequest((request, response) => {
const { userId, accessToken } = request.body.data as RequestPayload;
if (!userId || !accessToken) {
response
.status(400)
.send(errorResponse("Missing fields in request body"));
return;
}

const authenticationClient = new AuthenticationClient({
domain: config.auth0.domain,
clientId: config.auth0.client_id
});
const authenticationClient = new AuthenticationClient({
domain: config.auth0.domain,
clientId: config.auth0.client_id
});

authenticationClient.getProfile(
accessToken,
async (userInfoErr, user: any) => {
if (userInfoErr) {
console.error(userInfoErr);
response.status(401).send(errorResponse("Unauthorized"));
return;
} else if (userId !== user.sub) {
response
.status(401)
.send(errorResponse("userId and accessToken do not match"));
return;
}
try {
const customToken = await exchangeTokenApp
.auth()
.createCustomToken(userId, { email: user.email });
response.send({ result: { token: customToken } });
} catch (err) {
response.status(500).send(errorResponse("Error creating custom token"));
authenticationClient.getProfile(
accessToken,
async (userInfoErr, user: any) => {
if (userInfoErr) {
console.error(userInfoErr);
response.status(401).send(errorResponse("Unauthorized"));
return;
} else if (userId !== user.sub) {
response
.status(401)
.send(errorResponse("userId and accessToken do not match"));
return;
}
try {
const customToken = await exchangeTokenApp
.auth()
.createCustomToken(userId, { email: user.email });
response.send({ result: { token: customToken } });
} catch (err) {
response
.status(500)
.send(errorResponse("Error creating custom token"));
}
}
}
);
});
);
});

0 comments on commit ff350f5

Please sign in to comment.