question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: authProvider.checkAuth is not a function

See original GitHub issue

What you were expecting:

login page

What happened instead:

TypeError: authProvider.checkAuth is not a function

Steps to reproduce:

Only added App.js and authProvider.js

Related code:

import * as React from "react";
import authProvider from './authProvider';
import { Admin, Resource, ListGuesser } from 'react-admin';
import jsonServerProvider from 'ra-data-json-server';


const dataProvider = jsonServerProvider('https://jsonplaceholder.typicode.com');
const App = () => (
    <Admin dataProvider={dataProvider} authProvider={authProvider}>
        <Resource name="users" list={ListGuesser} />
    </Admin>
);

export default App;

Other information:

Environment

  • React-admin version:^3.8.2
  • Last version that did not exhibit the issue (if applicable):
  • React version:^16.13.1
  • Browser:Chrome
  • Stack trace (in case of a JS error):
(anonymous function)
E:/code/my-app/node_modules/ra-core/esm/auth/useCheckAuth.js:49
  46 | if (params === void 0) { params = {}; }
  47 | if (logoutOnFailure === void 0) { logoutOnFailure = true; }
  48 | if (redirectTo === void 0) { redirectTo = defaultAuthParams.loginUrl; }
> 49 | return authProvider.checkAuth(params).catch(function (error) {
     | ^  50 |     if (logoutOnFailure) {
  51 |         logout({}, error && error.redirectTo
  52 |             ? error.redirectTo

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
artemkozlenkovcommented, Oct 28, 2020

I can confirm this happens by following the Authentication tutorial.

Apparently the code provided for the authProvider is incomplete (missing checkAuth):

// in src/authProvider.js
const authProvider = {
    login: ({ username, password }) =>  {
        const request = new Request('https://mydomain.com/authenticate', {
            method: 'POST',
            body: JSON.stringify({ username, password }),
            headers: new Headers({ 'Content-Type': 'application/json' }),
        });
        return fetch(request)
            .then(response => {
                if (response.status < 200 || response.status >= 300) {
                    throw new Error(response.statusText);
                }
                return response.json();
            })
            .then(auth => {
                localStorage.setItem('auth', JSON.stringify(auth));
            });
    },
    // ...
};

export default authProvider;

yes you’re right, the sample code is incomplete, probably intentionally since it would be pretty obvious; you may overcome this with looking at the type of AuthProvider in node_modules/ra-core/src/types.ts, there you will see minimum amount of methods necessary for good working authenticator, however, experience proves, you need only 3 of those:

login, checkAuth and getPermissions, so for the other use temporary work around to see the direct output:

// in src/authProvider.js
export const authProvider = {
    login: ({username, password}) => {
        const request = new Request('https://mydomain.com/authenticate', {
            method: 'POST',
            body: JSON.stringify({username, password}),
            headers: new Headers({'Content-Type': 'application/json'}),
        });
        return fetch(request)
            .then(response => {
                if (response.status < 200 || response.status >= 300) {
                    throw new Error(response.statusText);
                }
                return response.json();
            })
            .then(auth => {
                localStorage.setItem('auth', JSON.stringify(auth));
            });
    },
    checkAuth: (...params) => {
        return Promise.resolve();
    },
    getPermissions: () => {
        return Promise.resolve();
    }
};

please, note, that in order to build fully functioning, secure authenticator you may need to fully implement all those functions. Regards.

0reactions
djhicommented, Jun 10, 2021

We would gladly accept a PR to fix it 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing An Auth Provider - React-admin - Marmelab
Here is the interface react-admin expect authProvider objects to implement. const authProvider = { // authentication login: params => Promise.resolve(/* .
Read more >
Why do I not get redirected to /login when accessing a secure ...
It is offered to change the redirect using an argument to rejected promise: checkAuth: () => localStorage. getItem('token') ? Promise.
Read more >
Auth | JavaScript SDK | Firebase JavaScript API reference
Enable email/password accounts in the Firebase Console, under the Auth tab. auth/weak-password: Thrown if the password is not strong enough. example.
Read more >
[Solved]-How to use Auth0 with react-admin?-Reactjs
My answer is following react-admin approach where I use its authProvider like below. ... Convert authProvider into function where it takes the above...
Read more >
Source - GitHub
Changelog ## v4.5.2 * Fix `authProvider` hooks support for redirectTo: ... Fix `useGetManyAggregate` throws "filter is not a function" when getting 401 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found