Delete user though Auth0.swift

Hey,

I’ve got an Auth0 app though Auth0.swift for my iOS app, and I got it to work properly but I need to have a delete user button. One problem with this is that based on what I’ve read theres no way to delete users though the sdk and I found a related question here I need to delete user account in my iOS project(swift) is that applicable from mobile side? but I need help in creating an API. Can someone pls help me understand how to create an backend api using node?

Here’s my current code I got from a quickstart:

const express = require('express');
const app = express();
const { auth, requiredScopes } = require('express-oauth2-jwt-bearer');

// Authorization middleware. When used, the Access Token must
// exist and be verified against the Auth0 JSON Web Key Set.
const checkJwt = auth({
  audience: 'http://GitaApi:5000',
  issuerBaseURL: `https://dev-1lh8f0uy7cxikoik.us.auth0.com/`,
});

// This route doesn't need authentication
app.get('/api/public', function(req, res) {
  res.json({
    message: 'Hello from a public endpoint! You don\'t need to be authenticated to see this.'
  });
});

// This route needs authentication
app.get('/api/private', checkJwt, function(req, res) {
  res.json({
    message: 'Hello from a private endpoint! You need to be authenticated to see this.'
  });
});

const checkScopes = requiredScopes('read:messages');

app.get('/api/private-scoped', checkJwt, checkScopes, function(req, res) {
  res.json({
    message: 'Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this.'
  });
});

app.listen(3000, function() {
  console.log('Listening on http://localhost:3000');
});

Thanks,
Arjun

Hey there @jakkipally welcome to the community !

This is correct - As this action requires the Management API and thus client credentials, it cannot be safely carried out client side (see public vs. confidential clients).

Your node API will need to utilize the node-auth0 library and in particular implement the delete users function.

Your API might look something like this:

const express = require('express');
const bodyParser = require('body-parser');
const Auth0ManagementClient = require('auth0').ManagementClient;

const app = express();
const port = 3000;

// Auth0 Management API Client
const auth0 = new Auth0ManagementClient({
  domain: 'YOUR_AUTH0_DOMAIN',
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  scope: 'read:users update:users delete:users'
});

app.use(bodyParser.json());

// Endpoint to delete a user by their Auth0 User ID
app.delete('/delete-user/:userId', (req, res) => {
  const userId = req.params.userId;

  auth0.deleteUser({ id: userId }, (err) => {
    if (err) {
      console.error('Error deleting user:', err);
      return res.status(500).json({ error: 'Failed to delete user' });
    }

    res.status(200).json({ message: 'User deleted successfully' });
  });
});

app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});

I have not tested this code and am sharing just as an example :slight_smile:

Hope this helps point you in the correct direction!

Yes, after some tweaking, I got it to work. Thank you!

1 Like

That’s great news, thanks for confirming here! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.