-
Notifications
You must be signed in to change notification settings - Fork 127
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
Can't get OAuth2 login to work #2
Comments
You could find a web example how to authenticate with OAuth2 in the "Manage Users > Auth via OAuth2" guide - https://pocketbase.io/docs/manage-users/#auth-via-oauth2 |
This guide is confusing: https://pocketbase.io/docs/manage-users/#auth-via-oauth2 The redirectUrl for both pages here is: http://localhost:8090/redirect.html |
Both pages are part of your client application (the example is for web but the OAuth2 API could be used also in mobile apps). The prebuilt PocketBase binary allows serving static content by placing your client files inside
For This url should be added in your OAuth2 provider app. This is different for each provider and some may not allow using http, localhost or deep link addresses for a redirectUrl. Google OAuth2 apps for example allows using localhost addresses only when your Google OAuth2 app is in "Test mode" (at least that was the case the last time I tested it). In general, the OAuth2 authentication requires 2 steps:
A good explanation of OAuth2 and the PKCE flow could be found in https://www.ory.sh/oauth2-for-mobile-app-spa-browser/#use-oauth2-with-pkce-instead. I apologize. I'm not a native speaker and sometimes I've difficulties trying to properly explain things in English. If you have suggestions how to improve it, please let me know and I'll update the docs. |
First, your English skills are excellent. I've followed everything exactly but I can't get things to work. I am verifying that I get a proper client.Users.authViaOAuth2(
provider.name,
params.get("code") || '',
provider.codeVerifier,
redirectUrl
).then((authData) => {
console.log('authData', authData);
}).catch((err) => {
console.error('error', err);
}); This throws an error:
The full page URL for my auth redirect page looks like this (after going through the Google auth page):
|
For |
That was it. The confusing part was that, inside of the I think it would really simplify things if you could somehow combine this into a single step. So if you could call an OAuth function in the Pocketbase binary, and just pass it the provider name ( |
I'm glad that you've managed to make it work. Providing twice the same url is required by the OAuth2 spec for verification purposes (the It is possible to add a redirectUrl configuration option in the admin area that could be passed seamlessly to the provider without requiring the end user to manually set it in the |
What if you have a separate redirect parameter for each provider in settings? |
Yes, that is what I meant by "add a redirectUrl configuration option in the admin area" but that will be only for a single platform. To mitigate this we would have to add 3 redirect fields (for web, android, ios) to each provider and I think that this will be even more confusing. |
Can't you just send the |
But we don't have the One possible workaround could be is to add a new PocketBase endpoint that will redirect to the providers OAuth2 login page and will store somewhere the user passed redirectUrl and when the user call In any case, you'd have to pass somewhere the |
That's exactly what I was proposing, an endpoint that does everything in a single call. Pocketbase could temporarily store the redirectUrl in the database, couldn't it, and avoid implementing sessions/cookies? It would require a unique redirect endpoint in the Pocketbase backend, but I think it's possible to make this whole thing work with a single API call. That's the way we do it at Supabase: https://supabase.com/docs/reference/javascript/auth-signin#sign-in-using-third-party-providers |
You might also look into how the Supabase api works, sending both an error object and the authData at the same time. Because the way Pocketbase works now, it throws an error when you have an invalid email or password, and it doesn't handle it gracefully. There's no easy way to retrieve an error message (say, to know if it timed out or was an invalid user/password combination, etc.) |
I don't think we can implement it the same way unless we add sessions/cookies (even if we store the redirect url in the database we'll have to link it to the previous request somehow, hence the sessions/cookies).
What do you mean by gracefully? In PocketBase I don't think it will make sense to return both error and user data because you don't have any user data when the request doesn't succeed (if we were using sessions or "guest logins" then it may make sense to have them as a fallback). The error message for email/pass login is intentionally generic to avoid (to some extend) users enumeration. But I'll have a more detailed look at Supabase's API this weekend. |
@burggraf I've reviewed Supabase and Appwrite's API and unfortunately I still think that we can't implement it in a single call in PocketBase due to lack of session cookies. Even if we handle the post-OAuth2 authorization action entirely server-side, on redirect we don't have a secure mechanism to return the auth data to the user. One possible approach is to have a default redirect callback page in PocketBase that will take care to store the authorized data in the localstorage and then redirect the user to his preferred redirect endpoint (implying that they are on the same domain). But this will work only in the browser (that is actually how it's implemented in Presentator, where we have a default I've opened a new issue in the main repo for better visibility with a proposal of using a persistent connections to handle even cases where redirect urls or deep links are not supported - pocketbase/pocketbase#55 |
The only documentation related to OAuth login is this:
const userAuthData2 = await client.Users.authViaOAuth2("google", "CODE", "VERIFIER", "REDIRECT_URL");
But there is no indication of what "CODE" and "VERIFIER" mean, or how to use them. Can you add a more complete example of how to use this?
The text was updated successfully, but these errors were encountered: