Custom Function Authentication not working with nodejs package

Hi, i want to use Magic.link for my auth flow. I uploaded the @magic-sdk/admin as dependency and use it in my function like this:

exports = async (loginPayload) => {
    const { Magic } = require('@magic-sdk/admin');
    const mAdmin = new Magic('<API_KEY>');
    
    try {
      const didToken = loginPayload;
      const metadata = await magic.users.getMetadataByToken(didToken);
      const userID = metadata.issuer;
      const userEmail = metadata.email;
      
      return { "id": userID, "email": userEmail };
      
    } catch(error) {
      console.log(error);
    }
    return;
  };

but when i try to run it, i get the following error:

failed to execute source for 'node_modules/@magic-sdk/admin/dist/index.js'

How can i solve it?

Hi @Niklas_Grewe,

How did you upload the magic-sdk module to your realm application and how do you run the custom function auth?

Please be prepare that the dependencies is in BETA and not all uploaded modules are supported currently on MongoDB Realm.

Can you use an API call to an ADMIN interface instead of uploading code?

Perhaps cosider using an alternative like Auth0 API for magic links.

Best regards,
Pavel

How did you upload the magic-sdk module to your realm application?

in a empty directory, i run

npm install --save @magic-sdk/admin
tar -czf node_modules.tar.gz node_modules/

i uploaded the node_modules.tar.gz package to Realm over the UI. I see this under the Dependencies Tab:

and how do you run the custom function auth?

import { Magic } from 'magic-sdk';
const m = new Magic('API_KEY');

async function loginWithMagic() {
   try {
      const didToken = await m.auth.loginWithMagicLink({ email: 'hello@example.com', showUI: false });
      const credentials = Realm.Credentials.function(didToken);
      const user = await app.login(credentials);
      return user;
   } catch {
     // Handle errors if required!
   }
}

Can you use an API call to an ADMIN interface instead of uploading code?

i will try that

Perhaps cosider using an alternative like Auth0 API for magic links.

Magic.link use a blockchain to offer a dezentralised authentication flow. That’s higher secure as storing tokens, user data and so on in a single database. I don’t know, how secure is Auth0, but it is expensive as far as I know or what are your experiences in this regard?


Passportjs is also a very popular auth library for Nodejs. Is this already supported by Realm Functions? Do you know of any other passwordless authentication alternatives that work well with Realm? it might be helpful if Realm itself offered such a login method :grinning:

Hi @Niklas_Grewe,

I have reproduce the issue and it seems that this is a syntax error inside one of the inner dependencies, probably due to some modules been unsupported.

So this Blockchain database is stored as part of magic.link offering?

I am not aware of a solution that was POCd with Realm and maybe other people know.

I will test Auth0 to see if its possible to use it. I know that oauth providers and JWT solutions work with realm.

There is an interesting new article

Thanks.
Pavel

@Niklas_Grewe,

A wild idea might be to host this code somewhere like realm hosting or a lambda function and call it via rest or aws service from custom function…

@Pavel_Duchovny

I have reproduce the issue and it seems that this is a syntax error inside one of the inner dependencies, probably due to some modules been unsupported.

ok thanks for the info. Is there any hope that these will be supported in the future?

So this Blockchain database is stored as part of magic.link offering?

No. Magic is based on the Ethereum decentralized blockchain network to authenticte and share user data

I will test Auth0 to see if its possible to use it. I know that oauth providers and JWT solutions work with realm.

Thank you. Please let me know what you and what experience you have gained and what is possible. and what I would be particularly interested in. Auth0 offers the possibility to use external databases to store the created user accounts. Do we have a Chance to connect MongoDB realm also in this respect with Auth0? More Informations here: https://auth0.com/docs/connections/database#using-your-own-user-store

A wild idea might be to host this code somewhere like realm hosting

Its possible to host a nodejs app with realm hosting? I thought only static or single page applications are supported

Hi @Niklas_Grewe

Auth0 offers the possibility to use external databases to store the created user accounts. Do we have a Chance to connect MongoDB realm also in this respect with Auth0? More Informations here: https://auth0.com/docs/connections/database#using-your-own-user-store

Since you can connect it to MongoDB you can connect it to Atlas cluster I believe but this needs to be tested. Perhaps you can even use the wire protocol and its realm connection string to do a direct realm store.

Its possible to host a nodejs app with realm hosting? I thought only static or single page applications are supported

Well depands on what you define as an app and its purpose.

I think with webpack you should be able to create a deployable application

I once blogged about stitch hosting and it has the same mechanism just rebrand to realm:

Thanks
Pavel

Hi @Pavel_Duchovny

Since you can connect it to MongoDB you can connect it to Atlas cluster I believe but this needs to be tested. Perhaps you can even use the wire protocol and its realm connection string to do a direct realm store.

Since you still wanted to test Auth0 with Realm, could you check that too? I am still a beginner when it comes to MongoDB and Realm. With this you would help me :+1:

Hi @Pavel_Duchovny

i now find a way to use Magic.link with Realm Custom Functions. It works but, how can i add User Metadata like there email address? The Realm Docs says:

Custom Function authentication users are not automatically associated with any data.

How can I save user metadata without interrupting Realm’s automatic create/login process?

Hi @Niklas_Grewe,

Can you share how you made it work?

Custom data is stored in a collection you can query it via the database service in a function or through context after user is authenticated and run function:

If you need to access from sdk after Auth completed check the relevant sdk docs.

Thanks