Skip to content

Commit

Permalink
Use the actual window.location to build a redirect_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
bhousel committed Apr 27, 2022
1 parent a0ba5fe commit 74a9101
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
37 changes: 5 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ Register a new OAuth2.0 application on openstreetmap.org:
### Example

```js
var redirectPath = window.location.origin + window.location.pathname;
var auth = osmAuth({
client_id: "JWXSAzNp64sIRMStTnkhMRaMxSR964V4sFgn3KUZNTA",
client_secret: "6umOXfkZqH5CVUtv6iDqN7k8o7mKbQvTrHvbDQH36hs",
redirect_uri: "http://127.0.0.1:8080/land.html",
redirect_uri: redirectPath + "land.html",
scope: "read_prefs",
auto: true // show a login form if the user is not authenticated and you try to do a call
});
Expand All @@ -114,36 +115,7 @@ document.getElementById("authenticate").onclick = function () {
```

### Example with single-page

```js
var auth = osmAuth({
client_id: "JWXSAzNp64sIRMStTnkhMRaMxSR964V4sFgn3KUZNTA",
client_secret: "6umOXfkZqH5CVUtv6iDqN7k8o7mKbQvTrHvbDQH36hs",
redirect_uri: "http://127.0.0.1:8080/land.html",
scope: "read_prefs",
auto: true,
singlepage: true, // Load the auth-window in the current window, with a redirect,
landing: window.location.href // Come back to the current page
});

var urlParams = new URLSearchParams(window.location.search);
if(urlParams.has('code')){
// The authorization code passed via the URL has to be passed into 'auth.bootstrapToken'.
// The callback is triggered when the final roundtrip is done
auth.bootstrapToken(urlParams.get('code'), (error) => {
if(error !== null){
console.log("Something is wrong: ", error);
return;
}
/* Do authenticated stuff here*/
}, this.auth);
} else {
// Attempt to do something authenticated to trigger authentication
}

```


coming soon

# API

Expand All @@ -153,10 +125,11 @@ Constructs an `osmAuth` instance.<br/>
At a minimum, `options` must contain OAuth2 client ID, secret, redirect URI, and scope(s):

```js
var redirectPath = window.location.origin + window.location.pathname;
{
client_id: "JWXSAzNp64sIRMStTnkhMRaMxSR964V4sFgn3KUZNTA",
client_secret: "6umOXfkZqH5CVUtv6iDqN7k8o7mKbQvTrHvbDQH36hs",
redirect_uri: "http://127.0.0.1:8080/land.html",
redirect_uri: redirectPath + "land.html",
scope: "read_prefs"
}
```
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ <h2 id="id"></h2>
</p>
<script src="dist/osm-auth.iife.js"></script>
<script>
var redirectPath = window.location.origin + window.location.pathname;
var auth = osmAuth.osmAuth({
client_id: "JWXSAzNp64sIRMStTnkhMRaMxSR964V4sFgn3KUZNTA",
client_secret: "6umOXfkZqH5CVUtv6iDqN7k8o7mKbQvTrHvbDQH36hs",
scope: "read_prefs",
redirect_uri: "http://127.0.0.1:8080/land.html"
redirect_uri: redirectPath + "land.html"
});

function done(err, res) {
Expand Down
8 changes: 4 additions & 4 deletions src/osm-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ declare namespace OSMAuth {
}

interface OSMAuthOptions {
scope?: string;
client_id?: string;
client_secret?: string;
redirect_uri?: string;
scope: string;
client_id: string;
client_secret: string;
redirect_uri: string;
access_token?: string;
url?: string;
auto?: boolean;
Expand Down

0 comments on commit 74a9101

Please sign in to comment.