From b172b07ed99f0e597f97b305c1388dee35c66d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Mon, 21 Jul 2014 16:23:15 +0200 Subject: [PATCH] Finish the module auth --- app/coffee/app.coffee | 23 ++- app/coffee/config.coffee | 2 +- app/coffee/modules/auth.coffee | 180 ++++++++++-------- app/coffee/modules/base/confirm.coffee | 4 +- app/partials/backlog.jade | 3 +- .../components/notification-message.jade | 2 +- app/partials/views/modules/login-form.jade | 2 +- config/main.coffee.example | 6 + 8 files changed, 130 insertions(+), 92 deletions(-) diff --git a/app/coffee/app.coffee b/app/coffee/app.coffee index aa62c61d..0c0ef7e5 100644 --- a/app/coffee/app.coffee +++ b/app/coffee/app.coffee @@ -21,8 +21,7 @@ @taiga = taiga = {} -configure = ($routeProvider, $locationProvider, $httpProvider, $provide, - $compileProvider, $gmUrlsProvider) -> +configure = ($routeProvider, $locationProvider, $httpProvider, $provide) -> $routeProvider.when("/project/:pslug/backlog", {templateUrl: "/partials/backlog.html"}) $routeProvider.when("/project/:pslug/taskboard/:id", {templateUrl: "/partials/taskboard.html"}) @@ -87,14 +86,17 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $httpProvider.defaults.headers.put = defaultHeaders $httpProvider.defaults.headers.get = {} - # authHttpIntercept = ($q, $location) -> - # return (promise) -> - # return promise.then null, (response) -> - # if response.status == 401 or response.status == 0 - # $location.url("/login?next=#{$location.path()}") - # return $q.reject(response) - # $provide.factory("authHttpIntercept", ["$q", "$location", authHttpIntercept]) - # $httpProvider.responseInterceptors.push('authHttpIntercept') + # Add next param when user try to access to a secction need auth permissions. + authHttpIntercept = ($q, $location) -> + return (promise) -> + return promise.then null, (response) -> + if response.status == 401 or response.status == 0 + nextPath = $location.path() + $location.url("/login").search("next=#{nextPath}") + return $q.reject(response) + + $provide.factory("authHttpIntercept", ["$q", "$location", authHttpIntercept]) + $httpProvider.responseInterceptors.push('authHttpIntercept') init = ($log, $i18n, $config, $rootscope) -> @@ -139,6 +141,7 @@ module.config([ "$routeProvider", "$locationProvider", "$httpProvider", + '$provide', configure ]) diff --git a/app/coffee/config.coffee b/app/coffee/config.coffee index 22b48ba3..d4a2952f 100644 --- a/app/coffee/config.coffee +++ b/app/coffee/config.coffee @@ -31,7 +31,7 @@ class ConfigService extends taiga.Service "es": "Spanish" "en": "English" } - allowPublicRegistration: false + pubblicRegisterEnabled: false } initialize: (localconfig) -> diff --git a/app/coffee/modules/auth.coffee b/app/coffee/modules/auth.coffee index 04fa49bd..67295100 100644 --- a/app/coffee/modules/auth.coffee +++ b/app/coffee/modules/auth.coffee @@ -28,7 +28,12 @@ module = angular.module("taigaAuth", ["taigaResources"]) ############################################################################# class AuthService extends taiga.Service - @.$inject = ["$rootScope", "$tgStorage", "$tgModel", "$tgResources", "$tgHttp", "$tgUrls"] + @.$inject = ["$rootScope", + "$tgStorage", + "$tgModel", + "$tgResources", + "$tgHttp", + "$tgUrls"] constructor: (@rootscope, @storage, @model, @rs, @http, @urls) -> super() @@ -60,6 +65,9 @@ class AuthService extends taiga.Service getToken: -> return @storage.get("token") + removeToken: -> + @storage.remove("token") + isAuthenticated: -> if @.getUser() != null return true @@ -75,6 +83,8 @@ class AuthService extends taiga.Service data = _.clone(data, false) data.type = if type then type else "normal" + @.removeToken() + return @http.post(url, data).then (data, status) => user = @model.make_model("users", data.data) @.setToken(user.auth_token) @@ -89,23 +99,30 @@ class AuthService extends taiga.Service if type == "private" data.existing = if existing then existing else false + @.removeToken() + return @http.post(url, data).then (response) => user = @model.make_model("users", response.data) @.setToken(user.auth_token) @.setUser(user) return user + getInvitation: (token) -> + return @rs.invitations.get(token) + acceptInvitiationWithNewUser: (data) -> - return register(data, "private", false) + return @.register(data, "private", false) acceptInvitiationWithExistingUser: (data) -> - return register(data, "private", true) + return @.register(data, "private", true) forgotPassword: (data) -> url = @urls.resolve("users-password-recovery") data = _.clone(data, false) + @.removeToken() + return @http.post(url, data) @@ -114,10 +131,9 @@ class AuthService extends taiga.Service data = _.clone(data, false) - return @http.post(url, data) + @.removeToken() - getInvitation: (token) -> - return @rs.invitations.get(token) + return @http.post(url, data) module.service("$tgAuth", AuthService) @@ -131,26 +147,29 @@ module.service("$tgAuth", AuthService) ## Login Directive ################### -LoginDirective = ($auth, $confirm, $location, $config) -> +LoginDirective = ($auth, $confirm, $location, $config, $routeParams) -> link = ($scope, $el, $attrs) -> - $scope.allowPublicRegistration = $config.get("allowPublicRegistration") - + $scope.pubblicRegisterEnabled = $config.get("pubblicRegisterEnabled") $scope.data = {} form = $el.find("form").checksley() + onSuccessSubmit = (response) -> + if $routeParams and $routeParams['next'] and $routeParams['next'] != '/login' + $location.url($routeParams['next']) + else + console.log("TODO: Redirect to '/'") # TODO: Redirect to / + $location.path("/project/project-example-0/backlog") + + onErrorSubmit = (response) -> + $confirm.notify("light-error", "According to our Oompa Loompas, your username/email or password + are incorrect.") #TODO: i18n + submit = -> if not form.validate() return promise = $auth.login($scope.data) - promise.then (response) -> - # TODO: finish this. Go tu user home page - $location.path("/project/project-example-0/backlog") - - promise.then null, (response) -> - $confirm.notify("light-error", "According to our Oompa Loompas, - your username/email or password - are incorrect.") #TODO: i18n + promise.then(onSuccessSubmit, onErrorSubmit) $el.on "submit", (event) -> event.preventDefault() @@ -162,30 +181,33 @@ LoginDirective = ($auth, $confirm, $location, $config) -> return {link:link} +module.directive("tgLogin", ["$tgAuth", "$tgConfirm", "$location", "$tgConfig", "$routeParams", LoginDirective]) + ################### ## Register Directive ################### -RegisterDirective = ($auth, $confirm) -> +RegisterDirective = ($auth, $confirm, $location) -> link = ($scope, $el, $attrs) -> $scope.data = {} form = $el.find("form").checksley() + onSuccessSubmit = (response) -> + $confirm.notify("success", "Our Oompa Loompas are happy, wellcome to Taiga.") #TODO: i18n + # TODO: finish this. Go tu '/' + $location.path("/project/project-example-0/backlog") + + onErrorSubmit = (response) -> + $confirm.notify("light-error", "According to our Oompa Loompas, the username or email is + already in use.") #TODO: i18n + submit = -> if not form.validate() return promise = $auth.register($scope.data) - promise.then (response) -> - $confirm.notify("success", "Our Oompa Loompas are happy, wellcome to Taiga.") #TODO: i18n - # TODO: finish this. Go tu user home page - $location.path("/project/project-example-0/backlog") - - promise.then null, (response) -> - $confirm.notify("light-error", "According to our Oompa Loompas, - your are not registered yet or - type an invalid password.") #TODO: i18n + promise.then(onSuccessSubmit, onErrorSubmit) $el.on "submit", (event) -> event.preventDefault() @@ -197,6 +219,8 @@ RegisterDirective = ($auth, $confirm) -> return {link:link} +module.directive("tgRegister", ["$tgAuth", "$tgConfirm", "$location", RegisterDirective]) + ################### ## Forgot Password Directive @@ -207,21 +231,23 @@ ForgotPasswordDirective = ($auth, $confirm, $location) -> $scope.data = {} form = $el.find("form").checksley() + onSuccessSubmit = (response) -> + $location.path("/login") # TODO: Use the future 'urls' service + $confirm.success("Check your inbox!
+ We have sent a mail to
+ #{response.data.email}
+ with the instructions to set a new password") #TODO: i18n + + onErrorSubmit = (response) -> + $confirm.notify("light-error", "According to our Oompa Loompas, + your are not registered yet.") #TODO: i18n + submit = -> if not form.validate() return promise = $auth.forgotPassword($scope.data) - promise.then (response) -> - $location.path("/login") # TODO: Use the future 'urls' service - $confirm.success("Check your inbox!
- We have sent a mail to
- #{response.data.email}
- with the instructions to set a new password") #TODO: i18n - - promise.then null, (response) -> - $confirm.notify("light-error", "According to our Oompa Loompas, - your are not registered yet.") #TODO: i18n + promise.then(onSuccessSubmit, onErrorSubmit) $el.on "submit", (event) -> event.preventDefault() @@ -233,6 +259,8 @@ ForgotPasswordDirective = ($auth, $confirm, $location) -> return {link:link} +module.directive("tgForgotPassword", ["$tgAuth", "$tgConfirm", "$location", ForgotPasswordDirective]) + ################### ## Change Password from Recovery Directive @@ -250,19 +278,21 @@ ChangePasswordFromRecoveryDirective = ($auth, $confirm, $location, $params) -> form = $el.find("form").checksley() + onSuccessSubmit = (response) -> + $location.path("/login") # TODO: Use the future 'urls' service + $confirm.success("Our Oompa Loompas save your new password.
+ Try to sign in with it.") #TODO: i18n + + onErrorSubmit = (response) -> + $confirm.notify("light-error", "One of our Oompa Loompas say + '#{response.data._error_message}'.") #TODO: i18n + submit = -> if not form.validate() return promise = $auth.changePasswordFromRecovery($scope.data) - promise.then (response) -> - $location.path("/login") # TODO: Use the future 'urls' service - $confirm.success("Our Oompa Loompas save your new password.
- Try to sign in with it.") #TODO: i18n - - promise.then null, (response) -> - $confirm.notify("light-error", "One of our Oompa Loompas say - '#{response.data._error_message}'.") #TODO: i18n + promise.then(onSuccessSubmit, onErrorSubmit) $el.on "submit", (event) -> event.preventDefault() @@ -274,6 +304,9 @@ ChangePasswordFromRecoveryDirective = ($auth, $confirm, $location, $params) -> return {link:link} +module.directive("tgChangePasswordFromRecovery", ["$tgAuth", "$tgConfirm", "$location", "$routeParams", + ChangePasswordFromRecoveryDirective]) + ################### ## Invitation @@ -284,13 +317,13 @@ InvitationDirective = ($auth, $confirm, $location, $params) -> token = $params.token promise = $auth.getInvitation(token) - promise.then (invitation)-> + promise.then (invitation) -> $scope.invitation = invitation promise.then null, (response) -> - $location.path("/login") # TODO: Use the future 'urls' service - $confirm.success("Ooops, we have a problems
- Our Oompa Loompas can't find your invitations.") #TODO: i18n + $location.path("/login") # TODO: Use the future 'urls' service + $confirm.success("Ooops, we have a problems
+ Our Oompa Loompas can't find your invitations.") #TODO: i18n #$############## # Login form @@ -298,22 +331,22 @@ InvitationDirective = ($auth, $confirm, $location, $params) -> $scope.dataLogin = {token: token} loginForm = $el.find("form.login-form").checksley() + onSuccessSubmitLogin = (response) -> + # TODO: finish this. Go to project home page + $location.path("/project/#{$scope.invitation.project_slug}/backlog") + $confirm.notify("success", "You've successfully joined to this project", + "Wellcome to #{$scope.invitation.project_name}") + + onErrorSubmitLogin = (response) -> + $confirm.notify("light-error", "According to our Oompa Loompas, your are not registered yet or + type an invalid password.") #TODO: i18n + submitLogin = -> if not loginForm.validate() return promise = $auth.acceptInvitiationWithExistingUser($scope.dataLogin) - promise.then (response) -> - # TODO: finish this. Go tu project home page - $location.path("/project/#{$scope.invitation.project_slug}/backlog") - $confirm.notify("success", "You've successfully joined to this project", - "Wellcome to #{$scope.invitation.project_name}") - - promise.then null, (response) -> - if response.data._error_message - $confirm.notify("light-error", "According to our Oompa Loompas, - your are not registered yet or - type an invalid password.") #TODO: i18n + promise.then(onSuccessSubmitLogin, onErrorSubmitLogin) $el.on "submit", "form.login-form", (event) -> event.preventDefault() @@ -329,21 +362,22 @@ InvitationDirective = ($auth, $confirm, $location, $params) -> $scope.dataRegister = {token: token} registerForm = $el.find("form.register-form").checksley() + onSuccessSubmitRegister = (response) -> + # TODO: finish this. Go tu project home page + $location.path("/project/#{$scope.invitation.project_slug}/backlog") + $confirm.notify("success", "You've successfully joined to this project", + "Wellcome to #{$scope.invitation.project_name}") + + onErrorSubmitRegister = (response) -> + $confirm.notify("light-error", "According to our Oompa Loompas, the + username or email is already in use.") #TODO: i18n + submitRegister = -> if not registerForm.validate() return promise = $auth.acceptInvitiationWithNewUser($scope.dataRegister) - promise.then (response) -> - # TODO: finish this. Go tu project home page - $location.path("/project/#{$scope.invitation.project_slug}/backlog") - $confirm.notify("success", "You've successfully joined to this project", - "Wellcome to #{$scope.invitation.project_name}") - - promise.then null, (response) -> - $confirm.notify("light-error", "According to our Oompa Loompas, - your username/email or password - are incorrect.") #TODO: i18n + promise.then(onSuccessSubmitRegister, onErrorSubmitRegister) $el.on "submit", "form.register-form", (event) -> event.preventDefault() @@ -355,11 +389,5 @@ InvitationDirective = ($auth, $confirm, $location, $params) -> return {link:link} - -module.directive("tgRegister", ["$tgAuth", "$tgConfirm", RegisterDirective]) -module.directive("tgLogin", ["$tgAuth", "$tgConfirm", "$location", "$tgConfig", LoginDirective]) -module.directive("tgForgotPassword", ["$tgAuth", "$tgConfirm", "$location", ForgotPasswordDirective]) -module.directive("tgChangePasswordFromRecovery", ["$tgAuth", "$tgConfirm", "$location", "$routeParams", - ChangePasswordFromRecoveryDirective]) module.directive("tgInvitation", ["$tgAuth", "$tgConfirm", "$location", "$routeParams", InvitationDirective]) diff --git a/app/coffee/modules/base/confirm.coffee b/app/coffee/modules/base/confirm.coffee index 074b611d..e0ccdb7c 100644 --- a/app/coffee/modules/base/confirm.coffee +++ b/app/coffee/modules/base/confirm.coffee @@ -131,8 +131,8 @@ class ConfirmService extends taiga.Service @.el.find("p").html(NOTIFICATION_MSG[type].message) body = angular.element("body") - body.find(".notification-message .notification-light").removeClass('active'); - body.find(selector).addClass('active'); + body.find(".notification-message .notification-light").removeClass('active') + body.find(selector).addClass('active') if @.tsem cancelTimeout(@.tsem) diff --git a/app/partials/backlog.jade b/app/partials/backlog.jade index a11416de..25f364e2 100644 --- a/app/partials/backlog.jade +++ b/app/partials/backlog.jade @@ -15,7 +15,8 @@ block content div.burndown(tg-gm-backlog-graph) include views/modules/burndown div.backlog-menu - a.trans-button.move-to-current-sprint(href="", title="Move to Current Sprint", id="move-to-current-sprint") + a.trans-button.move-to-current-sprint(href="", title="Move to Current Sprint", + id="move-to-current-sprint") span.icon.icon-move span.text Move to current Sprint a.trans-button(href="", title="Show Filters", id="show-filters-button") diff --git a/app/partials/views/components/notification-message.jade b/app/partials/views/components/notification-message.jade index 8903c41f..cb227eaa 100644 --- a/app/partials/views/components/notification-message.jade +++ b/app/partials/views/components/notification-message.jade @@ -1,6 +1,6 @@ //- To Aadd a new notification type: //- -//- div.notification-message.notification-message-< type > +//- div.hidden.notification-message.notification-message-< type > //- (...) //- h4 < title > //- (...) diff --git a/app/partials/views/modules/login-form.jade b/app/partials/views/modules/login-form.jade index 0819d245..f0f6d02b 100644 --- a/app/partials/views/modules/login-form.jade +++ b/app/partials/views/modules/login-form.jade @@ -13,6 +13,6 @@ div.login-form-container(tg-login) a.button.button-login.button-gray(href="", ng-click="ctrl.submit()", title="Sign in") Sign in input(type="submit", style="display:none") - p.login-text(ng-if="allowPublicRegistration") + p.login-text(ng-if="pubblicRegisterEnabled") span Not registered yet? a(href="", tg-nav="register", title="Register") create your free account here diff --git a/config/main.coffee.example b/config/main.coffee.example index 4b23249e..a8a71c8f 100644 --- a/config/main.coffee.example +++ b/config/main.coffee.example @@ -2,6 +2,12 @@ config = { host: "localhost:8000" scheme: "http" debug: true + pubblicRegisterEnabled: true + defaultLanguage: "en" + languageOptions: { + "es": "Spanish" + "en": "English" + } } angular.module("taigaLocalConfig", []).value("localconfig", config)