adonisjs / auth

Official Authentication package for AdonisJS
https://docs.adonisjs.com/guides/auth/introduction
MIT License
196 stars 63 forks source link

JWT - token middelware Routes not working In Adonis API #84

Closed Jayaramvenkatanarayanan closed 6 years ago

Jayaramvenkatanarayanan commented 6 years ago

I am using JWT token based auth in Adonis API. Login API working fine. I cant use the jwt auth check for other routes. for example, Admin login success full, but that same admin auth check not working other activities.

   My Routes:

   Route.group(() => {
   Route.get('user', 'UserController.index');
   Route.post('addUser', 'UserController.store');
   Route.get('getUser/:id', 'UserController.showId').middleware(['auth:jwt']);
   Route.put('updateUser/:id', 'UserController.userUpdate');
   Route.delete('deleteUser/:id', 'UserController.remove');
   Route.post('login', 'UserController.login');
   Route.get('check', 'UserController.check').middleware(['auth:jwt']);
   }).prefix('api/v2');

  My auth.js
     jwt: {
            serializer: 'lucid',
            model: 'App/Models/User',
            scheme: 'jwt',
            uid: 'email',
            password: 'password',
            options: {
             secret: 'self::app.appKey'
             }
             }
         }

User controller:

   async login({request, auth, response}) {
          const {email, password} = request.all();
          let token = await auth.attempt(email, password);
           return response.status(200).json({data: token, message: 'Login successfull', status: true});
          }

Its working fine.

But i want to check get users from DB using id passing in url with auth check it's not working.

      //get by id

        async showId({params, response, auth}) {
         try {
           let play = await auth.generate(user);
            console.log(play);
           let userInfo = await User.find(params.id)
             if (userInfo != null) {
             return response.json({data: userInfo, auth: auth, message: 'get the record', status: true})
           }
            return response.status(404).json(notFound)
          } catch (error) {
                   response.send('Missing or invalid jwt token')
             }

            }
   URL : oute.get('getUser/:id', 'UserController.showId').middleware(['auth:jwt']);

Response jwt

I passed header also.check this any issues.