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

Improve client side docs for authentication #397

Closed
ghostdevv opened this issue Oct 5, 2021 · 1 comment
Closed

Improve client side docs for authentication #397

ghostdevv opened this issue Oct 5, 2021 · 1 comment
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@ghostdevv
Copy link

ghostdevv commented Oct 5, 2021

Improve documentation

Link

https://supabase.io/docs/guides/auth/auth-apple

and other external provider pages

Describe the problem

Currently when using external ouath2 providers, you can't use the following code:

const { user, session, error } = await supabase.auth.signIn({
  provider: 'discord'
})

The reason being that when you call this function the user is redirected to the provider to sign in, which exits the function call and when you are returned to the page, unless supabase runs on page load the authtoken in the url isn't caught.

My solution to this was to have a listener on supabase.auth.onAuthStateChange which runs on every page, and allows me to have a switch statement like the following to reflect the state change:

    supabase.auth.onAuthStateChange((event, session) => {
        const data = session?.user;
        switch (event) {
            case 'SIGNED_IN':
                $user = {
                    id: data.user_metadata.provider_id,
                    supabaseId: data.id,
                    username: data.user_metadata.full_name,
                    avatarUrl: data.user_metadata.avatar_url,
                    email: data.email,
                };
                break;
            case 'SIGNED_OUT':
                $user = null;
                break;
        }
    });

Describe the improvement

Either the js client should improve to open a popup for external providers allowing for you to actually await the return of the session, or the need for what I said above should be added in the documentation. I noticed quite a few people in the discord faced a similar problem and this took me quite a while to figure out a good solution. And even this solution isn't perfect, I had to update it yesterday as it just stopped working in particular cases. I had to add a callback page witch ran supabase.auth.getSessionFromUrl(); to update the authStateChange function.

@ghostdevv ghostdevv added the documentation Improvements or additions to documentation label Oct 5, 2021
@monicakh monicakh transferred this issue from supabase/supabase-js Aug 18, 2022
@hf
Copy link
Contributor

hf commented Dec 30, 2022

A duplicate of #460.

@hf hf closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants