I
I
IDONTSUDO2019-05-20 02:12:23
JSON Web Token
IDONTSUDO, 2019-05-20 02:12:23

jwt dynamic request routing, is it possible?

I want to dynamically route requests to collections in my database by encrypting the jwt id of the user from my database.
Request schema.
endpoint: /auth ----> response ---> jwt[id]
//authorization
endpoint: /user_lenta{get} ---------> request jwt[id]
endpoint: /user_lenta --- ------> response [response]

This endpoint leads to collections of all users. And in order to distinguish one request from another, I want to encrypt the user ID from another collection. How good is this solution for organizing sessions in a microservice architecture? And are there examples of its application in practice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IDONTSUDO, 2019-05-20
@IDONTSUDO

server.route({
            method: 'GET', 
            path: '/profile', // определяем endpoint 
            handler:(req,  h) => { 
                var decode = jwt.verify(
                    reg.headers.authorization,
                    process.env.SECRET_KEY
                )  // передаем на него req  и расшифровываем его
                return User.findOne({
                    _id: mongoose.Types.ObjectId(decode.id)
                }) // при помощи moongoose ищем id колекции 
                .then(user => {
                    console.log(user)
                    if(user){
                        return user
                    }else{
                        return 'User dont exist'
                    }
                })
                .catch(err => {
                    return 'error:' + err
                })
            }
        })
    } // все остальное обработка ошибок

The behavior I expected. happi.js implementation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question