Closed mfgabriel92 closed 6 years ago
Hey @mfgabriel92! 👋
Are you sending the token from your client with the correct format to each request after being authenticated?
Hi @RomainLanz,
I am not sure. How would I do it? I have this following Redux action that triggers the methods above:
export function login ({ email, password }) {
return {
[CALL_API]: {
endpoint: '/api/auth/access-token',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
password
}),
types: [
LOGIN,
{
type: LOGIN_SUCCESS,
meta: {
done: true,
transition: {
success: (prevState, nextState, res) => {
return {
pathname: '/dashboard'
}
}
}
}
},
LOGIN_FAIL
]
}
}
}
By using the Authorization
HTTP header.
Hi, @RomainLanz
Nothing. I am sending the Authorization
header to the method I am requesting, but it still is undefined
.
return dispatch({
[CALL_API]: {
endpoint,
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
},
types: [ GET_ALL_GIGS, GET_ALL_GIGS_SUCCESS, GET_ALL_GIGS_FAIL ]
}
});
By logging out request.auth
, the user
object is also null
.
....
options:
{ serializer: 'Lucid',
model: 'App/Model/Token',
scheme: 'api',
expiry: '1d' },
user: null },
user: null }
This causes the .middleware('auth')
to fail, saying the login failed, followed with the message:
(node:20533) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: You may only yield a function, promise, generator, array, or object, but the following object was passed: "undefined"
Have you checked from devtools that you are correctly sending the token?
Yes. I found out that I hadn't set up the accessToken
when login is successful. This caused the error, because I saw that the token stored was different from the one newly generated.
I'm still using legacy version, and my application uses API tokens for authentication, therefore I would have to make use of
request.authUser
to be able to access the logged in user, but it's returningundefined
for me.I checked the initial configuration in
auth.js
, it matches what's said in the documentation. I also generate a token when the user logs in, withrequest.auth.generate(user)
, but even though, when checking for a logged in user withrequest.auth.check()
, it returnsfalse
, and everything else isundefined
.When the login operation is executed, this method below will execute before generating a token:
Which then returns the
User
to the controller, allowing the generation of the token:I also confirmed the
accessToken
value storing the user's token from the database when checking Networking tab on Google Chrome.