45
loading...
This website collects cookies to deliver better user experience
users
table in our database.user_id
is used to query from a unified table like tasks
. The problem with this approach is that when the project gets out there, hoarding all those github stars, malicious users could easily query the DB using it's URL and pretend to be other another user just using their user_id
. We could prevent this by implementing a robust identity management system and some access control at the database level. Let's see how we can implement exactly that.Launch Console
. This will open up a new tab where you can interact with the Hasura console. Data
tab at the topCreate Heroku Database
. You'll be shown some prompts regarding Hasura's connection to Heroku; approve the connectionspublic
on the Data Manager
tab to the left. Create a table using the Create table
button and add the necessary attributes (click on + Frequently used columns
to add common columns)users
table with attributes: id, name, federated_id. Make sure id and federated_id are set as unique.Add table
.Applications
under Applications
in the left sidebarCreate Application
button and enter the name of the application, let's call it Mini twitter
. If you're using a framework like Next.js on the frontend, select Regular Web Applications
as the application type. Select technology that you're using (this will let Auth0 recommend settings and instructions on how to set up the application); In our case, select Next.jsApplications > Applications > Settings > Domain
Env vars
on the left side and add a new env var HASURA_GRAPHQL_JWT_SECRET
. Paste in the config you copied into the field, click on Add
.users
table in Hasura DB. We'll implement the syncing using Auth0 actions. In the Auth0 dashboard, click Actions
on the left sidebar and click on Library
. This page will show all the actions that you've created. Click on the Build Custom
button. Custom
tab and then click on the newly created action. A code editor will open up. Paste the following code into the editor./**
* Handler that will be called during the execution of a PostUserRegistration flow.
*
* @param {Event} event - Details about the context and user that has registered.
*/
const axios = require("axios");
exports.onExecutePostLogin = async (event) => {
const federated_id = event.user.user_id;
const username = event.user.nickname;
const email = event.user.email;
const admin_secret = event.secrets.HASURA_SECRET;
const url = "https://YOUR_HASURA_ENDPOINT";
const query = `mutation ($federated_id: String!, $email: String, $username: String) {
insert_users(objects: {federated_id: $federated_id, email: $email, username: $username}, on_conflict: {constraint: users_federated_id_key}) {
affected_rows
}
}`;
const variables = {
federated_id: federated_id,
email: email,
username: username,
};
const config = {
headers: {
"content-type": "application/json",
"x-hasura-admin-secret": admin_secret,
},
};
const data = JSON.stringify({
query: query,
variables: variables,
});
await axios.post(url, data, config);
};
role
to determine the permissions allowed to the user. Furthermore, the user-id
is also evaluated to enable a more granular permission system. Thus we need to embed the role
and user_id
in the user token that we'll be using to gain access to Hasura.login-hasura-token
with a Login / Post Login
trigger. Add the following code into the action editor./**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
api.idToken.setCustomClaim("https://hasura.io/jwt/claims", {
"x-hasura-default-role": "user",
"x-hasura-allowed-roles": ["user"],
"x-hasura-user-id": event.user.user_id,
});
};
Actions
and then Flows
on the left sidebar. Login
option. A flow chart showing the login flow will be shown. Add the newly created actions by selecting them from the menu on the right. Drag the actions and place them into the flow chart. Make sure login-ping
is placed first. Click on apply.Getting Started
then click Try it out ->
in the Try your Login box
dialog box.