Skip to content
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

[RFR] [BC break] Reimplement auth logic using hooks #3655

Merged
merged 26 commits into from
Sep 10, 2019
Merged

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Sep 5, 2019

  • Reimplement auth logic using hooks (no Redux, no saga)
  • Use new hooks in Login, Logout, Resource, and CoreAdminRouter
  • In the Login page, redirect to home page if user is authenticated (refs Login Page should redirect to the app if the user is already logged #2953)
  • Use new hooks in examples
  • Update auth documentation
  • Handle auto logout when dataProvider returns a 403 (by using useAuthProvider inside useDataProvider)
  • Write upgrade guide
    • no more Redux action dispatched for auth calls
    • we systematically pass the location to the authProvider (rather than just the pathName for certain verbs)
import { useLogin } from 'react-admin';

const LoginButton = () => {
    const [loading, setLoading] = useState(false);
    const login = useLogin();
    const handleClick = {
        setLoading(true);
        login({ username: 'john', password: 'p@ssw0rd' }, '/posts')
            .then(() => setLoading(false));
    }
    return <button onClick={handleClick}>Login</button>;
}

import { useLogout } from 'react-admin';

const LogoutButton = () => {
    const logout = useLogout();
    const handleClick = () => logout();
    return <button onClick={handleClick}>Logout</button>;
}

import { useCheckAuth } from 'react-admin';

const MyProtectedPage = () => {
    const checkAuth = useCheckAuth();
    useEffect(() => {
        checkAuth().catch(() => {});
    }, []);
    return <p>Private content: EZAEZEZAET</p>
} // tip: use useAuthenticated() hook instead

import { useCheckAuth } from 'react-admin';

const MyPage = () => {
    const checkAuth = useCheckAuth();
    const [authenticated, setAuthenticated] = useState(true); // optimistic auth
    useEffect(() => {
        checkAuth(undefined, false)
             .then() => setAuthenticated(true))
             .catch(() => setAuthenticated(false));
    }, []);
    return authenticated ? <Bar /> : <BarNotAuthenticated />;
} // tip: use useAuthState() hook instead

@fzaninotto fzaninotto added this to the 3.0.0 milestone Sep 5, 2019
@fzaninotto fzaninotto changed the title [WIP] [BC break] Reimplement auth logic using hooks [RFR] [BC break] Reimplement auth logic using hooks Sep 9, 2019
@fzaninotto
Copy link
Member Author

Switching to RFR!

Copy link
Collaborator

@djhi djhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@djhi djhi merged commit 47c7171 into next Sep 10, 2019
@djhi djhi deleted the auth-side-effects branch September 10, 2019 07:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants