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

Commit 0dd97c1

Browse files
committed
add relog using window.open+postMessage
1 parent 4d51d23 commit 0dd97c1

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

app/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ config(['$routeProvider', function($routeProvider) {
2323
authService.loginConfirmed(resp.data);
2424
}, function () {
2525
// jsonp failed,
26+
// start window.open + postMessage
2627
$modal.open({ templateUrl: 'relog/relog.html', controller: 'RelogCtrl', backdrop: false });
2728
});
2829
});

app/backend/login.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,27 @@
33
require_once 'init.inc.php';
44

55
phpCAS::handleLogoutRequests();
6-
if (!phpCAS::checkAuthentication()) {
6+
if (isset($_GET["postMessage"])) {
7+
phpCAS::forceAuthentication();
8+
} else {
9+
if (!phpCAS::checkAuthentication()) {
710
header('HTTP/1.0 401 Unauthorized');
811
echo 'HTTP/1.0 401 Unauthorized';
912
exit();
13+
}
1014
}
1115

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

14-
if (isset($_GET["callback"])) {
18+
if (isset($_GET["postMessage"])) {
19+
?>
20+
Login success, please wait...
21+
<script>
22+
(window.opener ? (window.opener.postMessage ? window.opener : window.opener.document) : window.parent).postMessage('loggedUser=' +
23+
JSON.stringify(<? echo json_encode($user); ?>), '*');
24+
</script>
25+
<?
26+
} else if (isset($_GET["callback"])) {
1527
header('Content-type: application/javascript; charset=UTF-8');
1628
echo $_GET["callback"] . "(" . json_encode($user) . ");";
1729
} else {

app/relog/relog.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
'use strict';
22

3+
var relogState = {};
4+
35
angular.module('myApp.relog', [])
46

5-
.controller('RelogCtrl', function($scope, $window) {
6-
$scope.relog = function () {
7-
$window.location = "index.php";
7+
.controller('RelogCtrl', function($rootScope, $scope, $window, $modalInstance, $http, authService) {
8+
9+
function windowOpenCleanup(state) {
10+
try {
11+
if (state.listener) $window.removeEventListener("message", state.listener);
12+
if (state.window) state.window.close();
13+
if ($modalInstance) $modalInstance.close();
14+
} catch (e) {}
815
}
16+
17+
function onmessage(e) {
18+
if (typeof e.data !== "string") return;
19+
var m = e.data.match(/^loggedUser=(.*)$/);
20+
if (!m) return;
21+
22+
var user = angular.fromJson(m[1]);
23+
windowOpenCleanup(relogState);
24+
authService.loginConfirmed(user);
25+
};
26+
27+
$scope.relog = function () {
28+
windowOpenCleanup(relogState);
29+
relogState.listener = onmessage;
30+
$window.addEventListener("message", onmessage);
31+
relogState.window = $window.open('backend/login.php?postMessage');
32+
};
933
});

0 commit comments

Comments
 (0)