My application is in Heroku, it’s developed in Angularjs (1.6.4), I have some varibles in Heroku, I can access them through the server, I made an API to return the varibles by means of a request with the GET method, everything that works , the problem is when I call the service from the app.js, which is where ‘angularAuth0Provider’ is initialized, my question is where can I initialize this method in such a way that before initializing it?, I will first make an HTTP request to the server to return the data and do not change them directly in the code, but according to the internet you can’t use services in APP.JS, they say that you do it in APP.RUN.JS but I need that data to be able to get to that file, if you don’t have them Error initializing the application.
CODE OF APP.JS
(function() {
“use strict”;
angular
.module(“app”,
“auth0.auth0”,
“ui.router”,
“ngMaterial”,
“ngMessages”,
“datePicker”,
“angular-loading-bar”,
“ngStorage”
])
.config(config);
config.$inject =
“$stateProvider”,
“$locationProvider”,
“$urlRouterProvider”,
“angularAuth0Provider”
];
function config(
$stateProvider,
$locationProvider,
$urlRouterProvider,
angularAuth0Provider
) {
$stateProvider
.state(“home”, {
url: “/”,
controller: “HomeController”,
templateUrl: “app/views/project/home.html”,
controllerAs: “vm”
})
.state(“worklogs”, {
url: “/work-log”,
controller: “WorkLogController”,
name: “worklog”,
templateUrl: “app/views/project/work-log/work-log.html”,
controllerAs: “vm”
})
.state(“callback”, {
url: “/callback”,
controller: “CallbackController”,
templateUrl: “app/callback/callback.html”,
controllerAs: “vm”
});
I WANT TO DO THE REQUEST HERE
THEN…
// Initialization for the angular-auth0 library WITH RESULTS OF THE REQUEST
angularAuth0Provider.init({
clientID: “----------”,
domain: “-----------------”,
responseType: “token id_token”,
audience: “--------------”,
redirectUri: “----------------”,
scope: “openid”
});
$urlRouterProvider.otherwise(“/”);
$locationProvider.hashPrefix(“”);
/// Comment out the line below to run the app
// without HTML5 mode (will use hashes in routes)
$locationProvider.html5Mode(true);
}
})();
CODE OF SERVER.JS
const express = require(“express”);
const app = express();
const path = require(“path”);
const PORT = process.env.PORT || 5000;
const nforce = require(“nforce”);
const http = require(“http”);
var localStorage = require(“localStorage”);
var cors = require(“cors”);
var oauth;
var token = “”;
var CLIENT_ID = process.env.CLIENT_ID;
var CLIENT_SECRET = process.env.CLIENT_SECRET;
var USERNAME = process.env.USERNAME;
var PASSWORD = process.env.PASSWORD;
var BASEURL = process.env.BASEURL;
var SECURITYTOKEN = process.env.SECURITYTOKEN;
var REDIRECT_URL = process.env.REDIRECT_URL;
var audienceauth0 = process.env.audienceauth0;
var clientIDauth0 = process.env.clientIDauth0;
var domainauth0 = process.env.domainauth0;
var redirectUriauth0 = process.env.redirectUriauth0;
var responseTypeauth0 = process.env.responseTypeauth0;
var scopeauth0 = process.env.scopeauth0;
var MODE = “”,
ENVIRONMENT = “”,
APIVERSION = “”;
const server = app.listen(PORT, () => console.log(Listening on ${PORT}
));
if (process.env.MODE != null) {
MODE = process.env.MODE;
}
if (process.env.ENVIRONMENT != null) {
ENVIRONMENT = process.env.ENVIRONMENT;
}
if (process.env.APIVERSION != null) {
APIVERSION = process.env.APIVERSION;
}
const org = nforce.createConnection({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
redirectUri: REDIRECT_URL,
apiVersion: APIVERSION, // optional, defaults to current salesforce API version
environment: ENVIRONMENT, // optional, salesforce ‘sandbox’ or ‘production’, production default
mode: MODE, // optional, ‘single’ or ‘multi’ user mode, multi default
username: USERNAME,
password: PASSWORD,
securityToken: SECURITYTOKEN
});
app.use(“/”, express.static(__dirname + “/”));
app.get(“/”, function(req, res) {
res.sendFile(path.join(__dirname + “/index.html”));
});
app.get(“/callback”, function(req, res) {
res.sendFile(path.join(__dirname + “/index.html”));
});
app.get(“/work-log”, function(req, res) {
res.sendFile(path.join(__dirname + “/index.html”));
});
app.get(“/api/salesforce/baseurl”, function(req, res) {
return res.json({ BASEURL: BASEURL });
});
API
app.get(“/api/auth0/data”, function(req, res) {
return res.json({
audienceauth0: audienceauth0,
clientIDauth0: clientIDauth0,
domainauth0: domainauth0,
redirectUriauth0: redirectUriauth0,
responseTypeauth0: responseTypeauth0,
scopeauth0: scopeauth0
});
});
app.get(“/api/salesforce/token”, function(req, res) {
org.authenticate({}, function(err, resp) {
if (!err) {
oauth = resp;
token = resp.access_token;
} else {
console.log(err);
}
});
return res.json({ TOKEN: token });
});