Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(sessionInfo => {
console.log(sessionInfo);
const newSession = new UserSession(session);
// if we are in a browser (client side) we are working with an instance
// of `UserSession` otherwise (server side) we are working with a JSON
// representation of a session.
if (process.browser) {
this.store.set({ session: newSession });
// if we are in a browser we should also update IdentityManager
loadModules(["esri/identity/IdentityManager"]).then(([esriId]) => {
esriId.registerToken(session.toCredential());
});
} else {
this.store.set({ session: sessionInfo });
}
return error.retry(newSession, 1);
preload({ params }) {
const { session } = this.store.get();
// redirect to the homepage unless the user is signed in
if (!session) {
this.redirect(302, "/");
return;
}
const { webmapId } = params;
// if we are on the server we won't have an actuall UserSession just a
// JSON representation of one. So we need to hydrate a UserSession.
const userSession = process.browser ? session : new UserSession(session);
// next we can request both the webmap item and the webmap data (the JSON)
// and process the data to get a list of layers.
return Promise.all([
getItem(webmapId, {
authentication: userSession
}).catch(error => {
return retryWithNewSession(error, this.fetch);
}),
getItemData(webmapId, {
authentication: userSession
}).catch(error => {
return retryWithNewSession(error, this.fetch);
})
]).then(([item, webmap]) => {
const layers = webmap.operationalLayers
store: data => {
// `data` is whatever was in the Store on the server side.
// if we have a session we can create a new UserSession that we can
// use with ArcGIS REST JS on the client.
return new Store({
session: data && data.session ? new UserSession(data.session) : null,
user: data.user,
org: data.org
});
}
});
.then(({ username, password }) => {
session = new UserSession({
username,
password
});
// this will generate a token and use it to get into about a user
return session.getUser();
})
.then(self => {
preload() {
const { session } = this.store.get();
// redirect to the homepage unless the user is signed in
if (!session) {
this.redirect(302, "/");
return;
}
// if we are on the server we won't have an actuall UserSession just a
// JSON representation of one. So we need to hydrate a UserSession.
const userSession = process.browser ? session : new UserSession(session);
// now we can search for webmaps. Sapper will wait until this promise
// resolves before rendering the page.
return (
searchItems({
q: `owner:${session.username} type:"Web Map"`,
num: 100,
authentication: userSession
})
// if there is an error we can retry the request with a fresh session
// from /auth/exchange-token
.catch(error => {
return retryWithNewSession(error, this.fetch);
})
// then we can process out response.
.then(response => {
export function createRuntimeMockUserSession (
now: number
): UserSession {
const tomorrow = new Date(now + 86400000);
return new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "fake-token",
tokenExpires: tomorrow,
refreshToken: "refreshToken",
refreshTokenExpires: tomorrow,
refreshTokenTTL: 1440,
username: "casey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
});
}