summaryrefslogtreecommitdiff
path: root/p/scripts/persona.js
blob: 36aeeaf565ed2f1f4dd556cbb59205ade38fde65 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";

function init_persona() {
	if (!(navigator.id && window.$)) {
		if (window.console) {
			console.log('FreshRSS (Persona) waiting for JS…');
		}
		window.setTimeout(init_persona, 100);
		return;
	}

	$('a.signin').click(function() {
		navigator.id.request();
		return false;
	});

	$('a.signout').click(function() {
		navigator.id.logout();
		return false;
	});

	navigator.id.watch({
		loggedInUser: context['current_user_mail'],

		onlogin: function(assertion) {
			// A user has logged in! Here you need to:
			// 1. Send the assertion to your backend for verification and to create a session.
			// 2. Update your UI.
			$.ajax ({
				type: 'POST',
				url: url['login'],
				data: {assertion: assertion},
				success: function(res, status, xhr) {
					if (res.status === 'failure') {
						openNotification(res.reason, 'bad');
					} else if (res.status === 'okay') {
						location.href = url['index'];
					}
				},
				error: function(res, status, xhr) {
					// alert(res);
				}
			});
		},
		onlogout: function() {
			// A user has logged out! Here you need to:
			// Tear down the user's session by redirecting the user or making a call to your backend.
			// Also, make sure loggedInUser will get set to null on the next page load.
			// (That's a literal JavaScript null. Not false, 0, or undefined. null.)
			$.ajax ({
				type: 'POST',
				url: url['logout'],
				success: function(res, status, xhr) {
					location.href = url['index'];
				},
				error: function(res, status, xhr) {
					// alert(res);
				}
			});
		}
	});
}

if (document.readyState && document.readyState !== 'loading') {
	if (window.console) {
		console.log('FreshRSS (Persona) immediate init…');
	}
	init_persona();
} else if (document.addEventListener) {
	document.addEventListener('DOMContentLoaded', function () {
		if (window.console) {
			console.log('FreshRSS (Persona) waiting for DOMContentLoaded…');
		}
		init_persona();
	}, false);
}