2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2014-01-27 12:50:13 -05:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var appCtrl = require('../app-controller'),
|
|
|
|
cfg = require('../app-config').config;
|
2014-01-27 12:50:13 -05:00
|
|
|
|
2014-11-07 16:21:37 -05:00
|
|
|
var AddAccountCtrl = function($scope, $location, $routeParams, $http) {
|
2014-10-02 16:05:44 -04:00
|
|
|
if (!appCtrl._auth && !$routeParams.dev) {
|
|
|
|
$location.path('/'); // init app
|
|
|
|
return;
|
|
|
|
}
|
2014-08-13 10:16:28 -04:00
|
|
|
|
2014-11-07 16:21:37 -05:00
|
|
|
$scope.getAccountSettings = function() {
|
2014-10-02 16:05:44 -04:00
|
|
|
if ($scope.form.$invalid) {
|
2014-11-09 14:58:13 -05:00
|
|
|
$scope.errMsg = 'Please enter a valid email address!';
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.busy = true;
|
|
|
|
$scope.errMsg = undefined; // reset error msg
|
|
|
|
|
2014-11-07 16:21:37 -05:00
|
|
|
var domain = $scope.emailAddress.split('@')[1];
|
|
|
|
var url = cfg.settingsUrl + domain;
|
2014-09-19 12:59:13 -04:00
|
|
|
|
2014-11-09 11:34:48 -05:00
|
|
|
return $http.get(url).then(function(res) {
|
|
|
|
var config = res.data;
|
|
|
|
|
2014-11-07 16:21:37 -05:00
|
|
|
$scope.busy = false;
|
2014-11-08 07:02:24 -05:00
|
|
|
$scope.state.login = {
|
|
|
|
mailConfig: config,
|
|
|
|
emailAddress: $scope.emailAddress
|
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-07 16:21:37 -05:00
|
|
|
var hostname = config.imap.hostname;
|
2014-11-08 07:02:24 -05:00
|
|
|
if (hostname && hostname.match(/.gmail.com$/) || hostname.match(/.googlemail.com$/)) {
|
|
|
|
// check for gmail/oauth support
|
2014-11-07 16:21:37 -05:00
|
|
|
$scope.connectToGoogle();
|
2014-11-08 07:02:24 -05:00
|
|
|
} else if (config.imap.source === 'guess') {
|
|
|
|
// use standard password login... show config details due to guess
|
2014-11-10 12:36:26 -05:00
|
|
|
$scope.setCredentials('custom');
|
2014-11-08 07:02:24 -05:00
|
|
|
} else {
|
|
|
|
// use standard password login... hide config details
|
2014-11-10 12:36:26 -05:00
|
|
|
$scope.setCredentials();
|
2014-09-19 12:59:13 -04:00
|
|
|
}
|
|
|
|
|
2014-11-09 11:34:48 -05:00
|
|
|
}).catch(function() {
|
2014-11-07 16:21:37 -05:00
|
|
|
$scope.busy = false;
|
2014-11-08 07:02:24 -05:00
|
|
|
$scope.errMsg = 'Error fetching IMAP settings for that email address!';
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.connectToGoogle = function() {
|
|
|
|
// test for oauth support
|
|
|
|
if (appCtrl._auth._oauth.isSupported()) {
|
2014-11-08 07:02:24 -05:00
|
|
|
// ask user to use the platform's native OAuth api
|
|
|
|
$scope.onError({
|
|
|
|
title: 'Google Account Login',
|
|
|
|
message: 'You are signing into a Google account. Would you like to sign in with Google or just continue with a password login?',
|
|
|
|
positiveBtnStr: 'Google sign in',
|
|
|
|
negativeBtnStr: 'Password',
|
|
|
|
showNegativeBtn: true,
|
|
|
|
faqLink: 'https://github.com/whiteout-io/mail-html5/wiki/FAQ#how-does-sign-in-with-google-work',
|
|
|
|
callback: function(granted) {
|
|
|
|
if (granted) {
|
2014-11-10 12:36:26 -05:00
|
|
|
// query oauth token
|
2014-11-08 07:02:24 -05:00
|
|
|
useOAuth();
|
|
|
|
} else {
|
2014-11-10 12:36:26 -05:00
|
|
|
// use normal user/password login
|
|
|
|
$scope.setCredentials('gmail');
|
2014-11-08 07:02:24 -05:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
2014-09-19 12:59:13 -04:00
|
|
|
}
|
2014-01-27 12:50:13 -05:00
|
|
|
});
|
2014-11-08 07:02:24 -05:00
|
|
|
} else {
|
|
|
|
// no oauth support
|
2014-11-10 12:36:26 -05:00
|
|
|
// use normal user/password login
|
|
|
|
$scope.setCredentials('gmail');
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-11-08 07:02:24 -05:00
|
|
|
|
2014-11-10 12:36:26 -05:00
|
|
|
function useOAuth() {
|
|
|
|
// fetches the email address from the chrome identity api
|
|
|
|
appCtrl._auth.getOAuthToken(function(err) {
|
|
|
|
if (err) {
|
|
|
|
return $scope.onError(err);
|
|
|
|
}
|
|
|
|
$scope.setCredentials('gmail');
|
|
|
|
$scope.$apply();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-10 12:36:26 -05:00
|
|
|
$scope.setCredentials = function(provider) {
|
2014-10-02 16:05:44 -04:00
|
|
|
$location.path('/login-set-credentials').search({
|
|
|
|
provider: provider
|
|
|
|
});
|
2014-11-10 12:36:26 -05:00
|
|
|
};
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-01-27 12:50:13 -05:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = AddAccountCtrl;
|