Source

tools/Authenticate.js

// ///@ts-check
// /// <reference path="../../infohub/js/cn_scope.js" />
/// <reference path="./RestServices.js" />

'use strict'

import { RestServices } from "./RestServices.js"

// console.log("CN_AUTH LOADED.")

/**
 *      
 *  @namespace
 * 
 */
export class AuthenticationServices  {
         
    #_auth_target_ = null

    constructor( auth_target = null ) {

        // console.log("********************************")
        
        if (auth_target == null) {
            let url = window.location // .href
            this.#_auth_target_ = new URL( url.protocol + url.host + "/cn_login" )
        } else {
            this.#_auth_target_ = new URL ( auth_target )
        }                   

    }

    static async login_IAM( userid , passwd ) {
        
        let token = passwd

        let reqbody = {
            'userid'    : userid,
            'password'  : passwd
        }

        const resp = await RestServices.makeApiCall( null , RestServices.METHODS.POST , "https://app.colneo.services/iam/tokens" , reqbody )
        // resp.dump("RESPONSE:")

        return {
            'status': 200,
            'data': {
                'userid': userid,
                'token': resp.data.token
            }
        }

    }

    /**
     * 
     *  Login to infohub.
     * 
     *  Use webpage ./cn_login which writes to
     *  local storage variables 'user' and 'token'.
     * 
     * 
     *  @param {*} cb data : {
     *      'userid' : " ... ",
     *      'token'  : " ... "
     *  }
     * 
     *  @returns 
     */
    // async login( cb ) 
    // {

    //     console.log( `auth : ${this.#_auth_target_}`)

    //     //
    //     // check if user and token are stored in local storage
    //     // if so, return user/token, otherwise forwarrd to
    //     // webpage ./cn_login

    //     const u = window.localStorage.getItem('user')
    //     const t = window.localStorage.getItem('token')

    //     if (t == undefined) {
            
    //         let queryString = window.location.search;
    //         let urlParams = new URLSearchParams(queryString);

    //         urlParams.set('redirect', window.location) // window.location.href ???

    //         if (typeof _default_userid_ != 'undefined' ) {
    //             urlParams.set('userid', _default_userid_) // window.location.href ???
    //         }

    //         let target = this.#_auth_target_
            
    //         target.search = urlParams.toString()
            
    //         window.location = target

    //     } else {
    //         return cb({
    //             'status' : 200,
    //             'data' : {
    //                 'userid'    : u,
    //                 'token'     : t
    //             }
    //         })
    //     }

    // }

    /**
     * 
     *  Remove varables 'user' and 'token' from local strorage.
     * 
     */
    // static async logout ()
    // {    
    //     window.localStorage.removeItem('token')
    //     window.localStorage.removeItem('user')
    //     window.location.reload()
    // }      

}