Skip to content

Commit 137b986

Browse files
authored
Merge pull request #166 from teamsnap/browser-logout
Add method for ending browserAuth session
2 parents 0ded859 + 62b38cc commit 137b986

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# TeamSnap JavaScript SDK CHANGELOG
22

3+
### March 20, 2017 // Version 1.24.0
4+
- Add `browserLogout` method to end a session started with `startBrowserAuth`.
5+
36
### February 27, 2017 // Version 1.23.2
47
- Bug fix that adds `sendInvites` to `importMembersFromTeam` method in persistence layer.
58

lib/teamsnap.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ var require = global.require;
120120
global = this;
121121
if (global.window) window = this;
122122
require.register("auth.coffee", function(exports, require, module) {
123-
var TeamSnap, authRequest, browserStorageName, browserStore, collectionJSONMime, createAuthDialog, jsonMime, multipartMime, promises, request, sdkRequest;
123+
var TeamSnap, authRequest, browserStorageName, browserStore, collectionJSONMime, createAuthDialog, generateUrl, jsonMime, multipartMime, promises, request, sdkRequest;
124124

125125
TeamSnap = require('./teamsnap').TeamSnap;
126126

@@ -152,6 +152,22 @@ sdkRequest = request.create().hook(function(xhr, data) {
152152
return xhr.withCredentials = true;
153153
});
154154

155+
generateUrl = function(endpoint, params) {
156+
var key, queries, url, value;
157+
queries = [];
158+
for (key in params) {
159+
value = params[key];
160+
if (value) {
161+
queries.push(key + '=' + encodeURIComponent(value));
162+
}
163+
}
164+
url = teamsnap.authUrl + '/oauth/' + endpoint;
165+
if (queries.length) {
166+
url = url + '?' + queries.join('&');
167+
}
168+
return url.replace(/%20/g, '+');
169+
};
170+
155171
TeamSnap.prototype.auth = function(token) {
156172
var cachedCollections, callback;
157173
if (typeof token === 'function') {
@@ -197,20 +213,15 @@ TeamSnap.prototype.hasSession = function() {
197213
return !!browserStore();
198214
};
199215

216+
TeamSnap.prototype.browserLogout = function() {
217+
createAuthDialog(teamsnap.authUrl + '/logout');
218+
if (this.isAuthed) {
219+
return this.deleteAuth();
220+
}
221+
};
222+
200223
TeamSnap.prototype.init = function(clientId, secret) {
201-
var generateAuthUrl, generatePasswordUrl, generateTokenUrl, generateUrl;
202-
generateUrl = function(endpoint, params) {
203-
var key, queries, url, value;
204-
queries = [];
205-
for (key in params) {
206-
value = params[key];
207-
if (value) {
208-
queries.push(key + '=' + encodeURIComponent(value));
209-
}
210-
}
211-
url = teamsnap.authUrl + '/oauth/' + endpoint + '?' + queries.join('&');
212-
return url.replace(/%20/g, '+');
213-
};
224+
var generateAuthUrl, generatePasswordUrl, generateTokenUrl;
214225
generateAuthUrl = function(type, redirect, scopes) {
215226
scopes = Array.isArray(scopes) ? scopes.join(' ') : scopes;
216227
return generateUrl('authorize', {
@@ -6655,7 +6666,7 @@ ref = require('./model'), Collection = ref.Collection, Item = ref.Item;
66556666
require('./errors');
66566667

66576668
TeamSnap = (function() {
6658-
TeamSnap.prototype.version = '1.23.2';
6669+
TeamSnap.prototype.version = '1.24.0';
66596670

66606671
TeamSnap.prototype.promises = promises;
66616672

lib/teamsnap.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "teamsnap.js",
3-
"version": "1.23.2",
3+
"version": "1.24.0",
44
"engines": {
55
"node": "^7.0.0",
66
"npm": "^3.0.0"

src/auth.coffee

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ sdkRequest = request.create().hook (xhr, data) ->
1919
xhr.setRequestHeader('Content-Type', collectionJSONMime)
2020
xhr.withCredentials = true
2121

22+
# Generates urls
23+
generateUrl = (endpoint, params) ->
24+
queries = []
25+
for key, value of params
26+
if value
27+
queries.push key + '=' + encodeURIComponent value
28+
url = teamsnap.authUrl + '/oauth/' + endpoint
29+
30+
if queries.length
31+
url = url + '?' + queries.join('&')
32+
33+
url.replace /%20/g, '+'
34+
2235

2336
# Need 3 types of authentications
2437
# 1. server-side app
@@ -106,21 +119,15 @@ TeamSnap::isAuthed = ->
106119
TeamSnap::hasSession = ->
107120
!!browserStore()
108121

122+
# Destroy session with auth server and removes request object (if authed)
123+
TeamSnap::browserLogout = ->
124+
createAuthDialog(teamsnap.authUrl + '/logout')
125+
if @isAuthed
126+
@deleteAuth()
109127

110128
# Initializes teamsnap with clientId (and optionally secret on the server) to
111129
# allow authorization flows
112130
TeamSnap::init = (clientId, secret) ->
113-
114-
# Generates urls
115-
generateUrl = (endpoint, params) ->
116-
queries = []
117-
for key, value of params
118-
if value
119-
queries.push key + '=' + encodeURIComponent value
120-
url = teamsnap.authUrl + '/oauth/' + endpoint + '?' + queries.join('&')
121-
url.replace /%20/g, '+'
122-
123-
124131
# Generates the auth url for a resource owner to auth a client
125132
generateAuthUrl = (type, redirect, scopes) ->
126133
scopes = if Array.isArray(scopes) then scopes.join ' ' else scopes

src/teamsnap.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ promises = require './promises'
33
require './errors'
44

55
class TeamSnap
6-
version: '1.23.2'
6+
version: '1.24.0'
77
promises: promises
88
when: promises.when
99
TeamSnap: TeamSnap

0 commit comments

Comments
 (0)