Get email and token from g+
[scpubgit/stemmaweb.git] / root / src / auth / login.tt
1 [% WRAPPER header.tt
2         pagetitle = "Stemmaweb - Sign in"
3         applicationstyle = c.uri_for('/css/auth.css')
4 %]
5     <script type="text/javascript">
6 $(document).ready(function() {
7     // call out to load the directory div
8     $('#login_actions').accordion();
9     $('.login_button').button();
10     $('#submit').button();
11     
12     var status = '[% status_msg %]';
13     if( status == 'Logged in!' ) {
14         setInterval( function ( e ) {
15                 window.opener.location.reload(true);
16                 window.close();
17                 e.preventDefault();
18         }, 2000 );
19     }
20 });
21
22         function googleSignIn(authResult) {
23                 if (authResult['status']['signed_in']) {
24                         document.getElementById('signinButton').setAttribute('style', 'display:none');
25                         gapi.client.load('plus', 'v1', function apiClientLoaded() {
26                                 gapi.client.plus.people.get({ userId: 'me'}).execute(function infoRetrieved(resp) {
27                                         var primaryEmail;
28                                         for (var i = 0; i < resp.emails.length; i++) {
29                                                 if (resp.emails[i].type === 'account') {
30                                                         primaryEmail = resp.emails[i].value;
31                                                 }
32                                         }
33
34                                         dataRetrieved(authResult, primaryEmail);
35                                 });
36                         });
37                 } else {
38                         console.log("Error", authResult);
39                 }
40         }
41
42         function dataRetrieved(login, email) {
43                 console.log(email);
44                 console.log(login.id_token);
45
46                 document.getElementById('email').value = email;
47                 document.getElementById('id_token').value = login.id_token;
48                 document.getElementById('google_form').submit();
49         }
50
51     </script>
52         <script src="https://apis.google.com/js/client:platform.js" async defer></script>
53 [% END %]
54         <div id="topbanner">
55                 <h1>Stemmaweb - Sign in</h1>
56         </div>
57
58 <div id="login_status">
59 [% IF status_msg %]
60                         <p>[% status_msg | html %]</p>
61 [% END %]
62 [% IF error_msg %]
63                         <p class="error">[% error_msg | html %]</p>
64 [% END %]
65
66 [% IF form.has_errors %]
67                         <p class="error">Some fields had errors:</p>
68                                         
69                         <ul class="errors">
70         [% FOREACH msg IN form.errors %]
71                                 <li>[% msg | html %]</li>
72         [% END %]
73                         </ul>
74 [% END %]
75 </div>
76 [% UNLESS status_msg == 'Logged in!' %]
77 <div id="login_actions">
78         <h3><a href="#">Sign in with Google</a></h3>
79         <div>
80                 <p>If you have a Google account, you may use it to sign into Stemmaweb.</p>
81                 <span id="signinButton">
82                         <span
83                         class="g-signin"
84                         data-callback="googleSignIn"
85                         data-clientid="577442226093-pi2ud795g49ibip78bgfoabhl4kdrguc.apps.googleusercontent.com"
86                         data-cookiepolicy="single_host_origin"
87                         data-requestvisibleactions="http://schema.org/AddAction"
88                         data-scope="https://www.googleapis.com/auth/plus.profile.emails.read"
89                         data-openidrealm="http://sherlock.scsys.co.uk:3000/"
90                         >
91                         </span>
92                 </span>
93                 <form id="google_form" action="[% c.uri_for_action('/users/login') | html %]" method="post">
94                         <input id='email' name='email' value='' type='hidden' />
95                         <input id='id_token' name='id_token' value='' type='hidden' />
96                 </form>
97         </div>
98
99 [% UNLESS c.req.param('realm') == 'openid' %]
100         <h3><a href="#">Sign in with Stemmaweb</a></h3>
101         <div>
102                 <p>If you do not have Google or another OpenID account, you may <a href="[% c.uri_for_action('/users/register') | html %]">register</a> for a user account here with its own password.  Once you are registered, you can use this form to sign in.</p>
103                 <form id="login_local_form" method="post" action="[% c.uri_for_action('/users/login') | html %]" autocomplete="off">
104                         <input type="hidden" name="realm" value="default"/>
105                         [% userlabel = form.field('username').label('Email address') %]
106                         [% form.field('username').render %]
107                         [% form.field('password').render %]
108                         
109                         [% rememberlabel = form.field('remember').label('Remember me') %]
110                         [% form.field('remember').render %]
111                         
112                         [% submitbutton = form.field('submit').value('Sign in with Stemmaweb') %]
113                         [% form.field('submit').render %]
114                 </form>
115         </div>
116 [% END %]
117 </div>
118 [% END %]
119 [% PROCESS footer.tt %]