Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

externalized login controller as a service #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
</head>
<body>
<div id="wrap-page" ng-controller="LoginCtrl">
<div id="wrap-page" ng-controller="LoginCtrl">

<!-- main view -->
<div id="page" ui-view></div>
<div id="page" ui-view></div>

<!-- login box -->
<div id="wrap-login" class="ng-cloak" ng-show="loginrequired">
<div id="inner-login">
<div class="left">
<div class="title">Entrez votre identifiant pour vous connecter</div>
<form name="loginForm">
<p>
<label for="login-field">Email</label>
<input id="login-field" type="text" ng-model="username" placeholder="Votre nom d'utilisateur..." required>
</p>
<p>
<label for="pass-field">Password</label>
<input id="pass-field" type="password" ng-model="password" placeholder="Votre mot de passe..." required>
</p>
<button ng-click="submit()" ng-disabled="form.$invalid" class="submit">Login</button>
<a class="forgotten" href="#">Mot de passe oublié ?</a>
</form>
<!-- login box -->
<div id="wrap-login" class="ng-cloak" ng-show="authVars.loginrequired">
<div id="inner-login">
<div class="left">
<div class="title">Entrez votre identifiant pour vous connecter</div>
<form name="loginForm">
<p>
<label for="login-field">Email</label>
<input id="login-field" type="text" ng-model="authVars.username" placeholder="Votre nom d'utilisateur..." required>
</p>
<p>
<label for="pass-field">Password</label>
<input id="pass-field" type="password" ng-model="authVars.password" placeholder="Votre mot de passe..." required>
</p>
<button ng-click="submit()" ng-disabled="form.$invalid" class="submit">Login</button>
<a class="forgotten" href="#">Mot de passe oublié ?</a>
</form>
</div>
<div class="right">
<ul>
<li><button class="account">S'inscrire</button></li>
<li><button ng-click="authenticateGoogle()" class="gplus">Google +</button></li>
<li><button class="facebook">Facebook</button></li>
</ul>
</div>
<a href="javascript:void(0)" ng-click="authVars.loginrequired=false" class="close">Fermer</a>
<!-- TODO: changer cette méthode de fermeture de l'overlay -->
</div>
<div class="right">
<ul>
<li><button class="account">S'inscrire</button></li>
<li><button ng-click="authenticateGoogle()" class="gplus">Google +</button></li>
<li><button class="facebook">Facebook</button></li>
</ul>
</div>
<a href="javascript:void(0)" ng-click="loginrequired=false" class="close">Fermer</a>
<!-- TODO: changer cette méthode de fermeture de l'overlay -->
</div>
</div>
</div>
<!--
<footer>
<nav>
Expand Down Expand Up @@ -100,7 +100,7 @@

<script src="scripts/vendor/angularOauth.js"></script>
<script src="scripts/vendor/googleOauth.js"></script>

<script src="scripts/vendor/angular-unisson-auth/angular-unisson-auth.js"></script>

<!-- Common -->
<script src="scripts/lib/common/controllers.js"></script>
Expand All @@ -114,6 +114,7 @@
<script src="scripts/lib/map/directives.js" type="text/javascript"></script>

<!-- main app -->
<script src="scripts/config.js"></script>
<script src="scripts/app.js"></script>

</html>
Expand Down
8 changes: 3 additions & 5 deletions scripts/app.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
config = {
templateBaseUrl: '/views/',
}

angular.module('map', ['map.controllers', 'map.services', 'map.filters', 'leaflet-directive']);
angular.module('common', ['common.filters', 'common.controllers', 'common.services']);

app = angular.module('unisson_map', ['common', 'map', 'ui.router', 'ngAnimate', 'googleOauth']);
app = angular.module('unisson_map', ['common', 'map', 'ui.router', 'ngAnimate', 'googleOauth', 'angular-unisson-auth']);

// Config
app.constant('moduleTemplateBaseUrl', config.templateBaseUrl + 'map/');
Expand Down Expand Up @@ -96,6 +92,8 @@ app.config(['$locationProvider', '$stateProvider', '$urlRouterProvider', 'module
app.run(['$rootScope', function($rootScope) {
$rootScope.MEDIA_URI = 'http://localhost:8000';
$rootScope.CONFIG = config;
$rootScope.loginBaseUrl = config.loginBaseUrl;
$rootScope.homeStateName = config.homeStateName;

$rootScope.$on('$stateChangeSuccess', function (event, current, previous) {
if ( current.page_title )
Expand Down
11 changes: 11 additions & 0 deletions scripts/config.js.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
config = {
templateBaseUrl: '/views/',
bucket_uri: 'http://localhost:8000/bucket/upload/',
useHtml5Mode: false,
media_uri: 'http://localhost:8000',
rest_uri: "http://localhost:8000/api/v0",
rootUrl: "http://localhost:8082/",
applicationAlias : "#/",
homeStateName: 'index',
loginBaseUrl: 'http://localhost:8000/api/v0'
}
96 changes: 4 additions & 92 deletions scripts/src/common/controllers.coffee
Original file line number Diff line number Diff line change
@@ -1,95 +1,7 @@
module = angular.module('common.controllers', ['http-auth-interceptor', 'ngCookies', 'googleOauth'])
module = angular.module('common.controllers', ['angular-unisson-auth'])

class LoginCtrl
"""
Login a user
"""
constructor: (@$scope, @$rootScope, @$http, @$state, @Restangular, @$cookies, @authService, @Token) ->
@$scope.isAuthenticated = false
@$scope.username = ""
@$scope.loginrequired = false
constructor: (@$scope, @loginService) ->
@$scope.loginService = @loginService

# On login required
@$scope.$on('event:auth-loginRequired', =>
@$scope.loginrequired = true
console.debug("Login required")
)

# On login successful
@$scope.$on('event:auth-loginConfirmed', =>
console.debug("Login OK")
@$scope.loginrequired = false
@$scope.username = @$cookies.username
@$scope.isAuthenticated = true
)

# set authorization header if already logged in
if @$cookies.username and @$cookies.key
console.debug("Already logged in.")
@$http.defaults.headers.common['Authorization'] = "ApiKey #{@$cookies.username}:#{@$cookies.key}"
@authService.loginConfirmed()


@$scope.accessToken = @Token.get()

# Add methods to scope
@$scope.submit = this.submit
@$scope.authenticateGoogle = this.authenticateGoogle
@$scope.forceLogin = this.forceLogin
@$scope.logout = this.logout

forceLogin: =>
@$scope.loginrequired = true

logout: =>
@$scope.isAuthenticated = false
delete @$http.defaults.headers.common['Authorization']
delete @$cookies['username']
delete @$cookies['key']
@$scope.username = ""

@$state.go('index')


submit: =>
console.debug('submitting login...')
@Restangular.all('account/user').customPOST("login", {}, {},
username: @$scope.username
password: @$scope.password
).then((data) =>
@$cookies.username = data.username
@$cookies.key = data.key
@$http.defaults.headers.common['Authorization'] = "ApiKey #{data.username}:#{data.key}"
@authService.loginConfirmed()
, (data) =>
console.debug("LoginController submit error: #{data.reason}")
@$scope.errorMsg = data.reason
)

authenticateGoogle: =>
extraParams = {}
if @$scope.askApproval
extraParams = {approval_prompt: 'force'}

@Token.getTokenByPopup(extraParams).then((params) =>

# Verify the token before setting it, to avoid the confused deputy problem.
@Restangular.all('account/user/login').customPOST("google", {}, {},
access_token: params.access_token
).then((data) =>
@$cookies.username = data.username
@$cookies.key = data.key
@$http.defaults.headers.common['Authorization'] = "ApiKey #{data.username}:#{data.key}"
@authService.loginConfirmed()
, (data) =>
console.debug("LoginController submit error: #{data.reason}")
@$scope.errorMsg = data.reason
)
, ->
# Failure getting token from popup.
alert("Failed to get token from popup.")
)

LoginCtrl.$inject = ['$scope', '$rootScope', "$http", "$state", "Restangular", "$cookies", "authService", "Token"]

module.controller("LoginCtrl", LoginCtrl)
module.controller("LoginCtrl", ['$scope','loginService', LoginCtrl])
Loading