From cd02b08a9f776cc335d9e77983cb44f98d749ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Sun, 14 Aug 2016 23:44:30 +0200 Subject: [PATCH] Hide menu if user is not logged in Will also prompt user to login if user has not logged in yet. Furthermore I need to see if we actually need to really delete cache before asking for the user info. Hmm. --- src/app/menu/menu.controller.ts | 28 +++++++++- src/app/menu/menu.tpl.html | 81 +++++++++++++++++----------- src/app/menu/settings.controller.ts | 30 +++++++---- src/app/services/backend.service.ts | 6 ++- src/app/services/settings.service.ts | 24 ++++++++- 5 files changed, 125 insertions(+), 44 deletions(-) diff --git a/src/app/menu/menu.controller.ts b/src/app/menu/menu.controller.ts index 99cc0e6..ebefbfa 100644 --- a/src/app/menu/menu.controller.ts +++ b/src/app/menu/menu.controller.ts @@ -5,7 +5,8 @@ import { BackendService, WebPageService, TagStorageService, - FileService} from '../services/index'; + FileService, + SettingsService} from '../services/index'; import {IVMScope, ISense} from '../index.interfaces'; @@ -20,6 +21,7 @@ export class MenuCtrl { private BackendService: BackendService, private WebPageService: WebPageService, private TagStorageService: TagStorageService, + private SettingsService: SettingsService, private FileService: FileService) { $scope.vm = this; @@ -129,4 +131,28 @@ export class MenuCtrl { return this.senses && this.senses.length == 0 && this.selectedWord } + /** + * In order to hide the menu before angular has loaded I + * explicitly set display: none; on the div. + * + * Thus to override that after user has logged in + * we use the ng-style attribute in combination with + * this function below. + */ + isUserLoggedIn() { + if (!this.SettingsService.isUserLoggedIn()) return null; + else return {display: 'block'}; + } + + doLogin() { + this.$log.debug('doLogin()') + if (window.tagitTestMode || typeof chrome === 'undefined') return; //do nothing + chrome.runtime.sendMessage({command: 'loginAndRequestUserInfo'}) + } + + continueWithoutLoggingIn() { + this.$log.debug('continueWithoutLoggingIn()') + this.SettingsService.setLoggedIn(true); + } + } diff --git a/src/app/menu/menu.tpl.html b/src/app/menu/menu.tpl.html index 562f5cf..8876438 100644 --- a/src/app/menu/menu.tpl.html +++ b/src/app/menu/menu.tpl.html @@ -44,6 +44,10 @@ background-color: #eee; cursor: pointer; } + + .ng-hide { + display: none !important; + } @@ -65,39 +69,56 @@
-

Actions: - Download tags (current page) | - Download all tags (domain) | - Delete tags from current page -

+
+

Welcome dear user!

-

- Mark one or two words on the page to the right. And we'll supply you with possible definitions. Select a definition and the - word will be tagged. -

+

Please login to use this application.

-

- Currently selected word: {{vm.selectedWord}} -

+

+ Click here to log in +

+

+ Click here to use application without logging in. +

-

- Currently selected word: No word selected. -

+
-
-
-
+ +
+

Actions: + Download tags (current page) | + Download all tags (domain) | + Delete tags from current page +

- +

+ Currently selected word: No word selected. +

+ +
+
+
+ + +
@@ -110,15 +131,13 @@

-
- As you go about tagging pages the tags will be posted to this server endpoint. +
As you go about tagging pages the tags will be posted to this server endpoint.

-
- The email that will be included in the tag when it's sent to the server. +
The email that will be included in the tag when it's sent to the server.

@@ -127,8 +146,6 @@

Reset to default settings

-

Do test login (warning functionality currently being tested)

- diff --git a/src/app/menu/settings.controller.ts b/src/app/menu/settings.controller.ts index 8f4ec9b..86aa35d 100644 --- a/src/app/menu/settings.controller.ts +++ b/src/app/menu/settings.controller.ts @@ -31,7 +31,7 @@ export class SettingsCtrl { $scope.vm = this; this.loadSettings() - this.startListeningForLogins() + this.startListeningToLoginStatus() } loadSettings() { @@ -66,20 +66,32 @@ export class SettingsCtrl { this.SettingsService.resetSettings().then(() => this.loadSettings()) } - testLogin() { + doLogin() { if (typeof chrome === 'undefined') return; //do nothing - chrome.runtime.sendMessage({command: 'requestUserInfo'}) + chrome.runtime.sendMessage({command: 'loginAndRequestUserInfo'}) } - startListeningForLogins() { + logoutUser() { + if (typeof chrome === 'undefined') return; //do nothing + chrome.runtime.sendMessage({command: 'logOutUser'}) + } + + startListeningToLoginStatus() { + if (window.tagitTestMode || typeof chrome === 'undefined') return; //do nothing chrome.runtime.onMessage.addListener( (request, sender, sendResponse) => { - if (request.loginObj) { - this.$log.debug('listenForLogin() got a message from the extension') - this.$log.debug(request) + + this.$log.debug('listenForLogin() got a message from the extension') + this.$log.debug(request) + + if (request === 'deletedUserAuthToken') { + this.SettingsService.resetSettings() } - } - ); + else if (request.loginObj) { + this.emailToTagWith = request.loginObj.email + this.saveSettings(); + } + }) } } \ No newline at end of file diff --git a/src/app/services/backend.service.ts b/src/app/services/backend.service.ts index 11c94ff..9188fd9 100644 --- a/src/app/services/backend.service.ts +++ b/src/app/services/backend.service.ts @@ -36,7 +36,11 @@ export class BackendService { sendTaggedDataToServer(senseTag: ISenseTag) { this.$log.debug('sendTaggedDataToServer() was called'); return this.SettingsService.loadSettings() - .then(loadedSettings => this.$http.post(loadedSettings.tagitSenseDestinationUrl, senseTag)) + .then(loadedSettings => { + //let's add in the user's email to the tag. + senseTag.userEmail = loadedSettings.emailToTagWith + this.$http.post(loadedSettings.tagitSenseDestinationUrl, senseTag) + }) } } \ No newline at end of file diff --git a/src/app/services/settings.service.ts b/src/app/services/settings.service.ts index 7041a91..e53d845 100644 --- a/src/app/services/settings.service.ts +++ b/src/app/services/settings.service.ts @@ -18,6 +18,9 @@ export class SettingsService { // where to send the senses private _defaultSenseDestinationUrl = 'https://www.example.org/somewhere' + //If we have the user's email we treat the user as logged in. + private emailWasLoaded = false; + constructor(private $log: ng.ILogService, private $q: ng.IQService) { } @@ -70,7 +73,10 @@ export class SettingsService { if (!loadedSettings.tagitSenseDestinationUrl) { loadedSettings.tagitSenseDestinationUrl = this._defaultSenseDestinationUrl } - if (!loadedSettings.emailToTagWith) { + + if (loadedSettings.emailToTagWith) { + this.emailWasLoaded = true; + } else { loadedSettings.emailToTagWith = '' } @@ -78,6 +84,22 @@ export class SettingsService { }) } + /** + * Returns true if user was logged in, which + * we treat as true if we have the user's email + */ + isUserLoggedIn() { + return this.emailWasLoaded + } + + /** + * Purpose: Let user's use the app without + * actually logging in. + */ + setLoggedIn(loggedInState) { + this.emailWasLoaded = loggedInState + } + resetSettings() { return this.$q((resolve, reject) => {