UNPKG

1.66 kBJavaScriptView Raw
1const fs = require( 'fs' );
2const config = require( './config' );
3
4const samlIssuer = config.passport.saml.issuer;
5
6const filePre = '\n'
7 // + 'let authenticatedUser = {};\n'
8 + '\n'
9 + 'const fetchPromise = ( route, options ) => new Promise( ( resolve, reject ) => {\n'
10 + '\n'
11 + '\ttry {\n'
12 + '\t\tfetch( route, options ) //eslint-disable-line\n'
13 + '\t\t\t.then( resolve )\n'
14 + '\t\t\t.catch( reject );\n'
15 + '\t}\n'
16 + '\n'
17 + '\tcatch ( e ) {\n'
18 + '\t\treject( e );\n'
19 + '\t}\n'
20 + '\n'
21 + '} );\n'
22 + '\n'
23 + '\n'
24 + 'const getUser = () => {\n'
25 + '\n'
26 + '\treturn new Promise ( ( res, rej ) => {'
27 + '\t\tfetchPromise(';
28
29const filePost = '\', {\n'
30 + '\t\t\theaders : {\n'
31 + '\t\t\t\t\'Content-Type\' : \'application/json\',\n'
32 + '\t\t\t},\n'
33 + '\t\t\tmethod : \'GET\',\n'
34 + '\t\t\tcredentials : \'include\'\n'
35 + '\t\t}\n'
36 + '\t)\n'
37 + '\t\t.then( ( response ) => {\n'
38 + '\t\t\tresponse.json().then( ( json ) => {\n'
39 + '\n'
40 + '\t\t\t\tif ( json.success && json.redirect ) {\n'
41 + '\n'
42 + '\t\t\t\t\twindow.location.href = json.redirectTo;\n'
43 + '\n'
44 + '\t\t\t\t} else {\n'
45 + '\t\t\t\t\treturn json\n'
46 + '\t\t\t\t}\n'
47 + '\t\t\t} )\n'
48 + '\t\t\t.then( ( user ) => {\n'
49 // + '\t\t\t\tauthenticatedUser = user;\n'
50 + '\t\t\t\tconsole.log( user );\n'
51 + '\t\t\t\tres ( user );\n'
52 + '\t\t\t} );\n'
53 + '\n'
54 + '\t\t} );\n'
55 + '\n\t} );'
56 + '\n'
57 + '};\n'
58 + '\n';
59// + 'getUser();';
60
61const contents = `${filePre}'${samlIssuer}/saml/get-profile${filePost}`;
62
63const fileInit = () => {
64 fs.writeFile( `${__dirname}/../public/js/musd-saml.js`, contents, ( err ) => {
65 if ( err ) throw err;
66 } );
67};
68
69module.exports = {
70 fileInit
71};