MongoDB Stitch JWT custom auth: valid UID required (between 1 and 128 characters) MongoDB Stitch JWT custom auth: valid UID required (between 1 and 128 characters) mongoose mongoose

MongoDB Stitch JWT custom auth: valid UID required (between 1 and 128 characters)


Solved:

Set previusly on payload:

 "aud": "your api stitch id", "sub": "your user custom api key"
         var header = {            alg: "HS256",            typ: "JWT"          }          var payload = {            "aud": "your api stitch id",            "sub": "your user custom api key",            name : "name",            pass : "pass",          }           var options = {            expiresIn : (60 * 60 * 24).toString(), // ONE DAY TO EXPIRATION            algorithm: 'HS256'          } 

Later:

var token = jwt.sign(payload, Buffer.from("your user custom api key"), options);credential = new CustomCredential(token);stitchclient.auth.loginWithCredential(credential).then(authedUser => console.log(`logged in with custom auth as user ${authedUser.id}`)).catch( err => console.error(`failed to log in with custom auth: ${err}`))


To be more specific, if you are for example generating the token from a Realm function, you will get the user id from the context :

exports = function(userMail) {  const jwt = require('jsonwebtoken')  const KEY_JWT = 'MY_SECRET_KEY'  const token = jwt.sign({                  email: userMail,                   sub: context.user.id,                  aud: "<application_id>"                },                  KEY_JWT,                {                  expiresIn: "1h"                }            )  return token}