401 Unauthorized PLEASE HELP ME

hi,

i get error

HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: “Unauthorized”, url: “http://localhost:4200/users/userdetails”, ok: false, …}

  1. error: {message: “E_INVALID_JWT_TOKEN: jwt must be provided”, name: “InvalidJwtToken”, status: 401,…}

  2. frames: [{file: “node_modules/@adonisjs/auth/src/Exceptions/index.js”,…},…]

  3. message: “E_INVALID_JWT_TOKEN: jwt must be provided”

  4. name: “InvalidJwtToken”

  5. status: 401

is there anyone who can help me?

Your question contains lot of text, help others to help you by formatting your code. Then you may cross your hands and wait to see if the God of programming would send someone to help you.

is this ok?

Just format your code, do not remove it because it is needed for the person who may help you.

You’ll need to send a header with auth token on the request otherwise header is going to be empty and thus backend will end up thinking user is not authorised to see that specific page.

1 Like

Found a tutorial that is using JWT authentication.

thanks for the reply…

and I watch this tutorial and tried this way but I get this error

error: {message: “E_INVALID_JWT_TOKEN: jwt must be provided”, name: “InvalidJwtToken”, status: 401,…}

I want to do this;

I have 2 form
1- User ( migration - Model - Controller )
2- Userprofil ( migration - Model - Controller )

1.form I want user registration id, email and password and generate token set localstorage
(when i submitted button 1.form is working and generate token set localstorage )

then…
2.form I want create Userprofil id, user_id, name, surname, phone, compName, province, district, role… and ı want get user_id: user.id (in auth)
(when i submitted button 2.form not working i get this error )

but ı get this error

backend i use adonis api
front end angular 7

// this userprofile_schema.js

this.create(‘userprofile’, (table) => {
table.increments()
table.integer(‘user_id’).unsigned().references(‘id’).inTable(‘users’)
table.string(‘name’, 40)
table.string(‘surname’, 40)
table.string(‘phone’, 20)
table.string(‘compName’, 50)
table.string(‘province’, 20)
table.string(‘district’, 20)
table.string(‘role’, 20)
table.timestamps()
})
}

// create USER PROFİL (this ProfilController.js)

async create({ request, response, auth }) {

    try {

        const { name, surname, phone, compName, province, district, role } = request.all()

        **const userID = await auth.getUser()**

        const newUserProfil = await Userprofil.create({
            **user_id: userID.id,**
            name, surname, phone, compName, province, district, role
        })

        return newUserProfil

    } catch (err) {

        return response.status(err.status).send({ message: { error: 'Something went wrong while creating new event'}})

    }
}

—> const userID = auth.getUser()
—> user_id: userID.id,

this method not working…
i get this error: “E_INVALID_JWT_TOKEN: jwt must be provided”

when I disable it it s working

As stated by Tsume, your client request must embed the JWT token.

Correct. Your angluar 7 needs to pass your auth token via api so adonis can receive and authenticate user.

please help me

Hello @necati you can make this, following this steps:

First, in your UserController, you need attempt login

const token = await auth.attempt(email, password)

Second, add middleware auth on your Route file.

Route.post(‘profile’, 'ProfilController.create’).middleware('auth')

Third, add Header on your Client side request

header(‘authorization’, Bearer ${token})

I was forgetting, verify your config/auth.js authenticator is ‘jwt’

Regards!

1 Like

codiplace thanks for the reply…
this worked for me…

1 Like