Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Preparing for 0.8.39 release
  allow override of options.display for facebook auth
  correct display option for facebook auth
  minor
  Fix broken Hull.login without redirect
  Stray Debugger.
  Different-sized popups for Facebook
  Support redirect style Logins

Conflicts:
	bower.json
	package.json
  • Loading branch information
sbellity committed Sep 23, 2014
2 parents 1c4de2f + 6ccb458 commit e40ca24
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.8.39

* Allow redirect strategy login (for social and email/password)
* FB Auth: Use display: 'popup' option and smaller popup window

# 0.8.38

* Fix for browsers with 3rd party cookies disabled and (http://www.hull.io/docs/users/backend) : Use jquery ajaxSend to send user sig as a header
Expand Down
77 changes: 73 additions & 4 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,84 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link href="//hullstrap.s3.amazonaws.com/stylesheets/hullstrap.css" media="screen" rel="stylesheet" type="text/css" />
<link href="//hls3.s3.amazonaws.com/stylesheets/hullstrap.css" media="screen" rel="stylesheet" type="text/css" />

<title>Hull App...</title>
<script src="//code.jquery.com/jquery-1.8.3.js"></script>
<script src="//js.hull.dev/dist/current/hull.js"></script>
<script src="//localhost:3001/dist/current/hull.js"></script>
<script src="app.js"></script>
<script>
$(function() {
$('.login_facebook').on('click', function(e) {
e.preventDefault();
Hull.login('facebook');
});
$('.login_facebook_redirect').on('click', function(e) {
e.preventDefault();
Hull.login({ provider: 'facebook', strategy: 'redirect' });
});
$('.logout').on('click', function(e) {
e.preventDefault();
Hull.logout();
});

$('form[action="login"]').on('submit', function(e) {
e.preventDefault();
var login = $(e.target).serializeArray().reduce(function(l,i) {
if (i.value.length > 0) l[i.name] = i.value;
return l;
}, {});
login.strategy = login.redirect_url ? "redirect" : undefined;
console.warn("Login: ", login);
Hull.login(login);
});
});

Hull.ready(function(_, me) {
console.warn("Hello", me && me.name);
});
</script>
</head>
<body>
<div data-hull-component="login/profile@hull"></div>
<div data-hull-component="login/form@hull"></div>
<div class="container mt-1">
<div class="row">
<div class="col-sm-12">
<div class="btn-group btn-group-justified">
<a href="" class="btn btn-default login_facebook">Login Facebook</a>
<a href="" class="btn btn-default login_facebook_redirect">Login Facebook Redirect</a>
<a href="" class="btn btn-default login_email">Login email</a>
<a href="" class="btn btn-default login_email_redirect">Login email Redirect</a>
<a href="" class="btn btn-default logout">Logout</a>
</div>
</div>
</div>

<hr />

<div class="row">
<div class="col-sm-12" data-hull-component="login/profile"></div>
</div>

<hr />

<div class="row">
<div class="col-sm-12">
<form action="login">
<input type="text" name="redirect_url" placeholder="Redirect URL" />
<input type="text" name="login" placeholder="Login" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" >
</form>
</div>
</div>

<hr />

<div class="row">
<div class="col-sm-12">
<div class="log well"></div>
</div>
</div>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hull-js",
"version": "0.8.38",
"version": "0.8.39",
"main": "./lib/hull.js",
"dependencies": {
"jquery": "1.10.*",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hull-js",
"version": "0.8.38",
"version": "0.8.39",
"devDependencies": {
"grunt": "0.4.2",
"grunt-s3": "0.2.0-alpha",
Expand Down
96 changes: 78 additions & 18 deletions src/api/auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,71 @@ define ['underscore', '../utils/promises', '../utils/version'], (_, promises, ve
signup = (user) ->
apiFn('users', 'post', user).then emailLoginComplete, loginFailed

postForm = (path, method='post', params={}) ->
form = document.createElement("form")
form.setAttribute("method", method)
form.setAttribute("action", path)

for key of params
if params.hasOwnProperty key
hiddenField = document.createElement("input")
hiddenField.setAttribute("type", "hidden")
hiddenField.setAttribute("name", key)
hiddenField.setAttribute("value", params[key])
form.appendChild(hiddenField)
document.body.appendChild(form)
form.submit()

login = (params, options={}, callback) ->
refresh = ->
apiFn.clearToken()
apiFn('me')

# Legacy Format.
if _.isString(params)
if _.isString(options)
# Hull.login('[email protected]','password')
options = {login:params, password:options}
else
# Hull.login('facebook','opts')
options.provider = params
else
# We only use 1 hash for the new setup
options = params

login = (loginOrProvider, optionsOrPassword, callback) ->
throw new TypeError("'loginOrProvider' must be a String") unless _.isString(loginOrProvider)

if _.isString(optionsOrPassword)
promise = apiFn('users/login', 'post', { login: loginOrProvider, password: optionsOrPassword }).then ->
apiFn.clearToken()
apiFn('me')
evtPromise = promise.then emailLoginComplete, loginFailed
# Set defaults for Redirect to current page if redirecting.
options.redirect_url = options.redirect_url || window.location.href if options.strategy=='redirect'

# New Format:
if options.provider?
# Social Login
# Hull.login({provider:'facebook', strategy:'redirect|popup', redirect:'...'})
promise = loginWithProvider(options).then(refresh)
evtPromise = promise.then _.bind(loginComplete, undefined, params), loginFailed
else
promise = loginWithProvider(loginOrProvider, optionsOrPassword).then ->
apiFn.clearToken()
apiFn('me')
evtPromise = promise.then _.bind(loginComplete, undefined, loginOrProvider), loginFailed
# UserName+Password
# Hull.login({login:'[email protected]', password:'passwd', strategy:'redirect|popup', redirect:'...'})

throw new Error('Seems like something is wrong in your Hull.login() call, We need a login and password fields to login. Read up here: http://www.hull.io/docs/references/hull_js/#user-signup-and-login') unless options.login? and options.password?

# Early return since we're leaving the page.
if options.strategy=='redirect'
return postForm(config.orgUrl+'/api/v1/users/login', 'post', options)

promise = apiFn('users/login', 'post', _.pick(options, 'login', 'password')).then(refresh)
evtPromise = promise.then emailLoginComplete, loginFailed

evtPromise.then(callback) if _.isFunction(callback)

promise

loginWithProvider = (providerName, opts)->
loginWithProvider = (options)->
return module.isAuthenticating() if module.isAuthenticating()

providerName = providerName.toLowerCase()
providerName = options.provider.toLowerCase()
delete options.provider

authenticating = promises.deferred()
unless ~(_.indexOf(authServices, providerName ))
authenticating.reject
Expand All @@ -68,8 +110,18 @@ define ['underscore', '../utils/promises', '../utils/version'], (_, promises, ve

authenticating.providerName = providerName

authUrl = module.authUrl(config, providerName, opts)
module.authHelper(authUrl)

if providerName == 'facebook' && !options.display?
options.display = if options.strategy=='redirect' then 'page' else 'popup'

authUrl = module.authUrl(config, providerName, options)

if options.strategy == 'redirect'
# Don't do an early return to not break promise chain
window.location.href = authUrl
else
# Classic Popup Strategy
module.authHelper(authUrl, providerName)

authenticating.promise

Expand Down Expand Up @@ -105,7 +157,7 @@ define ['underscore', '../utils/promises', '../utils/version'], (_, promises, ve
auth_params = opts || {}
auth_params.app_id = config.appId
# The following is here for backward compatibility. Must be removed at first sight next time
auth_params.callback_url = config.callback_url || config.callbackUrl || module.location.toString()
auth_params.callback_url = opts.redirect_url || config.callback_url || config.callbackUrl || module.location.toString()
auth_params.auth_referer = module.location.toString()
auth_params.version = version
querystring = _.map auth_params,(v,k) ->
Expand All @@ -121,8 +173,16 @@ define ['underscore', '../utils/promises', '../utils/version'], (_, promises, ve
window.__hull_login_status__ = (hash) ->
window.__hull_login_status__ = null
onCompleteAuthentication(hash)
authHelper: (path)->
w = window.open(path, "_auth", 'location=0,status=0,width=1030,height=600')
authHelper: (path, providerName)->

if providerName == 'facebook'
width = 650
height = 430
else
width = 1030
width = 430

w = window.open(path, "_auth", "location=0,status=0,width=#{width},height=#{height}")

# Support for cordova events
if window.device?.cordova
Expand Down

0 comments on commit e40ca24

Please sign in to comment.