forked from JamesLefrere/meteor-twitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch_client.coffee
35 lines (32 loc) · 1.55 KB
/
twitch_client.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Request Twitch credentials for the user
# @param options {optional}
# @param credentialRequestCompleteCallback {Function} Callback function to call on
# completion. Takes one argument, credentialToken on success, or Error on
# error.
Twitch.requestCredential = (options, credentialRequestCompleteCallback) ->
# support both (options, callback) and (callback).
if not credentialRequestCompleteCallback and typeof options is "function"
credentialRequestCompleteCallback = options
options = {}
config = ServiceConfiguration.configurations.findOne(service: "twitch")
unless config
credentialRequestCompleteCallback and credentialRequestCompleteCallback(new ServiceConfiguration.ConfigError())
return
credentialToken = Random.secret()
scope = (options and options.requestPermissions) or []
flatScope = _.map(scope, encodeURIComponent).join(" ")
loginStyle = OAuth._loginStyle("twitch", config, options)
loginUrl = "https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id=" + config.clientId + "&redirect_uri=" + OAuth._redirectUri("twitch", config) + "&scope=" + flatScope + "&state=" + OAuth._stateParam(loginStyle, credentialToken)
# twitch box gets taller when permissions requested.
height = 620
height += 130 if _.without(scope, "basic").length
OAuth.launchLogin
loginService: "twitch"
loginStyle: loginStyle
loginUrl: loginUrl
credentialRequestCompleteCallback: credentialRequestCompleteCallback
credentialToken: credentialToken
popupOptions:
width: 900
height: height
return