Skip to content

Commit

Permalink
Merge pull request #63 from humhub/delete-device-id-after-logout
Browse files Browse the repository at this point in the history
Fix: Delete DeviceID after Logout
  • Loading branch information
luke- authored Jan 6, 2025
2 parents 4a1b827 + 7356b59 commit 625815b
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 49 deletions.
18 changes: 13 additions & 5 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use humhub\modules\fcmPush\components\MailerMessage;
use humhub\modules\fcmPush\components\NotificationTargetProvider;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use humhub\modules\fcmPush\helpers\WebAppHelper;
use humhub\modules\fcmPush\services\DriverService;
use humhub\modules\fcmPush\widgets\PushNotificationInfoWidget;
use humhub\modules\notification\targets\MobileTargetProvider;
Expand Down Expand Up @@ -101,22 +102,28 @@ public static function onLayoutAddonInit($event)
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION);
}

// Before logout
// After logout
if (Yii::$app->session->has(WebAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION)) {
static::registerAssets();
WebAppHelper::unregisterNotificationScript();
Yii::$app->session->remove(WebAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION);
}
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION)) {
MobileAppHelper::unregisterNotificationScript();
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION);
}

// After logout
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_SHOW_OPENER)) {
MobileAppHelper::registerShowOpenerScript();
Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_SHOW_OPENER);
}

if (Yii::$app->user->isGuest) {
return;
if (!Yii::$app->user->isGuest) {
static::registerAssets();
}
}

private static function registerAssets()
{
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');

Expand All @@ -136,6 +143,7 @@ public static function onAfterLogin()

public static function onAfterLogout()
{
Yii::$app->session->set(WebAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1);
}
Expand Down
1 change: 1 addition & 0 deletions assets/FcmPushAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function register($view)
if ($pushDriver !== null) {
Yii::$app->view->registerJsConfig('firebase', [
'tokenUpdateUrl' => Url::to(['/fcm-push/token/update']),
'tokenDeleteUrl' => Url::to(['/fcm-push/token/delete']),
'senderId' => $pushDriver->getSenderId(),
'projectId' => $module->getConfigureForm()->getJsonParam('project_id'),
'apiKey' => $module->getConfigureForm()->firebaseApiKey,
Expand Down
76 changes: 34 additions & 42 deletions controllers/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,58 @@ public function beforeAction($action)

public function actionUpdate()
{
$this->requireLogin();
return $this->update(false, Yii::$app->request->post('token'));
}

$driver = (new DriverService($this->module->getConfigureForm()))->getWebDriver();
if (!$driver) {
Yii::$app->response->statusCode = 400;
return $this->asJson(['success' => false, 'message' => 'No push driver available!']);
}
public function actionUpdateMobileApp()
{
return $this->update(true, Yii::$app->request->post('token'));
}

return $this->asJson([
'success' => (
(new TokenService())->storeTokenForUser(
Yii::$app->user->getIdentity(),
$driver,
Yii::$app->request->post('token'),
)
),
]);
public function actionDelete()
{
return $this->delete(false, Yii::$app->request->post('token'));
}

public function actionUpdateMobileApp()
public function actionDeleteMobileApp()
{
return $this->delete(true, Yii::$app->request->post('token'));
}

private function update(bool $mobile, ?string $token)
{
$this->requireLogin();
if (Yii::$app->user->isGuest) {
throw new HttpException(401, 'Login required!');
}

$driver = (new DriverService($this->module->getConfigureForm()))->getMobileAppDriver();
$driverService = new DriverService($this->module->getConfigureForm());
$tokenService = new TokenService();

$driver = $mobile ? $driverService->getMobileAppDriver() : $driverService->getWebDriver();
if (!$driver) {
Yii::error('Could not update token for mobile app. No driver available.', 'fcm-push');
Yii::error('Could not update token for ' . ($mobile ? 'mobile' : 'web') . ' app. No driver available.', 'fcm-push');

Yii::$app->response->statusCode = 400;
return $this->asJson(['success' => false, 'message' => 'No push driver available!']);
}

return $this->asJson([
'success' => (
(new TokenService())->storeTokenForUser(
Yii::$app->user->getIdentity(),
$driver,
Yii::$app->request->post('token'),
)
'success' => $tokenService->storeTokenForUser(
Yii::$app->user->getIdentity(),
$driver,
$token,
),
]);
}


public function actionDeleteMobileApp()
private function delete(bool $mobile, ?string $token)
{
$driver = (new DriverService($this->module->getConfigureForm()))->getMobileAppDriver();
$driverService = new DriverService($this->module->getConfigureForm());
$tokenService = new TokenService();

$driver = $mobile ? $driverService->getMobileAppDriver() : $driverService->getWebDriver();
if (!$driver) {
Yii::error('Could not delete token for mobile app. No driver available.', 'fcm-push');
Yii::error('Could not delete token for ' . ($mobile ? 'mobile' : 'web') . ' app. No driver available.', 'fcm-push');

Yii::$app->response->statusCode = 400;
return $this->asJson(['success' => false, 'message' => 'No push driver available!']);
Expand All @@ -88,19 +92,7 @@ public function actionDeleteMobileApp()
}

return $this->asJson([
'success' => (
(new TokenService())->deleteToken(
Yii::$app->request->post('token'),
)
),
'success' => $tokenService->deleteToken($token),
]);
}

private function requireLogin(): void
{
if (Yii::$app->user->isGuest) {
throw new HttpException(401, 'Login required!');
}
}

}
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

2.1.3 (Unreleased)
-------------------------
- Fix #32: Delete DeviceID after Logout

2.1.2 (December 19, 2024)
-------------------------
- Fix #262: Switch Network doesn't work
Expand Down
19 changes: 19 additions & 0 deletions helpers/WebAppHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace humhub\modules\fcmPush\helpers;

use Yii;

class WebAppHelper
{
public const SESSION_VAR_UNREGISTER_NOTIFICATION = 'mobileAppUnregisterNotification';

public static function unregisterNotificationScript()
{
if (MobileAppHelper::isAppRequest()) {
return;
}

Yii::$app->view->registerJs('humhub.modules.firebase.unregisterNotification();');
}
}
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"humhub": {
"minVersion": "1.14"
},
"version": "2.1.2",
"version": "2.1.3",
"screenshots": [
"resources/screenshot1.PNG",
"resources/screenshot2.PNG"
Expand Down
29 changes: 29 additions & 0 deletions resources/js/humhub.firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ humhub.module('firebase', function (module, require, $) {
}
};

const deleteTokenToServer = function (token) {
const that = this;
if (that.isTokenSentToServer(token)) {
module.log.info("Delete FCM Push Token to Server");
$.ajax({
method: "POST",
url: that.tokenDeleteUrl(),
data: {token: token},
success: function (data) {
that.deleteTokenLocalStore();
}
});
}
};

const isTokenSentToServer = function (token) {
return (this.getTokenLocalStore() === token);
};
Expand Down Expand Up @@ -96,10 +111,21 @@ humhub.module('firebase', function (module, require, $) {
return item.value;
};

const unregisterNotification = function () {
const token = this.getTokenLocalStore();
if (token) {
this.deleteTokenToServer(token);
}
}

const tokenUpdateUrl = function () {
return module.config.tokenUpdateUrl;
};

const tokenDeleteUrl = function () {
return module.config.tokenDeleteUrl;
};

const senderId = function () {
return module.config.senderId;
};
Expand All @@ -109,11 +135,14 @@ humhub.module('firebase', function (module, require, $) {

isTokenSentToServer,
sendTokenToServer,
deleteTokenToServer,
afterServiceWorkerRegistration,
unregisterNotification,

// Config Vars
senderId,
tokenUpdateUrl,
tokenDeleteUrl,

// LocalStore Helper
setTokenLocalStore,
Expand Down
5 changes: 4 additions & 1 deletion views/admin/mobile-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<h4>Registered FireBase Devices (Current User)</h4>

<?php
$tokens = FcmUser::findAll(['user_id' => Yii::$app->user->id]);
$tokens = FcmUser::find()->where(['user_id' => Yii::$app->user->id])->orderBy('created_at DESC')->all();
?>

<?php if (count($tokens) === 0): ?>
Expand All @@ -105,8 +105,11 @@

&middot;
<?= $fcm->sender_id ?>

&middot;
<?= Yii::$app->formatter->asDatetime($fcm->created_at, 'short') ?>

&middot;
<?= Html::a('Delete', ['mobile-app', 'deleteToken' => $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?>
</li>
<?php endforeach; ?>
Expand Down

0 comments on commit 625815b

Please sign in to comment.