Is it possible to programatically log in a user?
# suitecommerce
c
Is it possible to programatically log in a user?
s
Fun idea. Yes
Copy code
var url = SC.ENVIRONMENT.baseUrl.replace('{{file}}','services/Account.Login.Service.ss');
var data = {   
     email: '<mailto:example@example.com|example@example.com>', // put the login email here
     password: 'password', // put the password here
     redirect: 'true'
};
jQuery.ajax({
    url: url,
    data: JSON.stringify(data),
    type: 'post'
})
.done(function () {
    window.location.reload()
})
c
Wow, that's succinct and useful. Thank you, @Steve Goldberg. 🙏🏻
s
😊
This is just a proof of concept code
I think you should still use the Account modules to do this sort of thing
And obviously not reference
SC
in your code
👍🏻 1
m
I attempted to use something similar to this to logout and then redirect user. Unfortunately it didn't logout the user, but it did redirect them. Can you see something I missed in this?
Copy code
jQuery.ajax({
    url: Utils.getAbsoluteUrl(environmentComponent.getSiteSetting("touchpoints.logout"))
}).always(() => {
    window.location = suitelet_url;
});
I can see that it calls the correct url but the return code is 303.
When I logout using the menu I can see that 4 url's are hit.
Ah I missed that it redirected to logOut.ssp?logoff=T and then fails there with the following error.
Copy code
Javascript is disabled on your browser.
To view this site, you must enable JavaScript or upgrade to a JavaScript-capable browser.
s
I mean, something like this is the simplest way to log out:
Copy code
window.location = SC.ENVIRONMENT.siteSettings.touchpoints.logout
If you’re trying to do it with SuiteScript, I’ve already documented (what I think) is the best way: https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_164383450200.html#Redirect-Users-with-SuiteScript