Hi,
This has probably been asked thousands of times, but after multiple attempts last year(s) and a lot of reading I still can’t find a proper solution. Maybe someone can help me.
What is the ‘best’ way to handle API calls to my own API to make sql-queries?
My setup (let’s say it is a todo-app):
- 1 auth0 application with database connection
- App-backend: nodejs API built with express and postgresql database.
- App-database: postgresql database with 2 tables: user (id, auth0) and todo (id, title, status, user_id).
Questions:
-
When to create the user in the local db? Now I use a rule to create new user using a post-request (api.todo.com/user) with a secret in the authorization header. I store the postgres.user.id in the user’s app_metadata. This seems to work (except that error handling should be improved).
// An auth0 Rule to create a new local accout when the Auth0 acount is not yet connected. function (user, context, callback) { // Fetch app metadata user.app_metadata = user.app_metadata || {}; // If local_id exists, retusn if (user.app_metadata.local_id) { return callback(null,user,context); } // otherwise create a local account var request = require('request@2.56.0'); // Create authorization header var secret = configuration.LOCAL_DB_SECRET; var authorizationHeader = 'Basic: ' + secret; var url = configuration.LOCAL_DB_URL; var payload = { 'user_id': user.user_id, 'email': user.email }; var options = { method: 'POST', url: url + '/user', headers: { 'content-type': 'application/json', 'accept': 'application/json', 'Authorization': authorizationHeader }, body: JSON.stringify(payload) }; request(options, function(err, response, body) { if (err) return callback(err); if (response.statusCode !== 200) return callback(new Error(body)); // Parse the incoming data. var data = JSON.parse(body); var local_id = data.id; if (!local_id) { return callback(); } user.app_metadata.local_id = local_id; auth0.users.updateAppMetadata(user.user_id, user.app_metadata); return callback(null, user, context); }); } ```
-
How to grant access to the local API? I read that this has to be done with the access_token. I also want to send the local_id with the request in the token, because I want to query by primary key (dbuser.id) instead of the auth0 value (db.user.auth0);
Right now I add the app_metadata to the id_token with a rule:function (user, context, callback) { const namespace = configuration.METADATA_NAMESPACE; if (context.idToken && user.user_metadata) { context.idToken[namespace + '/user_metadata'] = user.user_metadata; } if (context.idToken && user.app_metadata) { context.idToken[namespace + '/app_metadata'] = user.app_metadata; } callback(null, user, context); }
I am using Ionic 4 for the frontend, a native app, and a PWA in the browser. My guess is that it should be possible that the frontend makes a request to the backend-api (api.todo.com/item), with a jwt that can be validated by the api, and the user.id. So that only a single db-query is needed: SELECT * FROM items WHERE user.id=token.user_id.
Somehow I can’t seem to find out how (or if) this is possible. Any advice is welcome!