mulesoft-labs / js-client-oauth2

A JavaScript implementation of an oauth2 client, for inclusion in the JavaScript client generator for APIs described with RAML.
Other
539 stars 146 forks source link

How to use js-client-oauth2 with a browser (how to browserify) #71

Open ftarlao opened 6 years ago

ftarlao commented 6 years ago

I have understood that your library can be employed to implement an oauth2 client running in a browser (user-agent) but I was not able to create a standalone bundle.js with browserify.

I have tried various ways, e.g.:

browserify -r 'safe-buffer' -r querystring -r 'url' -r ./request/browser.js:'./request' > bundle.js
browserify -r 'safe-buffer' -r querystring -r 'url' -r ./request/browser.js:'request' > bundle.js

the bundle.js is created, and I copied the project src folder with the generated bundle.js in the asset subfolder of my Js web application.

When I try to load the bare-metal test page, in my Firefox browser it fails, the test page:

<html>
    <head>
        <title>OAuth Demo</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="assets/js/bundle.js"></script>
        <script src="assets/js/client-oauth2.js"></script>
    </head>
    <body>
        <div>OAuth Demo</div>
    </body>
</html>

The error in the browser console:

ReferenceError: module is not defined[Ulteriori informazioni] client-oauth2.js:11:1
    <anonimo> http://localhost:8888/assets/js/client-oauth2.js:11:1

What is the right browserfy commandline to make js-client-oauth2 to work in the browser and what resources should I add to my "assets" folder? Do the bundle.js and the src folder suffice?

I'm quite incompetent with Javascript and web applications.. perhaps the problem solution looks evident, but I suppose that a step-by-step guide about the 'use it in the browser' case, may be a useful addition to your Project readme. Regards

Edit: browserify was pointed out as a "tool for the job" in the closed issues

evert commented 6 years ago

I'm just a bystander, and I don't know if this helps, but I have working version of js-client-oauth2 integrated in my own package, packaged by webpack.

So maybe my configuration helps:

https://github.com/evert/ketting

ftarlao commented 6 years ago

Thanks for your time, but I have decided to use another lib, may be useful for other guys for sure, Regards