From b8bf8902d190ac96db1612f6589ce14aa7f158ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Barrag=C3=A1n=20Merino?= Date: Tue, 4 Nov 2014 23:31:27 +0100 Subject: [PATCH] Task #1424: Login with GitHub --- app/coffee/app.coffee | 11 +- app/coffee/modules/integrations.coffee | 22 ++++ app/coffee/modules/integrations/github.coffee | 110 ++++++++++++++++++ .../views/modules/invitation-login-form.jade | 2 + .../modules/invitation-register-form.jade | 6 - app/partials/views/modules/login-form.jade | 6 +- conf/main.example.json | 3 +- gulpfile.coffee | 1 + 8 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 app/coffee/modules/integrations.coffee create mode 100644 app/coffee/modules/integrations/github.coffee diff --git a/app/coffee/app.coffee b/app/coffee/app.coffee index e708957a..0b014ad3 100644 --- a/app/coffee/app.coffee +++ b/app/coffee/app.coffee @@ -99,15 +99,15 @@ configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEven # User settings $routeProvider.when("/project/:pslug/user-settings/user-profile", - {templateUrl: "/partials/user-profile.html"}) + {templateUrl: "/partials/user-profile.html"}) $routeProvider.when("/project/:pslug/user-settings/user-change-password", - {templateUrl: "/partials/user-change-password.html"}) + {templateUrl: "/partials/user-change-password.html"}) $routeProvider.when("/project/:pslug/user-settings/user-avatar", - {templateUrl: "/partials/user-avatar.html"}) + {templateUrl: "/partials/user-avatar.html"}) $routeProvider.when("/project/:pslug/user-settings/mail-notifications", - {templateUrl: "/partials/mail-notifications.html"}) + {templateUrl: "/partials/mail-notifications.html"}) $routeProvider.when("/change-email/:email_token", - {templateUrl: "/partials/change-email.html"}) + {templateUrl: "/partials/change-email.html"}) $routeProvider.when("/cancel-account/:cancel_token", {templateUrl: "/partials/cancel-account.html"}) @@ -218,6 +218,7 @@ modules = [ "taigaUserSettings", "taigaFeedback", "taigaPlugins", + "taigaIntegrations", # Vendor modules "ngRoute", diff --git a/app/coffee/modules/integrations.coffee b/app/coffee/modules/integrations.coffee new file mode 100644 index 00000000..368e313c --- /dev/null +++ b/app/coffee/modules/integrations.coffee @@ -0,0 +1,22 @@ +### +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# File: modules/integrations.coffee +### + +module = angular.module("taigaIntegrations", []) diff --git a/app/coffee/modules/integrations/github.coffee b/app/coffee/modules/integrations/github.coffee new file mode 100644 index 00000000..7515446f --- /dev/null +++ b/app/coffee/modules/integrations/github.coffee @@ -0,0 +1,110 @@ +### +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# File: modules/integrations/github.coffee +### + +taiga = @.taiga + +module = angular.module("taigaIntegrations") + +AUTH_URL = "https://github.com/login/oauth/authorize" + + +############################################################################# +## User story team requirements button directive +############################################################################# + +GithubLoginButtonDirective = ($window, $params, $location, $config, $events, $confirm, $auth, $navUrls, $loader) -> + # Login or registar a user with his/her github account. + # + # Example: + # tg-github-login-button() + # + # Requirements: + # - ... + + template = """ + + + Login with Github + + """ #TODO: i18n + + link = ($scope, $el, $attrs) -> + redirectToUri = $location.absUrl() + clientId = $config.get("gitHubClientId", null) + + renderGitHubButton = -> + $el.html(template) if clientId + + loginOnSuccess = (response) -> + if $params.next and $params.next != $navUrls.resolve("login") + nextUrl = $params.next + else + nextUrl = $navUrls.resolve("home") + + $events.setupConnection() + + $location.search("next", null) + $location.search("token", null) + $location.search("state", null) + $location.search("code", null) + $location.path(nextUrl) + + loginOnError = (response) -> + $location.search("state", null) + $location.search("code", null) + $loader.pageLoaded() + + if response.data.error_message + $confirm.notify("light-error", response.data.error_message ) + else + $confirm.notify("light-error", "Our Oompa Loompas have not been able to get you + credentials from GitHub.") #TODO: i18n + + loginWithGitHubAccount = -> + type = $params.state + code = $params.code + token = $params.token + + return if not (type == "github" and code) + $loader.start() + + data = {code: code, token: token} + $auth.login(data, type).then(loginOnSuccess, loginOnError) + + renderGitHubButton() + loginWithGitHubAccount() + + $el.on "click", ".button-github", (event) -> + url = "#{AUTH_URL}?client_id=#{clientId}&redirect_uri=#{redirectToUri}&state=github&scope=user:email" + $window.location.href = url + + $scope.$on "$destroy", -> + $el.off() + + return { + link: link + restrict: "EA" + template: "" + } + +module.directive("tgGithubLoginButton", ["$window", '$routeParams', "$tgLocation", "$tgConfig", "$tgEvents", + "$tgConfirm", "$tgAuth", "$tgNavUrls", "tgLoader", + GithubLoginButtonDirective]) diff --git a/app/partials/views/modules/invitation-login-form.jade b/app/partials/views/modules/invitation-login-form.jade index 234866bd..8a0b50b5 100644 --- a/app/partials/views/modules/invitation-login-form.jade +++ b/app/partials/views/modules/invitation-login-form.jade @@ -10,3 +10,5 @@ form.login-form fieldset a.button.button-login.button-gray(href="", title="Log in") Enter input(type="submit", style="display:none") + + fieldset(tg-github-login-button) diff --git a/app/partials/views/modules/invitation-register-form.jade b/app/partials/views/modules/invitation-register-form.jade index b04ec30b..4005dc73 100644 --- a/app/partials/views/modules/invitation-register-form.jade +++ b/app/partials/views/modules/invitation-register-form.jade @@ -23,10 +23,4 @@ form.register-form a.button.button-register.button-gray(href="", title="Sign up") Sign up input(type="submit", style="display:none") - fieldset - a.button.button-github(href="", title="Enter with your github account") - span.icon.icon-github - span Sign Up with Github - input(type="submit", style="display:none") - tg-terms-notice diff --git a/app/partials/views/modules/login-form.jade b/app/partials/views/modules/login-form.jade index 14a9477e..f5648eab 100644 --- a/app/partials/views/modules/login-form.jade +++ b/app/partials/views/modules/login-form.jade @@ -13,10 +13,6 @@ div.login-form-container(tg-login) a.button.button-login.button-gray(href="", title="Sign in") Sign in input(type="submit", style="display:none") - fieldset - a.button.button-github(href="", title="enter with your github account") - span.icon.icon-github - span Login with Github - input(type="submit", style="display:none") + fieldset(tg-github-login-button) tg-public-register-message diff --git a/conf/main.example.json b/conf/main.example.json index 5d382492..e40d5dcd 100644 --- a/conf/main.example.json +++ b/conf/main.example.json @@ -6,5 +6,6 @@ "feedbackEnabled": true, "privacyPolicyUrl": null, "termsOfServiceUrl": null, - "maxUploadFileSize": null + "maxUploadFileSize": null, + "gitHubClientId": null } diff --git a/gulpfile.coffee b/gulpfile.coffee index 85d7788b..fb125940 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -73,6 +73,7 @@ paths.coffee = [ paths.app + "coffee/modules/base/*.coffee", paths.app + "coffee/modules/resources/*.coffee", paths.app + "coffee/modules/user-settings/*.coffee" + paths.app + "coffee/modules/integrations/*.coffee" paths.app + "plugins/**/*.coffee" ]