Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Commit b3f78d5

Browse files
committed
fix(oauth): make it work + add example
1 parent bcd45a7 commit b3f78d5

File tree

5 files changed

+112
-9
lines changed

5 files changed

+112
-9
lines changed

examples/oauth/example.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(function() {
2+
angular.module('example', ['osm.oauth'])
3+
.config(config)
4+
.controller('ExampleCtrl', ExampleCtrl);
5+
function config(osmAuthServiceProvider) {
6+
osmAuthServiceProvider.options = {
7+
oauth_consumer_key: 'Y4mIaH1rx9qqjYFo9mjDEc7rArpP7rFkj1hLl3Mj',
8+
oauth_secret: 'EkXrocMrHbtSQ3r9VH0D7KH6oAEhfJ6elImVRBzB'
9+
};
10+
}
11+
function ExampleCtrl (osmAuthService) {
12+
var $ctrl = this;
13+
this.options = {};
14+
this.login = function () {
15+
//osmAuthService.options(this.options);
16+
osmAuthService.authenticate().then(update);
17+
};
18+
function onUserDetails(res) {
19+
$ctrl.data = res;
20+
}
21+
function onError(err) {
22+
$ctrl.err = err;
23+
}
24+
function update() {
25+
if (osmAuthService.authenticated()) {
26+
$ctrl.authenticated = true;
27+
osmAuthService.xhr({
28+
method: 'GET',
29+
path: '/api/0.6/user/details'
30+
}).then(onUserDetails, onError);
31+
} else {
32+
$ctrl.authenticated = false;
33+
}
34+
}
35+
}
36+
37+
})();

examples/oauth/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>angular-osm oauth</title>
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
7+
<script type="text/javascript" src="../../dist/dependencies.js"></script>
8+
<script type="text/javascript" src="../../dist/osm.js"></script>
9+
<script type="text/javascript" src="example.js"></script>
10+
</head>
11+
<body ng-app="example" ng-controller="ExampleCtrl as $ctrl">
12+
<div class="container">
13+
<div class="row">
14+
<div class="col-md-6">
15+
<p>This example show you how to use osmOAuthService</p>
16+
<button ng-click="$ctrl.login()">login</button>
17+
</div>
18+
<div class="col-md-6">
19+
<span ng-show="$ctrl.loading" class="ng-hide">loading...</span>
20+
<pre ng-show="$ctrl.data">{{$ctrl.data | json}}</pre>
21+
<div class="alert alert-danger ng-hide" ng-show="$ctrl.error">
22+
{{$ctrl.error}}
23+
</div>
24+
</div>
25+
26+
</div>
27+
</div>
28+
</body>
29+
</html>

examples/oauth/land.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html><head></head>
3+
<body>
4+
<script>
5+
opener.authComplete(window.location.href);
6+
window.close();
7+
</script>
8+
</body>
9+
</html>

src/oauth/oauth.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ var osmOAuthModule = angular.module('osm.oauth', [])
44
.factory('osmAuthService', osmAuthService)
55
.provider('osmAuthService', function osmAuthServiceProvider () {
66
this.options = {};
7-
this.$get = function osmAuthServiceFactory() {
8-
return new osmAuthService(this.options);
7+
8+
this.$get = function osmAuthServiceFactory($q) {
9+
return new osmAuthService($q, this.options);
910
};
11+
this.$get.$inject = ['$q'];
1012
});
1113

1214
export default osmOAuthModule;

src/oauth/oauth.service.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,43 @@ import osmAuth from 'osm-auth';
66
* @requires angular-osm.osmSettingsService
77
* @description handle osm oauth
88
*/
9-
function osmAuthService(options) {
9+
function osmAuthService($q, options) {
1010
if (options) {
1111
if (options.oauth_secret && options.oauth_consumer_key) {
12-
osmAuth(options);
12+
this.auth = osmAuth(options);
1313
}
1414
}
15-
this.logout = osmAuth.logout;
16-
this.authenticated = osmAuth.authenticated;
17-
this.authenticate = osmAuth.authenticate;
18-
this.xhr = osmAuth.xhr;
19-
this.options = osmAuth.options;
15+
this.logout = function () {
16+
return this.auth.logout();
17+
};
18+
this.authenticated = function () {
19+
return this.auth.authenticated();
20+
};
21+
this.authenticate = function () {
22+
var deferred = $q.defer();
23+
this.auth.authenticate(function () {
24+
deferred.resolve(true);
25+
});
26+
return deferred.promise;
27+
};
28+
this.xhr = function (options) {
29+
var deferred = $q.defer();
30+
this.auth.xhr(options, function (err, data) {
31+
if (err) {
32+
deferred.reject(err);
33+
} else {
34+
deferred.resolve(data);
35+
}
36+
});
37+
return deferred.promise;
38+
};
39+
this.options = function (options) {
40+
if (this.auth) {
41+
this.auth.options(options);
42+
} else {
43+
this.auth = osmAuth(options);
44+
}
45+
};
2046
}
2147

2248
export default osmAuthService;

0 commit comments

Comments
 (0)