For whatever reason, users may want to delete their tweets. So let's give them the ability to do that. Add the line below in start/routes.js just after the route to reply a tweet:

// start/routes.js

Route.delete('/tweets/destroy/:id', 'TweetController.destroy').middleware(['auth:jwt'])

This route takes the ID of the tweet a user wants to delete.

Next, let's create the destroy method. Add the code below in TweetController:

Table of Contents

    // app/Controllers/Http/TweetController.js
    async destroy ({ request, auth, params, response }) {
        // get currently authenticated user
        const user = auth.current.user
        // get tweet with the specified ID
        const tweet = await Tweet.query()
        await tweet.delete()
        return response.json({
            status: 'success',
            message: 'Tweet deleted!',
            data: null

    Again, we get the currently authenticated user. Then we fetch the tweet matching the specified ID and the ID of the authenticated user. That way, users can delete only their own tweets. Then we delete the tweet.

    Chimezie Enyinnaya

    20 posts

    Software Developer [PHP Laravel JavaScript NodeJS AdonisJS VueJS] | movie lover | run