How to hook user login?

Hello everyone,
I have a problem with my Wix site.
I want to hook user’s login action and extract specific query string from URL and redirect user to some page depends on query string.
I tried to utilize wixUsers.onLogin but it seemed doesn’t fire login event when user enters internal page.
I tried to place the code snippet in my several pages.

Here is the code that I used.

import wixUser from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function () {
    wixUser.onLogin( (user) => {
         let userId = user.id;           
         let isLoggedIn = user.loggedIn; 
         let userRole = user.role;      
         console.log(userId);

         if (isLoggedIn) {
         let serialNumber = wixLocation.query.serial;
            console.log(serialNumber);
         }
    });
}

Please help me how I can hook user’s login action and redirect user depends on query string.
Thanks!

Using onLogin will only happen after the user has successfully logged themselves in.

If you are wanting something to have the login window show, then simply use the promptLogin call instead.
https://www.wix.com/corvid/reference/wix-users.html#promptLogin

1 Like

Thanks! It worked and I just tested it showed console.log on browser.
I appreciate for your help.

Hi @givemeawhisky
Unfortunately, I realized that the promptLogin() was executed always whenever user refreshes page.
I wanted to hook the moment when user logs in.
Can you advise me how I can hook user’s login action?

Any updates on this ticket?

You can try using the data hooks of beforeInsert or afterInsert.
https://www.wix.com/corvid/reference/wix-data.Hooks.html
https://support.wix.com/en/article/corvid-about-data-hooks

Or try to do a onAfterSave or a onBeforeSave on the actual dataset itself.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html
https://www.wix.com/corvid/reference/wix-dataset.DynamicDataset.html

Thanks for your answer.
However I think there is some misunderstanding on what I wanted to resolve. Sorry about that.
I want to trigger some action right after user logs in to the site.
But I can’t hook the login action.
onLogin() callback didn’t work even though I tried as the documentation.

Anyone who has experience to hook user login action, please help me advise. Thanks!

Any solution for this issue? I too stuck in that condition. I need to show a light box when an user login .

Just add your own login lightbox and use close lightbox to close your login lightbox and then use open lightbox to open your required lightbox straight after.
https://www.wix.com/corvid/reference/wix-window.lightbox.html

Old thread being closed.