forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.client.controller.js
More file actions
92 lines (71 loc) · 2.98 KB
/
Copy pathauthentication.client.controller.js
File metadata and controls
92 lines (71 loc) · 2.98 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
(function () {
'use strict';
angular
.module('users')
.controller('AuthenticationController', AuthenticationController);
AuthenticationController.$inject = ['$scope', '$state', 'UsersService', '$location', '$window', 'Authentication', 'PasswordValidator', 'Notification'];
function AuthenticationController($scope, $state, UsersService, $location, $window, Authentication, PasswordValidator, Notification) {
var vm = this;
vm.authentication = Authentication;
vm.getPopoverMsg = PasswordValidator.getPopoverMsg;
vm.signup = signup;
vm.signin = signin;
vm.callOauthProvider = callOauthProvider;
vm.usernameRegex = /^(?=[\w.-]+$)(?!.*[._-]{2})(?!\.)(?!.*\.$).{3,34}$/;
// Get an eventual error defined in the URL query string:
if ($location.search().err) {
Notification.error({ message: $location.search().err });
}
// If user is signed in then redirect back home
if (vm.authentication.user) {
$location.path('/');
}
function signup(isValid) {
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
return false;
}
UsersService.userSignup(vm.credentials)
.then(onUserSignupSuccess)
.catch(onUserSignupError);
}
function signin(isValid) {
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'vm.userForm');
return false;
}
UsersService.userSignin(vm.credentials)
.then(onUserSigninSuccess)
.catch(onUserSigninError);
}
// OAuth provider request
function callOauthProvider(url) {
if ($state.previous && $state.previous.href) {
url += '?redirect_to=' + encodeURIComponent($state.previous.href);
}
// Effectively call OAuth authentication route:
$window.location.href = url;
}
// Authentication Callbacks
function onUserSignupSuccess(response) {
// If successful we assign the response to the global user model
vm.authentication.user = response;
Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> Signup successful!' });
// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}
function onUserSignupError(response) {
Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Signup Error!', delay: 6000 });
}
function onUserSigninSuccess(response) {
// If successful we assign the response to the global user model
vm.authentication.user = response;
Notification.info({ message: 'Welcome ' + response.firstName });
// And redirect to the previous or home page
$state.go($state.previous.state.name || 'home', $state.previous.params);
}
function onUserSigninError(response) {
Notification.error({ message: response.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Signin Error!', delay: 6000 });
}
}
}());