Skip to content
This repository has been archived by the owner on Dec 24, 2019. It is now read-only.

Commit

Permalink
add relog using window.open+postMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
prigaux committed Dec 11, 2014
1 parent 4d51d23 commit 0dd97c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ config(['$routeProvider', function($routeProvider) {
authService.loginConfirmed(resp.data);
}, function () {
// jsonp failed,
// start window.open + postMessage
$modal.open({ templateUrl: 'relog/relog.html', controller: 'RelogCtrl', backdrop: false });
});
});
Expand Down
16 changes: 14 additions & 2 deletions app/backend/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
require_once 'init.inc.php';

phpCAS::handleLogoutRequests();
if (!phpCAS::checkAuthentication()) {
if (isset($_GET["postMessage"])) {
phpCAS::forceAuthentication();
} else {
if (!phpCAS::checkAuthentication()) {
header('HTTP/1.0 401 Unauthorized');
echo 'HTTP/1.0 401 Unauthorized';
exit();
}
}

$user = array('id' => phpCAS::getUser());

if (isset($_GET["callback"])) {
if (isset($_GET["postMessage"])) {
?>
Login success, please wait...
<script>
(window.opener ? (window.opener.postMessage ? window.opener : window.opener.document) : window.parent).postMessage('loggedUser=' +
JSON.stringify(<? echo json_encode($user); ?>), '*');
</script>
<?
} else if (isset($_GET["callback"])) {
header('Content-type: application/javascript; charset=UTF-8');
echo $_GET["callback"] . "(" . json_encode($user) . ");";
} else {
Expand Down
30 changes: 27 additions & 3 deletions app/relog/relog.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
'use strict';

var relogState = {};

angular.module('myApp.relog', [])

.controller('RelogCtrl', function($scope, $window) {
$scope.relog = function () {
$window.location = "index.php";
.controller('RelogCtrl', function($rootScope, $scope, $window, $modalInstance, $http, authService) {

function windowOpenCleanup(state) {
try {
if (state.listener) $window.removeEventListener("message", state.listener);
if (state.window) state.window.close();
if ($modalInstance) $modalInstance.close();
} catch (e) {}
}

function onmessage(e) {
if (typeof e.data !== "string") return;
var m = e.data.match(/^loggedUser=(.*)$/);
if (!m) return;

var user = angular.fromJson(m[1]);
windowOpenCleanup(relogState);
authService.loginConfirmed(user);
};

$scope.relog = function () {
windowOpenCleanup(relogState);
relogState.listener = onmessage;
$window.addEventListener("message", onmessage);
relogState.window = $window.open('backend/login.php?postMessage');
};
});

0 comments on commit 0dd97c1

Please sign in to comment.