Hi Sumedha, thank you. To explain this, I need details, so you’ll have to read a little long post – but let me do:
Can you explain why this isn’t simple? Is this because you can’t re-use the auth code that’s generated from a log-in twice?
One reason is that I’m not using Google’s SDK, so in my current code no authCode
and id_token
appear.
What does that mean? The first thing to mention, how I use Realm.Credentials.google()
func. In Realm’s official doc, you find Realm.Credentials.google(accessToken)
. The argument is accessToken
acquired via Google’s SDK. But I found another blog post which shows me I can use Realm.Credentials.google(redirectUri)
instead. It looked simpler so I adopted this approach.
The code looks something like this:
const redirectUri = process.env.NEXT_PUBLIC_REALM_LOGIN_REDIRECT_URL;
//...
const logIn = async () => {
const credentials = Realm.Credentials.google(redirectUri);
await app.logIn(credentials);
};
// in JSX
<button onClick={logIn}>Log In with Google</button>
This code successfully redirects user to Google’s login page. (I also used handleAuthRedirect
from realm-web
SDK to handle the user redirected back)
So, in my current code, I don’t know about Google’s id_token
or authCode
. They are nicely hidden behind. Yes if I write some code I will get access to them, but I want to skip the steps if possible. Does it make sense to you?
So next, the latter half, about JWT – yes, it looked very attractive so I have been trying it as a plan B in parallel. Actually, Next.js has a library called next-auth which can issue JWT as a sort of 3rd party authenticator. It also beautifully handles the client-server shared authentication.
The main problem I faced was that it seemed I have to manage two different logins accordingly. For example, a user clicks “Log out” button, it triggers next-auth
’s (3rd party’s) sign-out function. It doesn’t trigger Realm’s sing-out automatically and the user may remain signed-in with Realm. I may write my own code to handle this, but if there’s a bug it’s serious, so I gave up with this plan B. (I’ll maybe try again, I might be wrong)
Now you might come up with further questions, if so, please ask.