I have an endpoint where both users and guests (not authenticated) can post data to:
async create(
@requestBody({
content: {
'application/json': {
schema: {
type: 'object',
properties: {
create_account: {type: 'boolean'},
password: {type: 'string'},
password_repeat: {type: 'string'},
currency: {type: 'string'},
payment_method: {type: 'string'},
products: {type: 'array'},
voucher: {type: 'string'},
customer: {type: 'object'},
news_letter: {type: 'boolean'},
},
},
},
},
})
@inject(SecurityBindings.USER) currentUserProfile: UserProfile,
order: Omit<Order, 'id'>,
): Promise<{url: string}> {
const userId = currentUserProfile[securityId];
}
However, I am unsure how to get the logged-in user from the session as I am getting the following error:
The key 'security.user' is not bound to any value in context
How do I get the user id in this situation?
@secured
. Then the authentication service will setsecurity.user
which found from the token. The cause for the error is you have not decorated endpoint with@secured
so no user bound forsecurity.user
. I suggest you to remove@inject
from parameters and use request object to find either guest or user. – Willhiteuser
unless you decorated the class or endpoint with authentication decorator as mentioned by @rifa-achrinza – Willhite