DEV Community

Discussion on: Building an API with AdonisJS (part 3)

Collapse
 
yonglee79 profile image
Yong Lee • Edited

Hi Danilo Miranda,
I was following your instructions but there seems a typo from EventController.store.
this code worked

async store({ request, response, auth }) {
try {
const { title, location, date, time } = request.all(); // info for the event
const user = await auth.getUser(); // retrieving user id currently logged in
console.log(user);
const newEvent = await Event.create({
user_id: user.id,
title,
location,
date,
time
});

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

}

I kept getting no response from a server when I used
const userID = auth.user.id

and isn't it better to pass jwt to the server?

and in route.js
instead of just Route.post('events/create', 'EventController.store'),
Route.post('events/create', 'EventController.store').middleware(['auth]);
should be used.

How do you know that the user is logged in when it just api?
Did you want to just pass the user id which is saved in frontend to the server to create an event after
logging in from the login route?