Need some links explaining getting and setting esri portal user credentials using ArcGIS Javascript API
Here are a couple of guides that might help you.
Working with the ArcGIS Platform | ArcGIS API for JavaScript 4.14
If you learn best with samples like I do, you can download a working example here (it is included in the doc Rene provided as well)