I have created a web app and use Auth0 for authentication. Each user has his own page with personal data.
How can I connect an user logged via Auth0 to my Mysql database in order to retrieve and update his specific data?
I have created a web app and use Auth0 for authentication. Each user has his own page with personal data.
How can I connect an user logged via Auth0 to my Mysql database in order to retrieve and update his specific data?
The tokens returned from Auth0 contain encoded data. When you decode it you can see for example something like this.
{
"iss": "http://YOUR_DOMAIN/",
"sub": "auth0|123456",
"aud": "YOUR_CLIENT_ID",
"exp": 1311281970,
"iat": 1311280970,
"name": "Jane Doe",
"given_name": "Jane",
"family_name": "Doe",
"gender": "female",
"birthdate": "0000-10-31",
"email": "janedoe@example.com",
"picture": "http://example.com/janedoe/me.jpg"
}
This is the payload of an ID token which I copied from this page. You can connect the Auth0 user and the user data in your database by using a common unique ID. Here you could use sub for example. You can also use the access token. It contains the same sub but some of the other data is missing.
Create an authToken state in your user reducer & upon sign in set this authToken state equal to received auth0 token. Then for each subsequent request for data retrieval pass this authToken value to validate your request.
reducer.js
const INITIAL_STATE = {
...
authToken = '';
};
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
...
...
case 'SET_AUTH_TOKEN':
return ({
...state,
authToken: action.payload
})
}
};
To persist this authToken for a session you can use redux-persist library which uses localStorage & sessionStorage to preserve redux state upon reload.