-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authentication in react-admin #24
Comments
It uses ApolloClient so you can set authentication as you'd normally do in Apollo. Here are some exmples. As mentioned here, you can supply your own Apollo client (once configured for authentication) // Create and configure your own client
const myLinkWithAuthentication = { /* see Apollo docs */ };
const client = new ApolloClient({
link: myLinkWithAuthentication
});
// Supply the Apollo client to buildOpenCrudProvider
buildOpenCrudProvider({ client }); |
If you use it with a prisma token, you will receive an error message, and redirection to the login page will not occur (ra failed)
My App.js const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
}); My authProvider.js import { AUTH_LOGIN, AUTH_LOGOUT, AUTH_ERROR, AUTH_GET_PERMISSIONS, AUTH_CHECK } from 'react-admin';
// import decodeJwt from 'jwt-decode';
export default (type, params) => {
if (type === AUTH_LOGIN) {
const { token } = params;
localStorage.setItem('token', token);
}
if (type === AUTH_LOGOUT) {
localStorage.removeItem('role');
return Promise.resolve();
}
if (type === AUTH_ERROR) {
debugger
}
if (type === AUTH_CHECK) {
return localStorage.getItem('token') ? Promise.resolve() : Promise.reject();
}
return Promise.reject('Unknown method');
}; (I also use a custom login page where I just get the token from user) How we can resolve this issue? |
+1 |
Doc should be improved to showcase authentication example, it's not that straightforward. |
Here's how I did it. const httpLink = createHttpLink({
uri: 'https://api-euwest.graphcms.com/v1/xxxxx/master',
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = '';
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
};
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
buildGraphQLProvider({
client,
}).then((dataProvider) => this.setState({ dataProvider })); |
I tried to implement authentication with prisma with no success.
Do you have a working example with auth?
The text was updated successfully, but these errors were encountered: