wixUsers.generateSessionToken BUG?

Hello,

The function was working well until 4 or 5 days ago, I’ve realized that the site members was not able to login, I use a third party authentication and the login is fulfilled by the backend function wixUsers.generateSessionToken(email) , and wixUsers.applySessionToken(token) on the Frontend
I recreated it on a new wix site with a minimum code to show it:

/*******************************
* Backend code - wixTools.jsw  *
*******************************/
import wixUsers from 'wix-users-backend';
export function getLoginToken(email) {
    return wixUsers.generateSessionToken(email)
    .then((sessionToken) => {
        console.log("User session token is "+sessionToken);
        return { "sessionToken": sessionToken, "approved": true };
    }).catch(error => {
        console.log("Backend getLoginToken Crash: " + error);
        return { "approved": false, "error":error };
    });
}
/******************************* 
* Frontend code - NewPage      *
*******************************/
import wixUsers from 'wix-users';
import { getLoginToken } from 'backend/wixTools'
$w.onReady(function () {
    getLoginToken("demo@demo.com").then(result => {
        if (result.approved) {
            console.log("User sessionToken is: " + result.sessionToken);
            wixUsers.applySessionToken(result.sessionToken)
            .then(() => {
                console.log("User logged in.");
            });
        }
    })
});

There is no difference if the user (which already exist) status is Active (approved) or Applicant (pending) the function “generateSessionToken()” from “wix-users-backend” is always crashing like that (in both web sites I tested, production and test site):

The backend function"wixUsers.approveByEmail(email)" to approve pending members is also failing, Is this a bug?

If so could you please take a look at that, at the moment the users are not able to login and register on my site and it’s a l little bit painful.

Thanks and Regards

Firstly, only test any code that uses Wix Users on your live published website.
The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

Can you list the code that you are actually using on your website or link to the website in question (your main one and not the testing one with the above code sample from it, thanks).

Have you tried changing these few lines from this.

return { "sessionToken": sessionToken, "approved": true };
    }).catch(error => {
        console.log("Backend getLoginToken Crash: " + error);
        return { "approved": false, "error":error };

To this.

return {sessionToken, "approved": true};
    });
    .catch( (error => {
        console.log("Backend getLoginToken Crash: " + error);
        return {"approved": false, "error": error};

As you mention that it has been working fine until a few days ago, I have linked this forum post to higher status so that they can check if there is a bug in Wix Users.

In the meanwhile, can you try simply testing your third party authentication with the basic code examples given by Wix in the Wix Users API Reference.

For approve by email.
Approve a pending member using an email address
This example contains a backend function which approves a pending user using an email address and returns a session token to be used in client-side code to login the user who was just approved.

import wixUsers from 'wix-users-backend';

export function myBackendFunction(email) {
  return wixUsers.approveByEmail(email)
    .then( (sessionToken) => {
      return {sessionToken, "approved": true};
    } );
    .catch( (error) => {
      return {"approved": false, "reason": error};
    } );
  }
}

applySessionToken( )
Logs the current user into the site using the given session token.
Log in the current user by applying a session token

import wixUsers from 'wix-users';

// ...

wixUsers.applySessionToken(sessionToken)
  .then( () => {
    console.log("User logged in.");
  } );

For generate session token.
Log a user in after 3rd party authentication
This example contains a backend function which uses a 3rd party authentication service to authenticate a user. If the authentication is successful, a session session token is returned to the client-side and used to log in the authenticated user.

/*******************************
 * backend code - login.jsw *
 *******************************/
import wixUsers from 'wix-users-backend';
import {authBy3rdParty} from 'backend/authentications';

export function getLoginToken(email, password) {
  // authenticate using 3rd party
  return authBy3rdParty(email, password)
    .then( (isAuthenticated) => {
      // if authenticated generate and return session token
      if(isAuthenticated){
        return wixUsers.generateSessionToken(email)
          .then( (sessionToken) => {
            return {"sessionToken": sessionToken, "approved": true};
          } );
      }
      // if not authenticated return non-approval
      return {"approved": false};
    } );
}

/*********************************
 * client-side login code *
 *********************************/
import {getLoginToken} from 'backend/login';
import wixUsers from 'wix-users';

export async function button_onClick(event) {
  // call backend function
  getLoginToken($w("#email").value, $w("#password").value)
    .then( (loginResult) => {
      // if approved log the user in with the session token
      if (loginResult.approved) {
        wixUsers.applySessionToken(loginResult.sessionToken);
      }
      // if not approved log a message
      else {
        console.log("User not approved.");
      }
    } );
}

Hello GOS,

Thanks for your answer, the third party authorization is working fine, I tried to change:

return { "sessionToken": sessionToken, "approved": true };

by:

return {sessionToken, "approved": true};

but it crashes anyway, the crash doesn’t look like if it would have any relationship to Preview/Published, so published site is crashing too, I simplified it, to isolate the crash and show it in the stack trace on the published site:

/*******************************
* Backend code - wixTools.jsw *
*******************************/
import wixUsers from 'wix-users-backend';
export async function getLoginToken(email) {
    return await wixUsers.generateSessionToken(email)
}
/******************************* 
* Frontend code - NewPage      *
*******************************/
import wixUsers from 'wix-users';
import { getLoginToken } from 'backend/wixTools'
$w.onReady(function () {});
export function buttonLogin_click(event) {
    let userEmail = $w('#inputUserEmail').value
    $w('#textLog').text = "Generate session token for: " + userEmail
    getLoginToken(userEmail).then(result => {
        if (result.approved) {
            $w('#textLog').text +="\nSession Token is:"+result.sessionToken              
            wixUsers.applySessionToken(result.sessionToken).then(() => {
            $w('#textLog').text += "\nUser logged in."
        }).catch(exceptionApplyToken => {
            $w('#textLog').text +="\nCatch apply token: "+ exceptionApplyToken
        });
    } else {
        $w('#textLog').text += "\nNot Approved: "
    }
    }).catch(exceptionGenerateToken => {
        $w('#textLog').text+="\nCatch generate token: "+exceptionGenerateToken
    })
}

The result is the same here a screenshot of the published site: https://enricdelmolino.wixsite.com/misitio/testing

I wouldn’t like to post the production site here, can I send you the link by pm? anyway the crash is exactly the same thrown by wixUsers.generateSessionToken from the backend

Thanks

I faced with the same problem. I tried many implementation, but each time it throws the same error.

Helllo,

I just tested right now (same code) and it’s working fine on both sites (wixUsers.generateSessionToken(email) and wixUsers.approveByEmail(email) functions) for Preview and published site, so thanks to fix it!

Enric

Hi @enricdelmolino ,

As you can see - https://www.wix.com/corvid/forum/community-discussion/error-on-generatesessiontoken issue has been resolved on our end.

Thanks.