This repository was archived by the owner on Jun 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlogin.js
More file actions
170 lines (153 loc) · 4.62 KB
/
Copy pathlogin.js
File metadata and controls
170 lines (153 loc) · 4.62 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'use strict';
angular.module('stormpathIdpApp')
.controller('LoginCtrl', function ($scope,Stormpath,$window) {
$scope.ready = false;
$scope.canRegister = true;
$scope.errors = {
badLogin: false,
notFound: false,
userMessage: false,
unknown: false,
organizationNameKeyRequired: false,
organizationNameKeyInvalid: false
};
function initFB(){
$window.fbAsyncInit = function() {
var FB = $window.FB;
FB.init({
appId: Stormpath.getProvider('facebook').clientId,
xfbml: true,
status: true,
version: 'v2.0'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = '//connect.facebook.net/es_LA/sdk.js';
fjs.parentNode.insertBefore(js, fjs);
}($window.document, 'script', 'facebook-jssdk'));
}
Stormpath.init.then(function initSuccess(){
$scope.organizationNameKey = Stormpath.getOrganizationNameKey();
$scope.showOrganizationField = Stormpath.client.jwtPayload.sof;
$scope.disableOrganizationField = $scope.organizationNameKey !== '';
$scope.canRegister = !!Stormpath.idSiteModel.passwordPolicy;
$scope.providers = Stormpath.providers;
$scope.ready = true;
$scope.hasProviders = $scope.providers.length > 0;
if(Stormpath.getProvider('facebook')){
initFB();
}
});
var googleIsSignedIn = false;
function clearErrors(){
Object.keys($scope.errors).map(function(k){$scope.errors[k]=false;});
}
function showError(err){
if(err.status===400){
if(err.code && err.code===2014){
$scope.errors.organizationNameKeyInvalid = true;
}else{
$scope.errors.badLogin = true;
}
}
else if(err.status===404){
$scope.errors.notFound = true;
}
else if(err.userMessage || err.message){
$scope.errors.userMessage = err.userMessage || err.message;
}else{
$scope.errors.unknown = true;
}
}
function errHandler(err){
$scope.submitting = false;
if(err){
showError(err);
}
}
$scope.submit = function(){
clearErrors();
if($scope.showOrganizationField && !$scope.organizationNameKey){
$scope.errors.organizationNameKeyRequired = true;
}
else if($scope.username && $scope.password){
$scope.submitting = true;
var data = {
login: $scope.username.trim(),
password: $scope.password.trim()
};
if($scope.organizationNameKey){
data.accountStore = {
nameKey: $scope.organizationNameKey
};
}
if(Stormpath.client.jwtPayload.ash){
data.accountStore = {
href: Stormpath.client.jwtPayload.ash
};
}
Stormpath.login(data,errHandler);
}else{
$scope.errors.emailPasswordRequired = true;
}
};
$scope.googleLogin = function(){
var gapi = $window.gapi;
if(!gapi){
return;
}
clearErrors();
var params = {
clientid: Stormpath.getProvider('google').clientId,
scope: 'email',
cookiepolicy: 'single_host_origin',
callback: function(authResult){
if (!googleIsSignedIn && authResult.status.signed_in && authResult.status.method === 'PROMPT') {
googleIsSignedIn = true;
Stormpath.register({
providerData: {
providerId: 'google',
accessToken: authResult.access_token
}
},errHandler);
}
}
};
gapi.auth.signIn(params);
};
function fbRegister(response){
Stormpath.register({
providerData: {
providerId: 'facebook',
accessToken: response.authResponse.accessToken
}
},errHandler);
}
$scope.facebookLogin = function(){
var FB = $window.FB;
FB.login(function(response) {
if(response.status === 'connected'){
fbRegister(response);
}
},{scope: 'email'});
};
$scope.samlLogin = function(provider){
Stormpath.samlLogin(provider.accountStore,errHandler);
};
$scope.providerLogin = function(provider){
var providerId = provider.providerId;
var fn = $scope[providerId+'Login'];
if(typeof fn!=='function'){
console.error('provider login function \'' + providerId + '\' is not implemented');
}else{
fn(provider);
}
};
return $scope;
});